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
Claire NicolayClaire Nicolay 

Auto populate two fields based on value selected in lookup field

Dear all

I have an object called "relationships" in which I can select via a lookup field named "in_relation_with" the name of an existing contact to show up as the relationship.
Once the contact has been selected and shows up in the field named"in_relation_with", I would like the two fields called:"phone" and "email" on my relationship object to autpopulate using the corresponding data captured in the fields "phone" and "email" on the contact tab.
To my knowledge, this is a dynamic process and cannot be automated by point and click?
Can anybdy help me solve this ?
Thanks
Best Answer chosen by Claire Nicolay
EldonEldon
Hi claire use the below trigger,
 
trigger pocon on relationships__c (after insert,after update) {
    if(checkRecursive.runOnce())    {
        list<id> PobId= new list<id>();
        for(relationships__c p : trigger.new){
            if(p.in_relation_with__c!=null){
                PobId.add(p.id);
            }
        }
        list<relationships__c > pobj= [select name,in_relation_with__c,in_relation_with__r.email from relationships__c  where id in :pobId];
        for(relationships__c p : pobj){
            if(p.in_relation_with__c!=null){
                p.email__c = p.in_relation_with__r.email;
            }
        }
        update pobj;
    }
}

Also create the following class which is called to avoid calling teh trigger more than once.
public class checkrecursive {
    private static boolean run = true;
    public static boolean runOnce(){
        if(run){
            run=false;
            return true;
        }else{
            return run;
        }
    }
}

Let me know if you have any issues.

Mark it as best answer if it works.

Regards

All Answers

EldonEldon
Hi claire use the below trigger,
 
trigger pocon on relationships__c (after insert,after update) {
    if(checkRecursive.runOnce())    {
        list<id> PobId= new list<id>();
        for(relationships__c p : trigger.new){
            if(p.in_relation_with__c!=null){
                PobId.add(p.id);
            }
        }
        list<relationships__c > pobj= [select name,in_relation_with__c,in_relation_with__r.email from relationships__c  where id in :pobId];
        for(relationships__c p : pobj){
            if(p.in_relation_with__c!=null){
                p.email__c = p.in_relation_with__r.email;
            }
        }
        update pobj;
    }
}

Also create the following class which is called to avoid calling teh trigger more than once.
public class checkrecursive {
    private static boolean run = true;
    public static boolean runOnce(){
        if(run){
            run=false;
            return true;
        }else{
            return run;
        }
    }
}

Let me know if you have any issues.

Mark it as best answer if it works.

Regards
This was selected as the best answer
Claire NicolayClaire Nicolay
thanks a lot!