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 

Help with Insert Trigger that does lookup in another table (object)

I have written a trigger (ListingAgent) trying to match fields from different objects, Opportunity and Agent. On the Opportunity page if the Listing Agent ID from LR (Listing_Agent_ID_From_LR__c) has a ID that matches an Agent ID from the Agent Object , I want the Agent Name (Name) to be populated in the Listing Agent Field. I get 87% coverage but an error for the last line. Does someone see a problem with the code? I can not see it. Please help. Thank you.
trigger Listingagent on Opportunity (before insert) {
    Set<String> lagid=new set<string>();
    for(opportunity o :Trigger.new){
        //if(o.listing_Agent_ID_From_LR__c != NULL)      
            lagid.add(o.Listing_Agent_ID_From_LR__c);
    }

    Map<String, String>matchid=new Map<String, String>();

    For(Agent__c ag: [select Agent_ID__c,
               Name
               from Agent__c 
               where Agent_ID__c 
                IN: lagid]){
        matchid.put(ag.Agent_ID__c, ag.Name);
    }
    For(Opportunity o : Trigger.new){
        if(matchid.containskey(o.Listing_Agent_ID_From_LR__c))
            o.Listing_Agent__c = matchid.get(o.Listing_Agent_ID_From_LR__c);
    }
}


 
Best Answer chosen by Dorel4
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

The only reason that I can imagine to explain why this line is not getting covered is that your query [SELECT Id, Agent_ID__c FROM Agent__c WHERE Agent_ID__c IN: lagid] has no results.

What kind of information is stored in the Agent_ID__c and Listing_Agent_ID_From_LR__c fields? Are they lookup fields?

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
 

All Answers

Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

You are putting in your map the Agent's name field when you should put its Ids, try this instead:
 
trigger Listingagent on Opportunity (before insert) {
    Set<String> lagid=new Set<string>();
	
    for(opportunity o :Trigger.new){
        //if(o.listing_Agent_ID_From_LR__c != NULL)      
            lagid.add(o.Listing_Agent_ID_From_LR__c);
    }

    Map<String, Id> matchid =new Map<String, Id>();

   for (Agent__c ag: [SELECT 	Id,
								Agent_ID__c
					FROM Agent__c 
					WHERE Agent_ID__c 
					IN: lagid]){
        matchid.put(ag.Agent_ID__c, ag.Id);
    }
    
	for (Opportunity o : Trigger.new) {
        if(matchid.containskey(o.Listing_Agent_ID_From_LR__c))
            o.Listing_Agent__c = matchid.get(o.Listing_Agent_ID_From_LR__c);
    }
}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
I've assumed that is a lookup relationship. Am I right?
Brenda S FinnBrenda S Finn
What error are you getting for the last line?
William LópezWilliam López

Hello, as far I can see logic seems to be right.

If I understood  right the issue its the conditional:

 if(matchid.containskey(o.Listing_Agent_ID_From_LR__c))

That means or the map if empty (there is any agent on the database) or there is an issue matching the IDs. I will say start by checking that your test method creates an agent before the opportunity creation and it's has the same ID in the opportunity.

If you share your unit test code where you get to 87 I might be able to able to help you more.

Let me know how it goes.
 
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

I am getting no error as I did not tested and cannot do it as I don't have the same environment as you.

Have you tried the changes that I suggested you? If so, are you still getting an error? What's the error message?

Regards.

​Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Dorel4Dorel4
I am sorry, I do not get a problem m.essage, the last line of the tigger is not covered in the trigger. 
Brenda S FinnBrenda S Finn
Then I think Zuinglio Lopes Ribeiro Júnior has answered your question. Look at his post about the key you use for your map. It should be Agent's Id, and not the Name as he stated above.
Dorel4Dorel4
Zuinglio Lopes Ribeiro Júnior, I tried the code but now I do not get coverage on the matchid.put(ag Agent_ID__c, ag ID) line.  I am confused if I want to have the name imported in the Listing Agent Field. why wouldn't I have the Name in the table?  I do want to match the ID's. 
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

The only reason that I can imagine to explain why this line is not getting covered is that your query [SELECT Id, Agent_ID__c FROM Agent__c WHERE Agent_ID__c IN: lagid] has no results.

What kind of information is stored in the Agent_ID__c and Listing_Agent_ID_From_LR__c fields? Are they lookup fields?

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
 
This was selected as the best answer
William LópezWilliam López
Hello,

As far I can see as you need to insert an agent a part of your test class with the same listing agent Id of the opportunity. you shouldn't use (its possible but not recommend use existing data in the database).

But if you share unit test class we might able to help you further.

If Listing_Agent__c is a lookup you need to save the Salesforce id as Zuinglio suggested . If it's a Text field, Name will be ok. (I will suggest use lookup, it will be better design as you can relate data, but the name might be enough for what you need)

Please let me know hoe it goes.

Bill
Dorel4Dorel4
All of the fields are text fields.  The Agent Name and Agent ID are in the Salesforce.  The Listing Agent ID From LR is imported from the website. I want to match the Listing Agent ID from LR to the Agent ID and have the name imported intothe Listing Agent Field.  Thank you for all the help.  This is the first trigger I ever created and I do not know where to start to solve the problem
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

That was very clarifying, so I would say that your code is correct. Please share with us your unit test class as Bill suggested.

Regards.


Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.