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
HBF SFDCHBF SFDC 

Easy Trigger to update Child record from Account

I'm new to APEX and I want to start simple.  Can somebody help me with a simple example of creating the following trigger

 

My Objects are the standard Account object and the Owner field, and a simply child record and a "Sales Rep" user lookup field.

 

Every time a new child record is created, I want the "Sales Rep" field to be populated with the same name as the "Account Owner" field on the Standard Account Object.

 

I try to do these kind of things with configuration, field formula, and/or workflows, but the above does not seem to be possible with SFDC out of the box.

 

Thanks for any help!

 

Aaron

Best Answer chosen by Admin (Salesforce Developers) 
aalbertaalbert

Here is some sample code:

 

 

trigger updateChildRecord on ChildRecord__c (before insert){
  
    Map<Id,Account> accounts = new Map<Id,Account>{[select id, ownerid from Account where Id IN :trigger.new]};
  
    for(ChildRecord__c child : trigger.new){
         if(child.Account__c != null){
	    child.Sales_Rep__c = accounts.get(child.Account__c).OwnerId;	
          }
    }	
 
} 

You also might need a trigger on Account that checks when the Owner changes to then sync that change with the child record.

 

All Answers

aalbertaalbert

Here is some sample code:

 

 

trigger updateChildRecord on ChildRecord__c (before insert){
  
    Map<Id,Account> accounts = new Map<Id,Account>{[select id, ownerid from Account where Id IN :trigger.new]};
  
    for(ChildRecord__c child : trigger.new){
         if(child.Account__c != null){
	    child.Sales_Rep__c = accounts.get(child.Account__c).OwnerId;	
          }
    }	
 
} 

You also might need a trigger on Account that checks when the Owner changes to then sync that change with the child record.

 

This was selected as the best answer
HBF SFDCHBF SFDC

Thank you so much for the help!  I will try this out and let you know how it goes.

 

Aaron

HBF SFDCHBF SFDC

aalbert-

 

Thanks again for your help.  I attempted to use this code in my org, and I'm getting an error message:

 

 

ErrorError: Compile Error: Invalid bind expression type of SOBJECT:CCF__c for Id field of SObject Account at line 5 column 49

 

 

Here is the code:

 

 

trigger updateComplaint on CCF__c (before insert)
{
Map<Id,Account> accounts = new Map<Id,Account>
([SELECT id, Ownerid from Account where id IN : trigger.new]);
for(CCF__c child : trigger.new)
 {
  if(child.Account__c != null)
  {
    child.Territory_Manager__c = accounts.get(child.Account__c).Ownerid;
  }
 }
}

 

Got any suggestions and/or place I can look to figure out why I'm getting this error?

 

Thanks in advance!

Aaron

David81David81

I believe you want...

 

 

Map<Id,Account> accounts = new Map<Id,Account>([SELECT id, Ownerid from Account where id IN : trigger.newMap.keySet()]);

 

HBF SFDCHBF SFDC

Thanks for the help!  I'm now past error number 1.  On to error number two:

 

Here is the error:

 

 

 Error: Compile Error: Invalid field Account__c for SObject CCF__c at line 9 column 47

 

Here is the code:

 

trigger updateComplaint on CCF__c (before insert)
{
Map<Id,Account> accounts = new Map<Id,Account>
([SELECT id, Ownerid from Account where id IN : trigger.newMap.keySet()]);
for(CCF__c child : trigger.new)
 {
  if(child.Account__c != null)
  {
    child.Territory_Manager__c = accounts.get(child.Account__c).Ownerid;
  }
 }
}

 

The error line is:

child.Territory_Manager__c = accounts.get(child.Account__c).Ownerid;

 

 

Got any ideas?

 

Thanks!

Aaron

David81David81

What is the field name that is a lookup to Account on your CCF__c object?

HBF SFDCHBF SFDC

The field I want to complete, which is a USER lookup on the Child Record (CCF__c) is called:

 

Territory_Manager__c

 

It needs to be the same person (ID) as the "Owner" of the standard Object "Account"  The Account object is the Master Object in the object relationship.

 

Does clarify the info you need?

Thanks!

Aaron

 

 

David81David81

Nope. Need to know the name of the field that is that Master-Detail to Account.

 

It looks like (from the error message) it is not named "Account__c".

 

The error is basically saying that your CCF__c object doesn't have a field called "Account__c" so we need to replace that with the proper field name.

HBF SFDCHBF SFDC

OK.  I'm going to go thru the whole process of making this trigger to hopefully clarify.

 

The Account is the master record to a Complaint record (CCF__c).  The master-detail relationship is built from the "Account Name" (Account.Account_Name) field on the Account record which matches the "Account Name" field on the Complaint (CCF__c.Account_Name__c) record.

 

When a new complaint is created, I want to also pull the Account Owner (account.ownerid) from the Master Account Record and put it in the Territory Manager (CCF__c.Territory_Manager__c) field on the Child record.  The Territory_Manager__c field is a Lookup field to the USER object.

 

If this is still unclear, feel free to call me: 651.236.5312

 

Thanks so much!

Aaron

David81David81

Now that I have actually looked at the whole trigger, I believe you'll need some other edits.

 

First you'll need to loop the inserted complaints and collect the Account Ids (Account_Name__c is sounds like) in a Set.

Then use that Set in your WHERE clause to filter the Accounts.

Then you can continue as before and pull the Account.OwnerId from the value of the Map.

 

Hopefully that makes sense, if not, let me know and I'll take a crack at the actual code.

HBF SFDCHBF SFDC

Thanks again for the help!  

 

Since I've not written a trigger, my plan was to get some help writing this one, then study it and expand upon it.  Apparently this is NOT a good (and easy) trigger to begin with.  I don't know how to code what you suggest.  If you don't want to give it a shot (pseudo code would work for me), that's fine!  If so, go ahead and make my day :)

 

Thanks!

Aaron

David81David81

Try this...

 

 

trigger updateComplaint on CCF__c (before insert){

Set<Id> accountIds = new Set<Id>();
for(CCF__c ccf : trigger.new){
	if(ccf.Account_Name__c!=null){
	accountIds.add(ccf.Account_Name__c);
	}
}

Map<Id,Account> accounts = new Map<Id,Account>([SELECT id, Ownerid from Account where id IN :accountIds]);

for(CCF__c ccf : trigger.new){
	if(ccf.Account_Name__c != null){
		ccf.Territory_Manager__c = accounts.get(ccf.Account_Name__c).Ownerid;
	}
}

}

 

 

HBF SFDCHBF SFDC

It works great!  Thanks so much!!

 

Aaron