0

I have a trigger that maps information from one Custom Object (Agents) to another Custom Object (Trident Contracts). I am able to update Seller Agent Name in the Trident Contract Mapping with the ID. see code below

trigger SellAgent on Trident_Contract__c (before insert, before update) {
    Set <String> AgentID = new Set <String>();
    for (Trident_Contract__c tc : trigger.new) {
        if (tc.Seller_Agent_ID__c != NULL) {
            AgentID.add(tc.Seller_Agent_ID__c);
        }
    }
    Map<String, Agent__c> Agent = new map <String, Agent__C>();
    for (Agent__c obj : [SELECT ID, Agent_ID__c,
                         from Agent__c
                         Where Agent_ID__c
                         IN: AgentID]) {
        Agent.put(obj.Agent_ID__c, obj);
    }
    for (Trident_Contract__c tc : trigger.new) {
        if (Agent.containsKey(tc.Seller_Agent_ID__c)) tc.Seller_Agent_Lookup__c = Agent.get(tc.Seller_Agent_ID__c).ID;
    }
}

Now I want to add to this trigger and update another field on the Trident Contract (Seller Agent Office)using a map from the Agent (office) field. But I get this error message when I run the test

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AgentInfo: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de - reference a null object Trigger.AgentInfo: line 20, column 1: []

Here is my updated code and Test. Please let me know what I am doing wrong.

trigger SellAgent on Trident_Contract__c (before insert, before update) {
    Set <String> AgentID = new Set <String>();
    for (Trident_Contract__c tc : trigger.new) {
        if (tc.Seller_Agent_ID__c != NULL) {
            AgentID.add(tc.Seller_Agent_ID__c);
        }
    }
    Map<String, Agent__c> Agent = 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: AgentID]) {
        Agent.put(obj.Agent_ID__c, obj);
    }
    for (Trident_Contract__c tc : trigger.new) {
        if (Agent.containsKey(tc.Seller_Agent_ID__c)) tc.Seller_Agent_Lookup__c = Agent.get(tc.Seller_Agent_ID__c).ID;
        tc.Seller_Agent_Office__c = Agent.get(tc.Seller_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;


    }
}

Thank you for any suggestions. The trigger works in the Sandbox but it will not pass the test or deploy.

  • 1
    Don't you need an agent__c record in your test? You're querying for one and its not going to find one. – Phil B Feb 19 '15 at 20:31
  • 2
    The error message says the problem is on trigger AgentInfo yet you posted trigger SellAgent - do you have some other trigger in PROD called AgentInfo that perhaps doesn't exist in sandbox? – cropredy Feb 20 '15 at 19:42
  • Not to be a critic here but did you check Flows and Process builder. I literally got rid of several thousands lines of apex and additional overhead of test classes with Flows and Process builder. In your case, you might want to use process builder. Just a thought... – user477 Jun 25 '15 at 22:19
  • I'm glad it seems you got this to work, but I would recommend you take another look at your test method. Looking at the above it is very basic, doesn't include testing batches, but even more concerning, it doesn't contain any assert statements. How can you be sure the trigger is doing what it is supposed to going forward? Remember to aim for functional testing, not just 75%! :) – Christopher Alun Lewis Jan 2 '16 at 14:47
0

Here is the final code that worked

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

        Map <String, Agent__c> SellAG = 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: SellAGIDs]){SellAG.put(obj.Agent_ID__c,obj);}

        for(Trident_Contract__c t: trigger.new)
        {
            {
                if (SellAG.containsKey(tc.Seller_Agent_ID__c))  tc.Seller_Agent_Lookup__c=SellAG.get(tc.Seller_Agent_ID__c).ID;
                {
                    if (SellAG.containsKey(tc.Seller_Agent_ID__c)) tc.Seller_Agent_Office__c=SellAG.get(tc.Seller_Agent_ID__c).Agent_Office__c;}
                }
|improve this answer|||||
  • Awesome, Glad it works but when you declare a map you should do it as a Map<ID, Foo> strings go both ways but its easier for the next developer to read if you do it that way – EricSSH Feb 3 '16 at 5:39
  • If you ever return to SFSE, can you please accept this answer if it resolved your issue? – Adrian Larson May 2 '17 at 18:33

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.