+ Start a Discussion
Anshuman ParhiAnshuman Parhi 

Apex trigger devlopment

  1. Create custom object ‘Complaints’ with appropriate fields. It will have ‘Response Date’ field (Date/time). It will also have ‘Priority’ field (Picklist). Write a code which will auto-update Response Date field based on Priority using following mapping:             
 
PriorityResponse Date
CriticalCreation Date + 6 Business Hours
HighCreation Date + 2 Business Days
MediumCreation Date + 5 Business Days
LowCreation Date + 10 Business Days

[Response Date cannot include Saturday/ Sunday in calculation as they are not business days]

Any solution for this..
Best Answer chosen by Anshuman Parhi
CharuDuttCharuDutt
Hii Ayushman
Try Below Code
trigger ComplaintsTrigger on Complaints__c (before Update) {

    for(Complaints__c Acc:trigger.new){
         if(Acc.Priority__c == 'Critical'){
            Acc.Response_Date__c = Acc.CreatedDate.AddHours(6);
        }
        else if(Acc.Priority__c == 'High'){
            Acc.Response_Date__c = Acc.CreatedDate.AddDays(2);
        }
        else if(Acc.Priority__c == 'Medium'){
            Acc.Response_Date__c = Acc.CreatedDate.AddDays(5);
        }
        else if(Acc.Priority__c == 'Low'){
            Acc.Response_Date__c = Acc.CreatedDate.AddDays(10);
        }
       
       datetime myDate = datetime.newInstance(Acc.Response_Date__c.year(), Acc.Response_Date__c.month(), Acc.Response_Date__c.day());
       String day = myDate.format('EEEE');
         system.debug(day);
       
        if(day == 'Sunday' || day == 'Saturday'){
            Acc.priority.AddError('Error');
        }

	}
}
Please Mark It As Best Answer If It Helps
Thank You!

 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Anshuman,

>> for creating the custom object you can go to setup and follow the below steps to create a custom object with custom fields.

https://trailhead.salesforce.com/content/learn/modules/create-a-custom-object-quick-look/create-a-custom-object

For trigger, you can use the below link and modify the snippet present in it to fit your use case:

>> https://salesforce.stackexchange.com/questions/79090/using-businesshours-add-for-calculating-a-due-date-in-a-datetime-field

I am adding the snippet here for quick reference:
 
trigger CalculateQualificationDueDate on Case (before insert, before update) {
    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
    for( Case c : Trigger.new )
    {
        c.Qualification_Due_Date__c = BusinessHours.add(bh.Id, Datetime.Now (), 32400000);  
    }    
}

Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.  

Thanks.
CharuDuttCharuDutt
Hii Ayushman
Try Below Code
trigger ComplaintsTrigger on Complaints__c (before Update) {

    for(Complaints__c Acc:trigger.new){
         if(Acc.Priority__c == 'Critical'){
            Acc.Response_Date__c = Acc.CreatedDate.AddHours(6);
        }
        else if(Acc.Priority__c == 'High'){
            Acc.Response_Date__c = Acc.CreatedDate.AddDays(2);
        }
        else if(Acc.Priority__c == 'Medium'){
            Acc.Response_Date__c = Acc.CreatedDate.AddDays(5);
        }
        else if(Acc.Priority__c == 'Low'){
            Acc.Response_Date__c = Acc.CreatedDate.AddDays(10);
        }
       
       datetime myDate = datetime.newInstance(Acc.Response_Date__c.year(), Acc.Response_Date__c.month(), Acc.Response_Date__c.day());
       String day = myDate.format('EEEE');
         system.debug(day);
       
        if(day == 'Sunday' || day == 'Saturday'){
            Acc.priority.AddError('Error');
        }

	}
}
Please Mark It As Best Answer If It Helps
Thank You!

 
This was selected as the best answer