Skip to main content
Robert Goldberg 님이 #Apex에 질문했습니다
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;

    }

}
답변 6개
  1. 2015년 2월 20일 오후 4:43
    I've been able to take care of this by moving some lines around.  Thanks!
0/9000