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 

Update a trigger to add another field to be updated

I have created a trigger on a custom object called Trident Contract. It pulled the agent name from the custom object Agent using the Agent Id in the field Buyer Agent Lookup (it is a lookup field). Now I need to add the Buyer Agent Office with a look up from the Agent object mapping with   the Agent Id to the trigger. The Buyer Agent Lookup Field trigger was working until I tried to add the Buyer Agent Office.
Here is my originial trigger. Please advise how to add another field lookup for Buyer Agent Office. 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){
            AgentIDs.add(tc.Buyer_Agent_ID__c);
        } 
    }
    //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]){                            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_Lookup__c = Agents.get(tc.Buyer_Agent_ID__c).ID;
 }
}
Best Answer chosen by Dorel4
Dorel4Dorel4
The trigger is working but will not deploy.  This is the code that finally worked.

 trigger AgentInfo on Trident_Contract__c (before insert, before update) {
  
Set<String> BuyAGIDs = new Set<String>();
  for(Trident_Contract__c tc : trigger.new){
      if(tc.Buyer_Agent_ID__c  !=NULL){BuyAGIDs.add(tc.Buyer_Agent_ID__c);

Map <String, Agent__c> BuyAG = new Map <String, Agent__c>();
  for (Agent__c obj: [SELECT ID, Agent_ID__c, Agent_Office__c
                        from Agent__c
                        Where Agent_ID__c IN: BuyAGIDs]){BuyAG.put(obj.Agent_ID__c,obj);}

for(Trident_Contract__c t: trigger.new)
{
{if (BuyAG.containsKey(tc.Buyer_Agent_ID__c))  tc.Buyer_Agent_Lookup__c=BuyAG.get(tc.Buyer_Agent_ID__c).ID;
{if (BuyAG.containsKey(tc.Buyer_Agent_ID__c)) tc.Buyer_Agent_Office__c=BuyAG.get(tc.Buyer_Agent_ID__c).Agent_Office__c;}
}
      }
                                              
           }
  }
}

 

All Answers

SonamSonam (Salesforce Developers) 
Can you please specific where the  Buyer Agent Office field is present? Is it in the Agent object  and you are trying to get it in the trigger using SOQL?
Dorel4Dorel4
The Agent Object has a field called Office and I want to get that information from there and put it in the Trident Contract Object field Buyer Agent Office
SonamSonam (Salesforce Developers) 
okay, so i've added the  Office field in the SOQL and am assigning the value of this field to the  Buyer Agent Office in Trident Contract Object, pls try this and let me know if this works


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){
            AgentIDs.add(tc.Buyer_Agent_ID__c);
        } 
    }

 Map<String, Agent__c> Agents = new map <String, Agent__C>();
    
    for (Agent__c obj: [SELECT ID, Agent_ID__c, Office__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_Lookup__c = Agents.get(tc.Buyer_Agent_ID__c).ID;
tc.Buyer_Agent_Office__c=Agents.get(tc.Buyer_Agent_ID__c).Office__c;
 }
}
Dorel4Dorel4
The code works but I am having issues deploying it. 

