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
Dorel4Dorel4 

Trigger for cross object lookup

Please help.  I am creating my first trigger called "List Agent".  I would like to take the"Lead" field 'Listing Agent ID LR Source' to look up in the Custom Object "Agent" to see if there is a match to the "Agent" field 'Agent ID' If it does match, I would like to take the information from the "Agent" field 'Agent Name' and insert it in the "Lead" Field 'Listing Name'.  I can not get the trigger to save and I am not sure what I am doing wrong.

My fields 
Lead Fields
Listing Agent = Listing_Agent__c
Listing Agent ID LR Source = Listing_Agent_ID_LR_Source__c 

Agent Fields
Agent Name = Name
Agent ID = Agent_ID__c

I wrote the trigger two different way but either will work. Please let me know what I am doing wrong.  Thank you.

Trigger One
trigger ListingAgent on Lead (before insert) {
 List<String> listingagentid=new list<string>();
    Map<string,Leads>mapvalues= new Map<String,Agent>()
        ;Listing_Agent_ID_LR_Source__c sobjTrigger.new{
            Listing_Agent_ID_LR_Source__c.add(sobj.AgentID); //compare Listing Agent LR ID Source and Agent ID 
        }
    List<Agent__c> agentvalues = [select id,AgentName from Agent__c where AgentID IN: Listing_Agent_ID_LR_Source__c];
    for(Lead sobjs : Trigger.new){
        for(Agent__c agc: agent Values){
            if(agc.Agent_ID__c == sobjs.Listing_Agent_ID_LR_Source__c){
                fieldListing_Agent= agc.Agent_Name__c
            }
        }
    }
}



This is my second trigger I wrote.

trigger ListingAgent on Lead (before insert) {
  List<string> listingAgent = new List<String>();
Map<String,Lead> mapValues = new Map<String,Lead>()
         for(Listing_Agent_ID_LR_Source__c sobj : Trigger.new){
               Listing_Agent_ID_LR_Source__c.add(sobj.AgentID); //Field name you want to compare
 
        }
List<agent__c> agentValues = [select id,Name from Agent__c where AgentId IN: Listing_Agent_ID_LR_Source__c];
 
   for(Lead : Trigger.new){
    (agent__c agc: agentValues){
       if(agc.Agent_Id__C == Listing_Agent_ID_LR_Source__c){
           Listing_Agent_c = agc.Agent_Name__c
        }
 
    }
}
}
 

Thank you 
KaranrajKaranraj
trigger ListingAgent on Lead (before insert) {
 List<String> listingagentid=new list<string>();
    Map<string,Leads>mapvalues= new Map<String,Agent>()
        for(Listing_Agent_ID_LR_Source__c sobj: Trigger.new){
            Listing_Agent_ID_LR_Source__c.add(sobj.AgentID); //compare Listing Agent LR ID Source and Agent ID 
        }
    List<Agent__c> agentvalues = [select id,AgentName from Agent__c where AgentID IN: Listing_Agent_ID_LR_Source__c];
    for(Lead sobjs : Trigger.new){
        for(Agent__c agc: agentvalues){
            if(agc.Agent_ID__c == sobjs.Listing_Agent_ID_LR_Source__c){
                sobjs.fieldListing_Agent= agc.Agent_Name__c
            }
        }
    }
}

Try the above code, make sure that you are using the variable name correctly. Check this trailhead to learn salesforce platform https://developer.salesforce.com/trailhead
Dorel4Dorel4
I entered the following code: 
trigger ListingAgent on Lead (before insert) {
 List<String> listingagentid=new list<string>();
    Map<string,Lead>mapvalues= new Map<String,Agent__c>();
        for(Listing_Agent_ID_LR_Source__c sobj: Trigger.new){
            Listing_Agent_ID_LR_Source__c.add(sobj.AgentID); //compare Listing Agent LR ID Source and Agent ID 
        }
    List<Agent__c> agentvalues = [select id,Name from Agent__c where AgentID IN: Listing_Agent_ID_LR_Source__c];
    for(Lead sobjs : Trigger.new){
        for(Agent__c agc: agentvalues){
            if(agc.Agent_ID__c == sobjs.Listing_Agent_ID_LR_Source__c){
                sobjs.fieldListing_Agent= agc.Agent_Name__c
            ;
        }
    }
}
    }

I try to save and I get a problem for line 4 - Invalid Type:listing_agent_ID-LR-Source__c

Please advise what I am doing wrong.  Thank you
KaranrajKaranraj
trigger ListingAgent on Lead (before insert) {
 List<String> listingagentid=new list<string>();
    Map<string,Leads>mapvalues= new Map<String,Agent>()
        for(Lead sobj: Trigger.new){
            Listing_Agent_ID_LR_Source__c.add(sobj.AgentID); //compare Listing Agent LR ID Source and Agent ID 
        }
    List<Agent__c> agentvalues = [select id,AgentName from Agent__c where AgentID IN: Listing_Agent_ID_LR_Source__c];
    for(Lead sobjs : Trigger.new){
        for(Agent__c agc: agentvalues){
            if(agc.Agent_ID__c == sobjs.Listing_Agent_ID_LR_Source__c){
                sobjs.fieldListing_Agent= agc.Agent_Name__c
            }
        }
    }
}

Try the updated code and let know, if you are still facing any issue