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
ilewi121ilewi121 

How to write test query to confirm trigger creates custom object?

I have written a piece of code to create several custom objects when a lead converts. It works, but I don't know how to test the code. How do I write the queries to confirm the custom object was created automatically?

Here is the trigger:

trigger createModefromLead on Lead (after update) {

    List<Mode__c> UpdateModes = new List<Mode__c>();
    List<Mode__c> InsertModes = new List<Mode__c>();
    Set<Id> AccountIds = new Set<ID>();
   
    for (Lead l :Trigger.new){
        if(l.IsConverted){
            AccountIds.add(l.ConvertedAccountId);
        }
    }

List<Mode__c> ModeList = [SELECT Id, Account__c, Name, Mode_Type__c, Est_Shipping_Spend__c, Est_Shipping_Volume__c FROM Mode__c WHERE Account__c in :AccountIds];

    for (Lead l :Trigger.new){
        if(l.IsConverted){

   //Parcel Mode
            if(l.HasParcel__c == TRUE){
                Set<Id> ParcelModeCount = new Set<Id>();
                for (Mode__c m: ModeList){
                    if(m.Account__c == l.ConvertedAccountId && m.Mode_Type__c =='Parcel'){
                     m.Est_Shipping_Spend__c = l.Spend_Annual_Parcel__c;
                        m.Est_Shipping_Volume__c = l.Count_Daily_Parcel__c;
                        ParcelModeCount.add(m.Id);
                        UpdateModes.add(m);
                    }
                }
                if(ParcelModeCount.size() == 0){
                    Mode__c Parcel = new Mode__c(
                        Name = 'Parcel',
                        Mode_Type__c = 'Parcel',
                        Account__c = l.ConvertedAccountId,
                        Est_Shipping_Spend__c = l.Spend_Annual_Parcel__c,
                        Est_Shipping_Volume__c = l.Count_Daily_Parcel__c);
                    InsertModes.add(Parcel);
                }
            }
        }
        if(UpdateModes.size()>0){
            Update Updatemodes;
        }
        if(InsertModes.size()>0){
            Insert InsertModes;
        }
}  
}

And Here is the start of the test:
 
@isTest

private class createModefromLead_TestClass{

    public static testmethod void Convert_NewAcc_CreateMode(){

        //Insert Lead
        Lead LD1 = new Lead(
            LastName = 'LD1',
            Email = 'LD1@LCA.com',
            Company = 'LCA',
            Status = 'Closed - Qualified'
        );
        Insert LD1;
       
        //Convert Lead
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(LD1.id);
        lc.setConvertedStatus('Closed - Qualified');
       
        //Verify LD1 Convert
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());

        //Verify LD1 Creates Mode object on Account
       
    }
}


 
Best Answer chosen by ilewi121
Marty C.Marty C.

Hello, ilewi121, you can use the LeadConvertResult.getAccountId()[1] method to get the ID of the account created upon conversion. With this info, you can construct a query looking for Mode records related to the newly created account. For example:

List<Mode__c> modes = [SELECT Id, Name FROM Mode__c WHERE Account__c = :lcr.getAccountId()];
System.assertEquals(1, modes.size(), 'One and only one Mode should have been created!');

[1]: LeadConvertResult.getAccountId() (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_Database_LeadConvertResult_instance_methods.htm)

All Answers

Marty C.Marty C.

Hello, ilewi121, you can use the LeadConvertResult.getAccountId()[1] method to get the ID of the account created upon conversion. With this info, you can construct a query looking for Mode records related to the newly created account. For example:

List<Mode__c> modes = [SELECT Id, Name FROM Mode__c WHERE Account__c = :lcr.getAccountId()];
System.assertEquals(1, modes.size(), 'One and only one Mode should have been created!');

[1]: LeadConvertResult.getAccountId() (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_Database_LeadConvertResult_instance_methods.htm)

This was selected as the best answer
ilewi121ilewi121
Thank you Marty! That was what I needed!