You need to sign in to do that
Don't have an account?
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
Here is some sample code:
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
Here is some sample code:
You also might need a trigger on Account that checks when the Owner changes to then sync that change with the child record.
Thank you so much for the help! I will try this out and let you know how it goes.
Aaron
aalbert-
Thanks again for your help. I attempted to use this code in my org, and I'm getting an error message:
Here is the code:
Got any suggestions and/or place I can look to figure out why I'm getting this error?
Thanks in advance!
Aaron
I believe you want...
Thanks for the help! I'm now past error number 1. On to error number two:
Here is the error:
Here is the code:
The error line is:
child.Territory_Manager__c = accounts.get(child.Account__c).Ownerid;
Got any ideas?
Thanks!
Aaron
What is the field name that is a lookup to Account on your CCF__c object?
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
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.
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
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.
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
Try this...
It works great! Thanks so much!!
Aaron