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
KitagawaSan85KitagawaSan85 

Test Class for Trigger

Hello, 

 

I have a custom object that is a child to the opportunity object. There is a trigger on that custom object that updates a field on the opportunity if a checkbox is marked as true. 

 

The trigger appears to be working when I do manual testing, but the test class system.asserts are failing. 

 

I can't figure out why the trigger is not functioning properly in the testClass, but works correctly in the UI. 

 

Any help would be greatly appreciated!!! 

 

Trigger

trigger UpdateOpptyLeadBusCon on Business_Consultant__c (before update, before insert, before delete) {

//Get the Business Consultant & Opportunity records
set<id> opportunitiesid = new set<id>();

//Set trigger.new or trigger.old depending on if isUpdate / isInsert / isDelete
if(trigger.isUpdate || trigger.isInsert){
    for(Business_Consultant__c buscon: Trigger.New) {
        opportunitiesid.add(buscon.Opportunity__c);
        }
    }else{
    for(Business_Consultant__c buscon: Trigger.Old) {
        opportunitiesid.add(buscon.Opportunity__c);
        }
}

//Create the lists of opportunities        
    List<Opportunity> updatelst = new List<Opportunity>();
    List<Opportunity> lstopp =[select id, Lead_bus_consultant__c from Opportunity where id IN:opportunitiesid];

 
//If isInsert && lead_consultant__c = True, then set the opportunity lead_bus_consultant__c
if(trigger.isInsert){
    for(Business_Consultant__c buscon:Trigger.New) {
        if(buscon.Lead_Consultant__c == TRUE) {
            for(Opportunity opp:lstopp) {
                 if(opp.id == buscon.opportunity__c) {
                     opp.Lead_bus_consultant__c = buscon.consultant__c;
                      updatelst.add(opp);
                 }
            }
        }
    }
}

//If isUpdate check to see if it is changed
if(trigger.isUpdate){
    
    for(Business_Consultant__c buscon:Trigger.New){
    Business_Consultant__c oldBusCon = Trigger.oldMap.get(buscon.Id);
    //If the checkbox has changed position and is no longer true, clear out the opportunity.lead_bus_consultant__c field
    if(Trigger.isUpdate && buscon.Lead_Consultant__c != oldBusCon.Lead_Consultant__c  && buscon.Lead_Consultant__c != TRUE){
        for(Opportunity opp:lstopp){
            if(opp.id == buscon.opportunity__c){
                opp.Lead_bus_consultant__c = null;
                    updatelst.add(opp);
            }
        } 
    }else{
    //Otherwise if the checkbox is checked, set the opportunity.lead_bus_consultant__c field as the consultant from the connector
    if(buscon.Lead_Consultant__c == TRUE) {
 
        for(Opportunity opp:lstopp) {
                 if(opp.id == buscon.opportunity__c) {
 
                     opp.Lead_bus_consultant__c = buscon.consultant__c;
                      updatelst.add(opp);
                }
        }
    }
    }
    }
} 
//If isDelete set the opportunity.lead_bus_consultant__c to null 
else {
if(trigger.isDelete){

    for(Business_Consultant__c buscon:Trigger.Old){
        Business_Consultant__c oldBusCon = Trigger.oldMap.get(buscon.Id);
        if(Trigger.isDelete && buscon.Lead_Consultant__c == TRUE){
            for(Opportunity opp:lstopp){
                if(opp.id == buscon.opportunity__c){
                    opp.Lead_bus_consultant__c = null;
                        updatelst.add(opp);
                }
            } 
        } 
    } 
} 
}
//Update the list of opportnities
update updatelst;
}

 

 

 

Test Class

public class testUpdateOpptyLeadBusCon {
    static testMethod void testUpdateOpptyLeadBusCon() {

//Create test profile 
Profile p = [select id from profile where name='System Administrator'];

//Create Users 
    User User1 = new User(alias = 'u1', email='u1@testorg.com',
      emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
      localesidkey='en_US', profileid = p.Id, country='United States',
      timezonesidkey='America/Los_Angeles', username='u1@testorg.com');
    insert User1;

    User User2 = new User(alias = 'u2', email='u2@testorg.com',
      emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
      localesidkey='en_US', profileid = p.Id, country='United States',
      timezonesidkey='America/Los_Angeles', username='u2@testorg.com');
    insert User2;

//Create Q-Scan Opportunity 
    Opportunity op = new Opportunity (owner=User1, name='Q-Scan Opportunity', Type='Q-Scan', stageName='Targeted Accounts', closeDate=system.today(), LeadSource='Vision');
    insert op;

//Test isInsert portion of code
//Create Business_Consultant__c 'BC1' (lead = false)
    Business_Consultant__c BC1 = new Business_Consultant__c(Consultant__c = User1.id, Opportunity__c = op.id, Lead_Consultant__c = FALSE); 
    insert BC1; 

//Create Business_Consultant__c 'BC2' (lead = true)
    Business_Consultant__c BC2 = new Business_Consultant__c(Consultant__c = User2.id, Opportunity__c = op.id, Lead_Consultant__c = TRUE); 
    insert BC2; 

    //Test that opportunity.Lead_bus_Consultant__c = User2
        //THIS FAILS
System.assertEquals(User2.id , op.Lead_Bus_Consultant__c); //Test isUpdate portion of code //Uncheck the lead designation on BC2 BC2.Lead_Consultant__c = False; update BC2; //Test that lead business consultant = null system.assertEquals(op.Lead_Bus_Consultant__c, null); //Set Lead_Bus_Consultant=TRUE on BC1 BC1.Lead_Consultant__c = True; update BC1; //Test that lead business consultant = User1 //THIS FAILS
system.assertEquals(op.Lead_Bus_Consultant__c, User1.id); //Delete BC1 delete BC1; //Test that lead business consultant = null system.assertEquals(op.Lead_Bus_Consultant__c, null); } }

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sivaextsivaext

Hi 

 

the trigger updates database records, not in- memory records. you need requery for opportunity object.

 

before writing system.assertEquals , write query opportunity

 

Opportunity op1 =  [select id, lead_bus_consulatant__c from where id = op.id];

 

system.assertEquals (User2.id, op1.lead_bus_consultant__c);

 

All Answers

sivaextsivaext

Hi 

 

the trigger updates database records, not in- memory records. you need requery for opportunity object.

 

before writing system.assertEquals , write query opportunity

 

Opportunity op1 =  [select id, lead_bus_consulatant__c from where id = op.id];

 

system.assertEquals (User2.id, op1.lead_bus_consultant__c);

 

This was selected as the best answer
KitagawaSan85KitagawaSan85

Ah! That was it! 

 

Thanks!!