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
MikeCloudMikeCloud 

Auto populate account lookup field on custom object

I have a custom object with contact lookup field and account lookup field, when I enter Contact I want Account lookup field to populate based on the contact (associated to only one account) to cut down on all the lookups my users need to do in this object page layout.

raj123raj123

Write a trigger to after insert to update the account lookup field to update with the account id of the populated contact id 

 

you can do this by queryin the accouintID field from the contact and populate it on the related account field and update record.

MikeCloudMikeCloud
Thanks, is there any examples of this. Mike
SFAdmin5SFAdmin5

you want something like this.  note the active__c field is just a yes/no picklist and is used for demo purposes.  your trigger would just swap out this active__c field for the account lookup fields

trigger customobjectTrigger on Custom_Object__c (before insert, before update) {
  for(Custom_Object__c co: Trigger.new){
    if (co.Contact__c != null){
      co.Active__c = 'Yes';
    }
  }
}

 

 

MikeCloudMikeCloud
Got it THANKS!