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
KyoKyo 

Pop Field Case to Field Case

Hi

I have a problem poppulate Field.

Email_Subject__c = Subject At before insert and subject = null

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger ChangeEmail caused an unexpected exception, contact your administrator: ChangeEmail: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.ChangeEmail: line 13, column 52

 

trigger ChangeEmail on Case(before insert) {
   
   
    Set<Id> CaseIDs = new Set<Id>();
            
    for (Case COT: trigger.new) {
        CaseIDs.add(COT.id);
    }        
     Map<Id, Case> pSale = new Map<Id, Case>([SELECT a.id, a.Email_Subject__c,a.Subject FROM Case a WHERE a.id IN :CaseIDs]);
                                                         
   for(Case fcon : Trigger.New){
   if(fcon.Origin == 'Email'){
        fcon.Email_Subject__c = pSale.get(fcon.id).Subject;
        }
   }
    

}

 

Thank you

 

 

dotnet developedotnet develope

Hi Kyo,

 

In your code you are retriving the data which is not exists/ inserted in the system.

 

As per your requirement i have changed some code and given below.

 

trigger ChangeEmail on Case(before insert) {
   
   /*
    Set<Id> CaseIDs = new Set<Id>();
            
    for (Case COT: trigger.new) {
        CaseIDs.add(COT.id);
    }        
     Map<Id, Case> pSale = new Map<Id, Case>([SELECT a.id, a.Email_Subject__c,a.Subject FROM Case a WHERE a.id IN :CaseIDs]);
     */                                                    
   for(Case fcon : Trigger.New){
   if(fcon.Origin == 'Email'){
        fcon.Email_Subject__c = fcon.Subject;
        }
   }
    

}