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
Srini1678Srini1678 

APEX Code Verification

I have written below APEX trigger to update contact address whenever contact account is changed. Code is working but when i am using data loader to update other fields they are not updating and getting this error; i am trying to update a field which is not used in trigger. Any help will great, thanks in advace

 

Error

------

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY:UpdMailingAddress: execution of BeforeUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.UpdMailingAddress: line 17, column 1

 

Trigger

----------

trigger UpdMailingAddress on Contact (before update)
{

Set<Id> parentIds = new Set<Id>();
for(contact c : trigger.new)
{
parentids.add(c.accountid);
}

map<id, account> accountid = new map<id, account>();
accountid.putall([select Id,billingCity,billingCountry, billingPostalCode,billingState, billingStreet from account where id in :parentIds]);

for(contact c:trigger.new)
{
c.mailingcity=accountid.get(c.accountid).billingCity;
c.mailingCountry=accountid.get(c.accountid).billingCountry;
c.mailingPostalCode=accountid.get(c.accountid).billingPostalCode;
c.mailingState=accountid.get(c.accountid).billingState;
c.mailingStreet=accountid.get(c.accountid).billingStreet;

}
}

VVKrishnaVVKrishna

the billingStreet field must not be having any value.  Please try to add a value and then check if it works else try to write a null check in the code 

sfdcfoxsfdcfox
The "AccountId" field is null (it is a contact without an account), so the map has no value for the contact. In the line after "for(contact c:Trigger.new) {" add "if(c.accountd==null) { continue; }".