I have the lookup field New_Contact__c
in Account and Opportunity.When I create New_ Contact__c in Account record and save ,the same value should be populated on to Opportunity lookup field New_ Contact__c.
I wrote the below trigger to update opportunity field based on value in account field.
Trigger:
trigger contactupdation on Account (after update) {
Set<ID> OppAcc = New Set<id>();
List<Opportunity> opps = New List<Opportunity>();
for(Account a : Trigger.New)
{
OppAcc.add(a.Id);
}
Map<Id, Opportunity> mapOppty = new Map<Id, Opportunity>([SELECT Id, Primary_Contact__c FROM Opportunity where Id IN :OppAcc]);
for(Account acc : Trigger.New){
Opportunity opp = mapOppty .get(acc.Id);
opp.Primary_Contact__c= acc.Primary_Contact__c;
opps.add(opp);
}
update opps;
}
Error: contactupdation: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.contactupdation: line 16, column 1
I got this error while creating New_Contact__c Field in Account.
Anyone help me solve this Scenario.
Thanks, Jane
Opportunity
, but trying to find Opportunity Ids that are the same as your Account Ids. As is, that query will never return any results. Your WHERE clause should be comparing the account Id of the Opp against your set of Account Ids.WHERE AccountId IN :OppAcc
.