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
nununinununi 

Trigger for cross object update

Can anyone help me with the following trigger?

 

I have "Contacts" object. On the "Contacts" object i have a field called "Parent Account Name" and "Account Name"

On the "Accounts" object i have a field called "Parent Name".

 

If "Account Name" is not equal to "Not Available", then "Parent Account Name" should populate the value from "Parent Name" from the "Accounts" object for that particular account. I hope i am clear!

 

Thanks!!!

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Change this from

 

c.Parent_Name__c = accMap.get(c.AccountId).Parent;

 

to 

 

c.Parent_Name__c = accMap.get(c.AccountId).ParentId;

All Answers

Vinit_KumarVinit_Kumar

Hi,

 

Please try below :-

 

Trigger updateParentAccountName on Contact(before insert,befor update){

List<Contact> conList = new List<Contact>();
List<Contact> accList = new List<Contact>();
List<Id> idList = new List<Id>();

for(Contact con :Trigger.new){
if(con.Account_Name__c !='Not Available'){
idList.add(con.AccountId);
}
}

Map<Id,Account>accMap = new Map<Id,Account>([select Parent_Name__c from Account where id in:idList]);

for(Contact c : trigger.new){
c.Parent_Account_Name__c = accMap.get(c.AccountId).Parent_Name__c;
}

}

nununinununi

Thanks for the reply Vinit

 

The field you refered to as Parent_Name__c is actually a standard look up field with API name Parent. Also con.Account_Name__c should be con.Account since it is a standard look-up field. I changed that so the updated trigger is

 

Trigger updateParentAccountName on Contact(before insert,before update){

List<Contact> conList = new List<Contact>();
List<Contact> accList = new List<Contact>();
List<Id> idList = new List<Id>();

for(Contact con :Trigger.new){
if(con.Account.Name !='Not Available'){
idList.add(con.AccountId);
}
}

Map<Id,Account>accMap = new Map<Id,Account>([select Parent from Account where id in:idList]);

for(Contact c : trigger.new){
c.Parent_Account_Name__c = accMap.get(c.AccountId).Parent;
}

}

 

But got the following error:

 

Error: Compile Error: No such column 'Parent' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 13 column 45

nununinununi

Sorry Vinit, made a few more changes to that trigger and got around that error. This is the new trigger

 

Trigger updateParentAccountName on Contact(before insert,before update){

List<Contact> conList = new List<Contact>();
List<Contact> accList = new List<Contact>();
List<Id> idList = new List<Id>();

for(Contact con :Trigger.new){
if(con.Account.Name !='Not Available'){
idList.add(con.AccountId);
}
}

Map<Id,Account>accMap = new Map<Id,Account>([select ParentId from Account where id in:idList]);

for(Contact c : trigger.new){
c.Parent_Name__c = accMap.get(c.AccountId).Parent;
}

}

 

Still throwing an error: Error: Compile Error: Illegal assignment from SOBJECT:Account to Id at line 16 column 1

 

nununinununi

FYI.. c.Parent_Name__c is also a look up field (to Accounts)

Vinit_KumarVinit_Kumar

Change this from

 

c.Parent_Name__c = accMap.get(c.AccountId).Parent;

 

to 

 

c.Parent_Name__c = accMap.get(c.AccountId).ParentId;

This was selected as the best answer
nununinununi

It works dude!

 

But when i populat 'Not Available' in the account name and manually poppulate a value in the Parent accoount name and save, it wipes out that value. Is there a work around for this?

Vinit_KumarVinit_Kumar

Try this change from 

 

for(Contact c : trigger.new){
c.Parent_Name__c = accMap.get(c.AccountId).Parent;
}

 

 

to 

 

for(Contact c : trigger.new){

if(c.Parent_Name__c!=null){
c.Parent_Name__c = accMap.get(c.AccountId).Parent;
}

}

 

nununinununi

replaced with a few corrections, trigger saved. But getting this error when trying to save a record with 'Not Available' in the acount name.

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger updateParentAccountName caused an unexpected exception, contact your administrator: updateParentAccountName: execution of BeforeUpdate caused by: System.StringException: Invalid id: Not Available: Trigger.updateParentAccountName: line 17, column 1

Vinit_KumarVinit_Kumar

Check the debug logs,this should not happen.

nununinununi

Basicaly this trigger is only executing when the Parent Account Name field is blank., whereas it should execute regardless.

 

Account Name != Not Available

Parent Acount Name is BLANK

 

Trigger executes

 

Accouont Name != Not Available

Parent Account Name is not Blank

 

Error thrown

 

Account Name = Not Avialble

Parent Accoount Name is blank

 

Trigger Executes

 

Account Name = Not Available

Parent Account is not Blank

 

Error thrown

nununinununi

Correction..

 

Account Name != Not Available

Parent Acount Name is BLANK

 

Trigger executes

 

Accouont Name != Not Available

Parent Account Name is not Blank

 

Trigger Executes

 

Account Name = Not Avialble

Parent Accoount Name is blank

 

Trigger Executes

 

Account Name = Not Available

Parent Account is not Blank

 

No error.. parent Accoount field wiped out