trigger BuyAgentOffice on Trident_Contract__c (before update) {
Set <String> AgentIDs = new Set <String>();
    for(Trident_Contract__c tc:trigger.new){
        if(tc.Buyer_Agent_ID__c !=NULL){
            AgentIDs.add(tc.Buyer_Agent_ID__c);
        } 
    }

 Map<String, Agent__c> Agents = new map <String, Agent__C>();
    
    for (Agent__c obj: [SELECT ID, Agent_ID__c, Agent_Office__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_Lookup__c = Agents.get(tc.Buyer_Agent_ID__c).ID;
tc.Buyer_Agent_Office__c=Agents.get(tc.Buyer_Agent_ID__c).Agent_Office__c;
 }
}

here is the test
@isTest 
private class TridentContract {
    static testMethod void UpdateAgent() {
       Trident_Contract__c t = new Trident_Contract__c
           (Name='test', Buyer_Agent_ID__c='123456',
                                                       Buyer_Agent_Office__c='a02q0000002rils', Seller_Agent_Office__c= 'a02q0000002rils');

       // Insert contract
       insert t;
    
                 }
}

The trigger passes but when I try to deploy I get two error messages 
TestSellAgentTrident UpdateAgent System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, BuyAgent: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.BuyAgent: line 18, column 1: [] 
Stack Trace: Class.TestSellAgentTrident.UpdateAgent: line 7, column 1
TridentContract UpdateAgent System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, BuyAgent: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.BuyAgent: line 18, column 1: [] 
Stack Trace: Class.TridentContract.UpdateAgent: line 7, column 1

Can anyone help I am not sure where to start.  

I went to the Buy Agent Trigger line 18 - tc.Buyer_Agent_Office__c=Agents.get(tc.Buyer_Agent_ID__c).Agent_Office__c; 
Can someone explain the meaning of the error so I can understand how to fix it.

Thank you
 
SonamSonam (Salesforce Developers) 
Is the trigger updating the correct values in the ID and Buyer agent office field?please check.

Also, change the last lines to this:

{if (Agents.containsKey(tc.Buyer_Agent_ID__c))
                tc.Buyer_Agent_Lookup__c = Agents.get(tc.Buyer_Agent_ID__c).ID;
tc.Buyer_Agent_Office__c=Agents.get(tc.Buyer_Agent_ID__c).Agent_Office__c;

TO:

{if (Agents.containsKey(tc.Buyer_Agent_ID__c))
                tc.Buyer_Agent_Lookup__c = Agents.get(tc.Buyer_Agent_ID__c).ID;
if(Agents.get(tc.Buyer_Agent_ID__c).Agent_Office__c!=null)//check its value in case its NULL
tc.Buyer_Agent_Office__c=Agents.get(tc.Buyer_Agent_ID__c).Agent_Office__c;
Dorel4Dorel4
Both Triggers update the correct fields, do you think it could be my test class?  This is what I have for the test;  Thank you for your help

@isTest 
private class TridentContract {
    static testMethod void insertAgent() {
       Trident_Contract__c t = new Trident_Contract__c();
           t.Name ='test'; 
           t.Buyer_Agent_ID__c ='1234';
           t.Buyer_Agent_Office__c ='a02q0000002rils';
           t.Seller_Agent_ID__c ='1234';
           t.Seller_Agent_Office__c ='a02q0000002rils';
             
       // Insert contract
       insert t;
    
                 }
}
Dorel4Dorel4
I have updated the trigger and the test.  If I remove the Office I get 100% coverage and can deploy.  Any idea how I can fix this

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)
        {
            AgentIDs.add(tc.Buyer_Agent_ID__c);
        }
    }
    Map <String, Agent__c> Agents = new Map <String, Agent__c>();
    for (Agent__c obj: [SELECT ID, Agent_ID__c, Agent_Office__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_Lookup__c = Agents.get(tc.Buyer_Agent_ID__c).ID;

 //tc.Buyer_Agent_Office__c = Agents.get(tc.Buyer_Agent_ID__c).Agent_Office__c;
             
    }
}

@isTest 
private class TridentContract {
    static testmethod void testcontracts()
    {
        Trident_Contract__c contract1 = new Trident_Contract__c(Name='1234568723423', Buyer_Agent_ID__c='1234', Buyer_Agent_Branch__c='a02q0000002rils', Seller_Agent_ID__c='1234', Seller_Agent_Branch__c='a02q0000002rils');

        test.startTest();
        
        insert contract1;


                 }
}
SonamSonam (Salesforce Developers) 
You should be creating an agent in the test class and then creating and inserting a Trident_Contract__c without the values Agent ID and Office sucht at when you insert it - the trigger will automatically pick the values from the the agent table.
This will prove that the assignment given in the trigger is working..
Dorel4Dorel4
The trigger is working but will not deploy.  This is the code that finally worked.

 trigger AgentInfo on Trident_Contract__c (before insert, before update) {
  
Set<String> BuyAGIDs = new Set<String>();
  for(Trident_Contract__c tc : trigger.new){
      if(tc.Buyer_Agent_ID__c  !=NULL){BuyAGIDs.add(tc.Buyer_Agent_ID__c);

Map <String, Agent__c> BuyAG = new Map <String, Agent__c>();
  for (Agent__c obj: [SELECT ID, Agent_ID__c, Agent_Office__c
                        from Agent__c
                        Where Agent_ID__c IN: BuyAGIDs]){BuyAG.put(obj.Agent_ID__c,obj);}

for(Trident_Contract__c t: trigger.new)
{
{if (BuyAG.containsKey(tc.Buyer_Agent_ID__c))  tc.Buyer_Agent_Lookup__c=BuyAG.get(tc.Buyer_Agent_ID__c).ID;
{if (BuyAG.containsKey(tc.Buyer_Agent_ID__c)) tc.Buyer_Agent_Office__c=BuyAG.get(tc.Buyer_Agent_ID__c).Agent_Office__c;}
}
      }
                                              
           }
  }
}

 
This was selected as the best answer