You need to sign in to do that
Don't have an account?
Dorel4
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);
}
}
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);
}
}
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
You are putting in your map the Agent's name field when you should put its Ids, try this instead:
Regards.
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
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.
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.
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.
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
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.