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
Robert Goldberg 9Robert Goldberg 9 

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;
    }

}
Best Answer chosen by Robert Goldberg 9
Robert Goldberg 9Robert Goldberg 9
I've been able to take care of this by moving some lines around.  Thanks!

All Answers

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

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): 
 
public class AgentLeadTest {
    
	static testMethod void UpdateAgent()
    {
        		
		Agent__c agent = new Agent( Agent_ID__c='100683628' );
		Database.SaveResult saveResultAgent = Database.insert(agent);
		
        Lead lead = new Lead ();
        lead.LastName='Adrian';
        lead.FirstName='Tooms';
        lead.Status='Unread';
        lead.Metro__c='New Jersey';
        lead.Agent_ID__c='100683628';
		
		// Initially is null
		System.assert(lead.Agent__c == null);
        
		Test.startTest();
		
		Database.SaveResult saveResultLead = Database.insert(lead);
			lead = [SELECT Agent__c FROM Lead WHERE Id = :saveResultLead.getId()];
			
			// Check if it was corretly setted
			System.assert(lead.Agent__c = saveResultAgent.getId());
			
		Test.stopTest();
		
    }
}
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
Correct this line:
 
System.assert(lead.Agent__c == saveResultAgent.getId());

 
Robert Goldberg 9Robert Goldberg 9
Zuinglio - thanks very much, but this made absolutely no difference in terms of coverage.
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Which lines are not getting covered?

Regards.
Robert Goldberg 9Robert Goldberg 9
There are 2 uncovered lines:

Agents.put(obj.Agent_ID__c, obj);

l.Agent__c=Agents.get(l.Agent_ID__c).ID;
Robert Goldberg 9Robert Goldberg 9
I've been able to take care of this by moving some lines around.  Thanks!
This was selected as the best answer