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
Dipti DDipti D 

Attempt to de-reference a null object - Need Help with Trigger

Attempt to de-reference a null object - Need Help with Trigger

Hi, 
I need help with a trigger. Someone please help me on what I am doing wrong. Thank you.
My scenario is - For a person Account - Address, City & Zip are filled.
When a Case is created for that Customer - Address, City & Zip should automatically be filled. So that Sales reps don't have to fill those 3 fields again.
Here is my CODE
trigger CasesTrigger on Case (after insert){
    Set<Id> accIds = new Set<Id>();
    List<Case> lstCases = new List<Case>();
    for(Case objCase:trigger.new){
        accIds.add(objCase.AccountId);
        system.debug('ACCOUNTIDS'+accIds);
    }
    Map<ID, Account> MapAccIdtoAccount = new Map<ID, Account>([Select Id, Street__c, City__c, State__c, Zip__c from Account where Id IN:accIds]);
system.debug('ACCOUNTSMAP'+MapAccIdtoAccount);
    for(Case objCase:Trigger.new){
        Case oCase = new Case();
       
    if(MapAccIdtoAccount.containsKey(objCase.AccountId))
{
         oCase.Address_Line1__c = MapAccIdtoAccount.get(oCase.AccountId).Street__c ;
            system.debug('ADDRESS---'+oCase.Address_Line1__c); 
           oCase.City__c = MapAccIdtoAccount.get(oCase.AccountId).City__c ;
            oCase.State__c = MapAccIdtoAccount.get(oCase.AccountId).State__c ;
           oCase.Zip__c = MapAccIdtoAccount.get(oCase.AccountId).Zip__c ;
        lstCases.add(oCase);
         system.debug('oCASE'+oCase); 
            }
            }
    if(lstCases.size()>0){
        update lstCases;
    }
}
Best Answer chosen by Dipti D
ClintLeeClintLee
Since you're instantiating a new Case object to do the update, you'll need to include the Id of the Case from the trigger.

Change the first line of your for-loop to this:
 
Case oCase = new Case(Id = objCase.Id);
Hope that helps,

Clint

All Answers

ClintLeeClintLee
In you for loop you are trying to access the map with your new case. 

You should change oCase to objCase when you access the map. Like this:
 
oCase.Address_Line1__c = MapAccIdtoAccount.get(objCase.AccountId).Street__c ;
system.debug('ADDRESS---'+oCase.Address_Line1__c); 
oCase.City__c = MapAccIdtoAccount.get(objCase.AccountId).City__c ;
oCase.State__c = MapAccIdtoAccount.get(objCase.AccountId).State__c ;
oCase.Zip__c = MapAccIdtoAccount.get(objCase.AccountId).Zip__c ;
Hope that helps,

Clint

 
Dipti DDipti D
Hi Lee,
Thanks for your quick response. That helped in resolving the error. But the purpose of trigger - i.e. copying the 3 fields from Account & insering them in Case object did not happen. Do you know what could be the reason? 
ClintLeeClintLee
Since you're instantiating a new Case object to do the update, you'll need to include the Id of the Case from the trigger.

Change the first line of your for-loop to this:
 
Case oCase = new Case(Id = objCase.Id);
Hope that helps,

Clint
This was selected as the best answer
Shukla SumitShukla Sumit
Hi Deepthi,

Agree with Clint Lee comment for update case you new to pass object id in parameters

 Case oCase = new Case(Id = objCase.Id);

Thanks
Sumit Shukla
 
Azhar Aziz GAzhar Aziz G
This trigger should be fire on before insert and update the relavant fields.
 
trigger CasesTrigger on Case (before insert){
    Set<Id> accIds = new Set<Id>();
    List<Case> lstCases = new List<Case>();
    for(Case objCase:trigger.new){
        accIds.add(objCase.AccountId);
        system.debug('ACCOUNTIDS'+accIds);
    }
    Map<ID, Account> MapAccIdtoAccount = new Map<ID, Account>([Select Id, Street__c, City__c, State__c, Zip__c from Account where Id IN:accIds]);
system.debug('ACCOUNTSMAP'+MapAccIdtoAccount);
    for(Case oCase:Trigger.new){
        //Case oCase = new Case();
       
		if( oCase.AccountId != null && MapAccIdtoAccount.get(oCase.AccountId) != null && MapAccIdtoAccount.containsKey(oCase.AccountId)){
			Account relatedAccount = MapAccIdtoAccount.get(oCase.AccountId);
			oCase.Address_Line1__c = relatedAccount.Street__c ;system.debug('ADDRESS---'+oCase.Address_Line1__c); 
			oCase.City__c = relatedAccount.City__c ;
			oCase.State__c = relatedAccount.State__c ;
			oCase.Zip__c = relatedAccount.Zip__c ;
			//lstCases.add(oCase);
			system.debug('oCASE'+oCase); 
        }
    }
    //if(lstCases.size()>0){
        //update lstCases;
    //}
}

 
Dipti DDipti D
Thank you everyone for your prompt responses and time. Clint Lee, your solution worked. Thanks again!
NGR MPL1NGR MPL1
Hi can you plz any one write the test class for above trigger plz.


Regards
Raj