0

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

  • Beyond the NPE, you have a fundamental issue with your query. You're querying 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. – Derek F Aug 26 '20 at 15:23
1
Opportunity opp = mapOppty .get(acc.Id);

This will always be null; the Id for this map is the Opportunity Id, not the Account Id. Also, it doesn't properly handle the situation of multiple opportunities per account. In addition, it doesn't check if any work needs to be done, wasting CPU time and queries.

Ideally, you should just do this with a Process Builder flow. No need to get Apex involved.

However, this is how I'd fix it:

Opportunity[] updates = new Opportunity[0];
for(Opportunity record:[SELECT AccountId,New_Contact__c FROM Opportunity WHERE AccountId = :Trigger.new]) {
  Account accountRecord = Trigger.newMap.get(record.AccountId).New_Contact__c;
  if(record.New_Contact__c != accountRecord.New_Contact__c) {
    record.New_Contact__c = accountRecord.New_Contact__c;
    updates.add(record);
  }
}
update updates;

You could also just restrict the update to when actual changes occur:

Account[] changes = new Account[0];
for(Integer i = 0, s = Trigger.new.size(); i < s; i++) {
  if(Trigger.new[i].New_Contact__c != Trigger.old[i].New_Contact__c) {
    changes.add(Trigger.new[i]);
  }
}
Opportunity[] updates = new Opportunity[0];
for(Opportunity record:[SELECT AccountId,New_Contact__c FROM Opportunity WHERE AccountId = :changes]) {
  Account accountRecord = Trigger.newMap.get(record.AccountId);
  if(record.New_Contact__c != accountRecord.New_Contact__c) {
    record.New_Contact__c = accountRecord.New_Contact__c;
    updates.add(record);
  }
}
update updates;
  • I got this error -Error: Compile Error: Illegal assignment from Id to Account at line 10 column 15. Can you please explain this line Account accountRecord = Trigger.newMap.get(record.AccountId).New_Contact__c; – Jane Aug 26 '20 at 15:14
  • @Jane Sorry, that was a copy-paste error. The .New_Contact__c should not be on that line. I've edited this. – sfdcfox Aug 26 '20 at 15:16
  • This error showing while creating New_Contact__c field .**contactupdation: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Opportunity.AccountId Trigger.contactupdation: line 10, column 1** – Jane Aug 26 '20 at 15:40
  • @Jane Yes, I forgot to query the field. – sfdcfox Aug 26 '20 at 15:52
  • Thanks a lot ,It's working .I have to add one more scenario.I need to update the New_Contact__c field from Account to Opportunity and Opportunity Product.I am trying to update ,It's updated but the mistake is I am not able to see the Updated New_Contact__c field in Opportunity.It is only shown in the Opportunity Product.What can be done to fix this.Give me some idea to solve. – Jane Aug 27 '20 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.