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
SnehalCSnehalC 

Auto Populate lookup fields based on another lookup field value

Hi,

I have two objects A & B. I have 4 lookup fields on object A with object B. When I select lookup field value in first field; I want all the other 3 lookup fields to be autopopulated. (All the three are user fields on object B which are looked up in object A). How to accomplish this? Thanks in advance!
Best Answer chosen by SnehalC
Satish_SFDCSatish_SFDC
Is it possible to use three different formula fields to get the values from the Parent object.

If not you can write your own apex trigger for this.

<code>
trigger myTrigger on ObjectA__c(before insert, before update){
    for(Object A obj : Trigger.new){
        if(obj.Lookupfield__c !=null){
            obj.secondLookUp = obj.LookupField__c;
            obj.thirdLookup = obj.lookupfield__c;
            obj.fourthlookup = obj.lookupfield__c;
        }
    }
}

</code>


Regards,
Satish Kumar

All Answers

Satish_SFDCSatish_SFDC
Is it possible to use three different formula fields to get the values from the Parent object.

If not you can write your own apex trigger for this.

<code>
trigger myTrigger on ObjectA__c(before insert, before update){
    for(Object A obj : Trigger.new){
        if(obj.Lookupfield__c !=null){
            obj.secondLookUp = obj.LookupField__c;
            obj.thirdLookup = obj.lookupfield__c;
            obj.fourthlookup = obj.lookupfield__c;
        }
    }
}

</code>


Regards,
Satish Kumar
This was selected as the best answer
SnehalCSnehalC
Thanks for the reply.
I saw similar solution here: https://developer.salesforce.com/forums/ForumsMain?id=906F00000008zdWIAQ
Will this work?
Satish_SFDCSatish_SFDC
Yes this should work .
You can give it a try.

Regards,
Satish Kumar
Manuel Bustos 9Manuel Bustos 9
I have a similar problem but my trigger has errors
I have custom Object Propuesta__c with two lookup fields, Opportunity__c and Account__c
I want that Account__c gets populated with the AccountId related in the Opportunity__c

I tried the following:
trigger propuesta_cuenta on Propuesta__c(before insert, before update){
    for(Propuesta__c prop : Trigger.new){
        if(prop.Opportunity__c !=null){
            prop.Account__c = prop.Opportunity__c.AccountId;
        }
    }
}

but get error: Error: Compile Error: A non foreign key field cannot be referenced in a path expression: Opportunity__c at line 4 column 36

Any help?
thanks