You need to sign in to do that
Don't have an account?

Populating A Lookup Field To A Custom Object Via Apex Trigger
I've got a number of lookup fields that I need to populate based on an ID field that will be passed over from our source system. The custom object is Agent__c.
I've been able to get to 83% coverage (which I know is fine - but we're going to have hundreds of leads processing via this every day - and I want to make sure it won't fail).
Any help/advice would be hugely appreciated.
Here's my trigger:
trigger AgentAssignmentLead on Lead (before insert, before update) {
Set <String> AgentIDs = new Set <String>();
for (Lead l : trigger.new) {
if (l.Agent_ID__c !=NULL) {
AgentIDs.add (l.Agent_ID__c );
}
}
//now we have our list of agent IDs that we want to check against.
//We will build a map of the "match" field
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 (Lead l : trigger.new) {
if (l.Agent_ID__c !=NULL){
if (Agents.containsKey(l.Agent_ID__c))
l.Agent__c=Agents.get(l.Agent_ID__c).ID;
}
}
}
And my test class:
@istest
public with sharing class AgentLeadTest {
public static void UpdateAgent()
{
//create the new lead
Lead Lead = new Lead ();
Lead.LastName='Adrian';
Lead.FirstName='Tooms';
Lead.status='Unread';
Lead.Metro__c='New Jersey';
Lead.Agent_ID__c='100683628';
test.startTest();
insert Lead;
}
}
I've been able to get to 83% coverage (which I know is fine - but we're going to have hundreds of leads processing via this every day - and I want to make sure it won't fail).
Any help/advice would be hugely appreciated.
Here's my trigger:
trigger AgentAssignmentLead on Lead (before insert, before update) {
Set <String> AgentIDs = new Set <String>();
for (Lead l : trigger.new) {
if (l.Agent_ID__c !=NULL) {
AgentIDs.add (l.Agent_ID__c );
}
}
//now we have our list of agent IDs that we want to check against.
//We will build a map of the "match" field
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 (Lead l : trigger.new) {
if (l.Agent_ID__c !=NULL){
if (Agents.containsKey(l.Agent_ID__c))
l.Agent__c=Agents.get(l.Agent_ID__c).ID;
}
}
}
And my test class:
@istest
public with sharing class AgentLeadTest {
public static void UpdateAgent()
{
//create the new lead
Lead Lead = new Lead ();
Lead.LastName='Adrian';
Lead.FirstName='Tooms';
Lead.status='Unread';
Lead.Metro__c='New Jersey';
Lead.Agent_ID__c='100683628';
test.startTest();
insert Lead;
}
}
All Answers
You can add an Agent to have at least one record when your lead be inserted. It always recommended to assert your results. Try the code below (didn't tested):
Regards.
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Regards.
Agents.put(obj.Agent_ID__c, obj);
l.Agent__c=Agents.get(l.Agent_ID__c).ID;