function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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
Ankita TodkarAnkita Todkar
Hello everyone,
Can anyone give me the test cases for this code.I have tried it but it is showing 40% code coverage.
Here is my test class:

@isTest
public class ComplaintsTest {
    
    @isTest
    public static void testComplaintsClass(){
        List<Complaint__c> cc= [SELECT id,Name, Response_Date__c,Priority__c FROM Complaint__c WHERE Name='My first Case'];
        
        Datetime myDateTime = ComplaintsHandler.updateResponseDate(cc);
        
        Datetime myDateTimeExpected = Datetime.newInstance(2021, 7, 27);
        System.assertEquals(myDateTimeExpected,myDateTime);
        
        
    }
    
    @isTest
    public static void testComplaintsClassSecond(){
        List<Complaint__c> cc= [SELECT id,Name, Response_Date__c,Priority__c FROM Complaint__c WHERE Name='Test 1'];
        String s='';
        for(Complaint__c clist:cc){
            s=clist.Priority__c;
            
        }
        
        if(s.equals('Low')){
            Datetime myDateTime=ComplaintsHandler.updateResponseDate(cc);
            
            Datetime myDateTimeExpected = Datetime.newInstance(2021, 8, 10);
            System.assertEquals(myDateTimeExpected,myDateTime);
        }
        
        
        
    }
    
}