You need to sign in to do that
Don't have an account?
Dipti 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;
}
}
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;
}
}
Change the first line of your for-loop to this:
Hope that helps,
Clint
All Answers
You should change oCase to objCase when you access the map. Like this:
Hope that helps,
Clint
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?
Change the first line of your for-loop to this:
Hope that helps,
Clint
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
Regards
Raj