I am hoping someone can guide me. I have created this trigger on a custom object called Trident Contract. It pulled the agent name from the custom object Agent using the Agent Id. Now I need to add the Buyer Agent Office to the trigger. The Buyer Agent Lookup Field was working until I tried to also add the office. Here is my first trigger. Please advise how to add another field lookup. Thank you

trigger BuyAgent on Trident_Contract__c (before insert, before update) {
    Set <String> AgentIDs = new Set <String>();
    for(Trident_Contract__c tc:trigger.new) {
        if(tc.Buyer_Agent_ID__c !=NULL){
    //Match Agent ID to Buyer Agent ID
    //Map ID 

    Map<String, Agent__c> Agents = new map <String, Agent__C>();

    for (Agent__c obj: [
        SELECT ID, Agent_ID__c
        FROM Agent__c
        WHERE Agent_ID__c IN :AgentIDs]) {
    for(Trident_Contract__c tc : trigger.new) {
        if (Agents.containsKey(tc.Buyer_Agent_ID__c))
            tc.Buyer_Agent_Lookup__c = Agents.get(tc.Buyer_Agent_ID__c).ID;

I updated the trigger to the following

trigger BuyAgent on Trident_Contract__c (before insert) {
    Set<String> lagid=new set<string>();
    for(Trident_Contract__c tc :Trigger.new){
    Map<String, String>matchid=new Map<String, String>();

    for(Agent__c ag: [
        SELECT Agent_ID__c, Agent_Office__c
        FROM Agent__c 
        WHERE Agent_ID__c IN :lagid]) {
        matchid.put(ag.Agent_ID__c, ag.Agent_Office__c);
    for(Trident_Contract__c tc : Trigger.new) {
            tc.Buyer_Agent_lookup__c = matchid.get(tc.Buyer_Agent_Lookup__c);
            tc.Buyer_Agent_Office__c = matchid.get(tc.Buyer_Agent_Office__c);

But it does not update the Buyer Office Field

  • What did you do to try to add office? It does not look like your code sample makes any reference to it. – Adrian Larson Feb 13 '15 at 18:13
  • Adrian, I have added the code but it does not work that is why I was asking how to update it – Dorel Nasso Feb 13 '15 at 19:13

The keys to your (updated trigger) map matchId are only values of ag.agent_id__c from the SOQL

As such, the line

tc.Buyer_Agent_Office__c = matchid.get(tc.Buyer_Agent_Office__c);

will never find any entries in matchId unless the Buyer_agent_office__c is the same as the buyer_agent_id__c


I updated the code to the following and get an error message 'Line 1 Map must have exactly 2 arguments'

trigger BuyAgent on Trident_Contract__c (before insert, before update) { Set AgentIDs = new Set (); for(Trident_Contract__c tc:trigger.new){ if(tc.Buyer_Agent_ID__c !=NULL){ AgentIDs.add(tc.Buyer_Agent_ID__c,tc,Buyer_Agent_Office__C);
} } //Match Agent ID to Buyer Agent ID //Map ID

Map Agents = new map ();

for (Agent__c obj: [SELECT ID, Agent_ID__c
                    from Agent__c
                    Where Agent_ID__c 
                    IN: AgentIDs]){Agents.put(obj.Agent_ID__c,obj);}

for(Trident_Contract__c tc : trigger.new) {if (Agents.containsKey(tc.Buyer_Agent_ID__c,tc.Buyer_Agent_Office__C)) tc.Buyer_Agent_Lookup__c = Agents.get(tc.Buyer_Agent_ID__c).ID; tc.Buyer_Agent_Office__c = matchid.get(tc.Buyer_Agent_Office__c); } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.