You need to sign in to do that
Don't have an account?

Apex Trigger
Hi,
I have an object with a lookup to both contact and account.
Is it possible to have the account lookup populated automatically when you add a contact by using the account they are assigned to?
If so can this be done my a trigger or another method.
Thanks
Ross
Not quite - the problem is that the trigger doesn't know what the Contact's Account Id is - triggers don't store data relationships for related objects I believe
So you have to populate that info in a map beforehand, and then use the map as your lookup table (I think you'll need to change the field names to be the ones you have used)
All Answers
To excute the trigger you need do some action what is action you are going to excute yours trigger in lead anc contact there is no merge option.
Hi,
Thanks for the post.
I was thinking when the user adds a contact and saves the record a trigger would fire.
The trigger would pull through the account name using the contact lookup.
But I am not sure if this is possible or how to write the code.
Thanks
Ross
You can try something similar to this:
Let me know if you need some clarification.
Thanks for this Daniel, I get this error so I tried changing the after to a before but still no joy.
Error:Apex trigger OppSumAfterInsertUpdate caused an unexpected exception, contact your administrator: OppSumAfterInsertUpdate: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.OppSumAfterInsertUpdate: line 5, column 12
Have you tried changing the update to 'before update' ? You should be able to modify the record on before update.
Thanks for helping.
I get this error message still.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger OppSumAfterInsertUpdate caused an unexpected exception, contact your administrator: OppSumAfterInsertUpdate: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.OppSumAfterInsertUpdate: line 5, column 12
Oh, see, the problem is the trigger will run on before update, but it still is setup for after insert, and as you can see from the error message, the error is on insert. Change the after insert to before insert and try again.
Ok so no error message which is good an the record saves.
But it does insert the account?
Now that I think about it, you can just create a formula field on the Opportunity_Summary__c that would be similar to: Primary_Contact__r.Account.Name. That would display the account name in the field. Is that what you need or do you need your users to see an actual lookup where they can click on the account name and be taken to that account?
Hi,
Thanks for all your help.
I had a formula which pulled through the account name first of all but it was changed to a lookup. The problem I have is that an opportunity summary can be linked to a contact or account. If the account if populated than the contact field does not have to be.
But if they populate the contact they must also populate the account.
Thanks again,
Ross
So I guess the problem is before the record is saved the contact is not populated so the trigger will not be able to pull through the account name because it does not know.
So you have to save the record first.
But when you save the record it is locked so you can no longer add the account name even though you now know what it is.
Is it possible to write a trigger that fires when the contact field is no longer blank, updates the record (quick save) and then pulls through the account name and saves?
Not quite - the problem is that the trigger doesn't know what the Contact's Account Id is - triggers don't store data relationships for related objects I believe
So you have to populate that info in a map beforehand, and then use the map as your lookup table (I think you'll need to change the field names to be the ones you have used)
Thanks so much for taking the time to get me an explantion on the solution and providing the sample code (which works so well).
Thanks again all the very best
Ross
:smileyvery-happy: