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
PFangPFang 

Test Class for trigger matching Opportunity's Agent ID (Custom Object ID) with Account's Agent ID

Hi,

 

Need help with getting the test class to 100% as I'm having trouble with a couple of lines which may help with future testing.

 

1.) Trigger on Opportunity

 

trigger SetAgent on Opportunity (before insert, before update) { 
List<String> accountIds = new List<String>();
List<Account> parentAccounts = new List<Account>();
for(Opportunity oppty : Trigger.new)
{
accountIds.add(oppty.AccountId);
}
 parentAccounts = [Select Id, Agent__c from Account where Id in :accountIds];
    Map<String,Account> accountMap = new Map<String,Account>();
     for(Account a : parentAccounts)
    {
        accountMap.put(a.Id,a);
    }
   for(Opportunity oppty : Trigger.new)
    {
        Account parentAccount = accountMap.get(oppty.AccountId);
        if(parentAccount != null)
        {
            oppty.Agent__c = parentAccount.Agent__c;
        }
    }
}

 2.) Apex Test Class 

 

@isTest
private class SetAgentTest {
    static testMethod void SetAgent() {
        Account a = new Account(Id = '001K000000KnlcE', Agent__c = 'a03K0000001A7v2');        
        Opportunity o = new Opportunity(Name = 'TestOpportunity', StageName = 'Prospecting', CloseDate = system.Today(), Agent__c = a.Agent__c);
        insert o;
        System.assertEquals(a.Agent__c, o.Agent__c);
    }
}

 3.) The 83% result tells me that these two lines from the code above were not tested, a brief explanation of why would be much appreciated.

 

accountMap.put(a.Id,a);

 

oppty.Agent__c = parentAccount.Agent__c;

 Thanks again!

Best Answer chosen by Admin (Salesforce Developers) 
Hengky IlawanHengky Ilawan

Hi,

 

In your test class, i think it is ideally not to assign the opportunity agent explicitly, because when the opportunity is inserted, the agent will be copied over from account by the trigger, and that is exactly the thing you want to test in the apex test class.

So if the assertion passed then your trigger should be working as expected.

 

Btw, you can assign query result to a map directly:

 

Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Agent__c from Account where Id in :accountIds]);

 

Regards,

Hengky 

All Answers

DuTomDuTom
 Account a = new Account(Id = '001K000000KnlcE', Agent__c = 'a03K0000001A7v2');   
insert a;
Opportunity o = new Opportunity(Name = 'TestOpportunity', AccountId=a.id, StageName = 'Prospecting', CloseDate = system.Today(), Agent__c = a.Agent__c);
insert o;

 insert your acc,  and add to opp.

Hengky IlawanHengky Ilawan

Hi,

 

In your test class, i think it is ideally not to assign the opportunity agent explicitly, because when the opportunity is inserted, the agent will be copied over from account by the trigger, and that is exactly the thing you want to test in the apex test class.

So if the assertion passed then your trigger should be working as expected.

 

Btw, you can assign query result to a map directly:

 

Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Agent__c from Account where Id in :accountIds]);

 

Regards,

Hengky 

This was selected as the best answer
PFangPFang

Tried yours DuTom, but ended up with 0%

 

@isTest
private class SetAgentTest {
    static testMethod void SetAgent() {
         Account a = new Account(Id = '001K000000KnlcE', Agent__c = 'a03K0000001A7v2');   
         insert a;
         Opportunity o = new Opportunity(Name = 'TestOpportunity', AccountId=a.id, StageName = 'Prospecting', CloseDate = system.Today(), Agent__c = a.Agent__c);
         insert o;
    }
}

 Appreciate your insight though!

PFangPFang

Hengky, 

 

You're a coding wizardl!

 

Revising my trigger to this one allowed my simple test class to reach 100%, which also goes for you, sheer greatness!

 

@isTest
private class SetAgentTest {
    static testMethod void SetAgent() {
         Account a = new Account(Name = 'devtest', Agent__c = 'devtest');   
         insert a;
         Opportunity o = new Opportunity(Name = 'TestOpportunity', AccountId = a.id, StageName = 'Prospecting', CloseDate = system.Today());
         insert o;
    }
}

 

Regards,

 

PFang