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
Anant Pandey 8Anant Pandey 8 

Trigger on case object

HI All,

I am writing a trigger as below to update Case due date automtically while insert.
I don't why this trigger is not working.
however when i remove caselst.add(c); this works fine. Please help

trigger DueDate on Case (before insert) {
    
    list<case>  caselst = new list <case>();
    
    for(Case c:trigger.new){
               
        if(c.Date_Due__c==Null){
            
            c.Date_Due__c = system.today() + 1;
                                          
          }
             
        caselst.add(c);
      }
      
   Insert caselst;
  
}
Best Answer chosen by Anant Pandey 8
Khan AnasKhan Anas (Salesforce Developers) 
Hi Anant,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger UpdateCaseField on Case (before insert) {
    
    for(Case c : Trigger.new){
        if (c.Date_Due__c == Null) {
            c.Date_Due__c = System.today() + 1;
        }   
    } 
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Anant,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger UpdateCaseField on Case (before insert) {
    
    for(Case c : Trigger.new){
        if (c.Date_Due__c == Null) {
            c.Date_Due__c = System.today() + 1;
        }   
    } 
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Naren9Naren9
Hi Anant,
As your Trigger is on Case before insert, you no need to use the insert caselst method. automatically system save will be called after before insert or before update. I hope this will help why it is working if you remove the insert caselst.

Thanks,
Narendar
Anant Pandey 8Anant Pandey 8
Your code is working fine. Can you please help me with the test class for the same
Khan AnasKhan Anas (Salesforce Developers) 
Anant, use below test class:
 
@isTest
public class Test_UpdateCaseField {
    
    static testMethod void insertCase() {
        
        Case c = new Case();
        c.Status = 'New';
        c.Origin = 'Phone';
        INSERT c;
    }
}

I hope it helps you.

Kindly mark this as solved if the information was helpful. It will help to keep this community clean.

Regards,
Khan Anas
devloper sfdcdevloper sfdc
@isTest
 
public class Test_UpdateCaseField {
 
     
 
    static testMethod void UpdaateCase() {
 
         List<case>lstcase=new List<case>();
 
        Case c = new Case();
 
        c.Status = 'New';
 
        c.Origin = 'Phone';
        
       c.Date_Due__c=system.today();
  
       lstcase.add(c);

    INSERT lstcase;
 
    }
 
}

Please Use this code.
Anant Pandey 8Anant Pandey 8
Thanks Everyone. It wroked. Appriciate your Help.