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
Laytro80Laytro80 

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

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

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)

 

 

trigger OppSumAfterInsertUpdate on Opportunity_Summary__c (before insert, before update) {

Set<Id> aids = new Set<Id> ();
for (Opportunity_Summary__c c : Trigger.new) {
aids.add(c.Contact__c); }

Map<ID, Contact> m = new Map<ID, Contact>([select id, AccountId from contact where Id in :aids]);

for (Opportunity_Summary__c c : Trigger.new) {
        if (c.Account__c == null) {
        if (m.containsKey(c.Contact__c)) {
        c.Account__c = m.get(c.Contact__c).AccountId; }
        }
        }
}

 

 

 

All Answers

gv007gv007

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.

Laytro80Laytro80

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

dnakonidnakoni

You can try something similar to this:

 

 

trigger customObjectAfterInsertUpdate on customObject__c (after insert, after update)
{
     for (customObject__c c : Trigger.new)
     {
           c.Account__c = c.Contact__r.AccountId;
     }
}

 

 

Let me know if you need some clarification.

 

 

Laytro80Laytro80

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

 

 

trigger OppSumAfterInsertUpdate on Opportunity_Summary__c (after insert, after update)
{
     for (Opportunity_Summary__c c : Trigger.new)
     {
           c.Account__c = c.Primary_Contact__r.Account.ID;
     }
}

 

 

 

 

dnakonidnakoni

Have you tried changing the update to 'before update' ? You should be able to modify the record on before update.

Laytro80Laytro80

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

 

 

trigger OppSumAfterInsertUpdate on Opportunity_Summary__c (after insert, before update)
{
     for (Opportunity_Summary__c c : Trigger.new)
     {
           c.Account__c = c.Primary_Contact__r.Account.ID;
     }
}

 

 

dnakonidnakoni

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.

Laytro80Laytro80

Ok so no error message which is good an the record saves.

 

But it does insert the account?

 

 

trigger OppSumAfterInsertUpdate on Opportunity_Summary__c (before insert, before update)
{
     for (Opportunity_Summary__c c : Trigger.new)
     {
           c.Account__c = c.Primary_Contact__r.Account.ID;
     }
}

 

 

dnakonidnakoni

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?

Laytro80Laytro80

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

Laytro80Laytro80

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?

BritishBoyinDCBritishBoyinDC

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)

 

 

trigger OppSumAfterInsertUpdate on Opportunity_Summary__c (before insert, before update) {

Set<Id> aids = new Set<Id> ();
for (Opportunity_Summary__c c : Trigger.new) {
aids.add(c.Contact__c); }

Map<ID, Contact> m = new Map<ID, Contact>([select id, AccountId from contact where Id in :aids]);

for (Opportunity_Summary__c c : Trigger.new) {
        if (c.Account__c == null) {
        if (m.containsKey(c.Contact__c)) {
        c.Account__c = m.get(c.Contact__c).AccountId; }
        }
        }
}

 

 

 

This was selected as the best answer
Laytro80Laytro80

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: