You need to sign in to do that
Don't have an account?
Neha Kakroo
Adding a record of one object in the related list of another custom object
I have 2 custom objects on Contact. One is "Address" and the other object is a junction object
between Account & Contact. It is called "Account_Contact_Relationship__c".
Now whenever a user creates a new Account Contact Relationship object, the address of the
Account on this object should come as record under the "Address" related list on Contact Page.
I am writing the trigger as below but it is giving me Null Pointer exception at line 29 column 1.
(ac.Account__r.Marketting_City__c= imap.get(ac.Account__c).Marketting_City__c;)
Can somebody please help me with the trigger:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | trigger AddressUpdate on Account_Contact_Relationship__c (before insert,after insert) { List<Address__c> addr = new List<Address__c>(); List<id> insertaid = new List<id>(); //List<id> updateaid = new List<id>(); if(trigger.isinsert) { for(Account_Contact_Relationship__c ac: Trigger.new) { insertaid.add(ac.Account__c); } system.debug(insertaid); /* if(trigger.isupdate) { for(Account_Contact_Relationship__c ac: Trigger.new) { updateaid.add(ac.accountid); } }*/ Map<id,Account> imap = new Map<id,Account>([Select Marketting_Street__c,Marketting_City__c,MarketingState__c,Marketting_PostalCode__c,ISO_Country_Code__c from Account where id IN: insertaid]); //Map<id,Account> umap = new Map<id,Account>([Select Marketting_Street__c,Marketting_City__c,MarketingState__c,Marketting_PostalCode__c,ISO_Country_Code__c from Account where id IN: updateaid]); if(trigger.isinsert) { for(Account_Contact_Relationship__c ac: Trigger.new) { ac.Account__r.Marketting_City__c= imap.get(ac.Account__c).Marketting_City__c; ac.Account__r.Marketting_Street__c=imap.get(ac.Account__c).Marketting_Street__c; } }/* if(trigger.isupdate) { for(Account_Contact_Relationship__c ac: Trigger.new) { ac.Account__r.Marketting_City__c= umap.get(ac.accountid).Marketting_City__c; ac.Account__r.Marketting_Street__c=umap.get(ac.accountid).Marketting_Street__c; } }*/ for(Account_Contact_Relationship__c ac: Trigger.new) { if(trigger.isinsert) { Address__c a = new Address__c (City__c=ac.Account__r.Marketting_City__c,Name=ac.Account__r.Marketting_Street__c); addr.add(a); } } insert(addr); } } |
try to debug your code at line num 21 to make sure the query is returning expected results. and let us know the results
I debugged the code and the query is returing the expected results. But I am again getting the below error
"System.NullPointerException: Attempt to de-reference a null object: Trigger.AddressUpdate: line 30, column 1"
the problem with the code is that you calling ac.Account__r.Marketting_City__c in before insert trigger.
since it is a before insert trigger, this reference doesnt exist, so throwing a null pointer exception.