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
BrianWXBrianWX 

Unexpected System.assertException

I have a trigger that, before insert, base on a value selected from a picklist on the Lead form, it will auto-populate another value to another field.  In order to do this, we maintain a mapping table where we select the mapping value from.

 

The issue I have is to write a test case.  When I do the System.assertEquals, becasue the Global_Lead_Subtype_New__c never formulated/populated from the lead object, it throws error when I try to compare two values: one inserted against one from lead object. 

 

I don't think I need to populate Lead_Subtype_New__c when formulate a lead in my test.  Would that defeat the purpose of the trigger?

 

Here are my codes.  What's the trick for doing this?

 

Trigger class:

 

trigger ETO_SourceDetail_LeadSubType on Lead (before insert) {
    // get current Lead.RecordTypeId   
    
    String strRecordTypeId = '';
    List<Id> recordTypeId = NEW List<Id>();
    List<Source_Detail__c> leadSubtype = NEW List<Source_Detail__c>();
    List<Integer> count = NEW List<Integer>();
    Integer i = 0;
    
    
    try {
        FOR (Lead l : TRIGGER.NEW) {
            try {    
                recordTypeId.add(l.recordTypeId);                      
                strRecordTypeId = recordTypeId.get(0);
                strRecordTypeId = strRecordTypeId.substring(0, 15);
                
                count.add([SELECT count() FROM Source_Detail__c WHERE Lead_Record_Type_Id__c = :strRecordTypeId AND Value_del__c = :l.Source_Details_New__c]);
                
                if (count.get(i) > 0) {
                    leadSubtype.add([SELECT Lead_Subtype__c FROM Source_Detail__c WHERE Lead_Record_Type_Id__c = :strRecordTypeId AND Value_del__c = :l.Source_Details_New__c LIMIT 1]);
                    l.Global_Lead_Subtype_New__c = leadSubtype.get(0).Lead_Subtype__c;
                }
                else {
                    l.Global_Lead_Subtype_New__c = 'Unknown';
                }
                
                System.debug('recordTypeId: ' + l.recordTypeId + ' - ' + 'Value_del__c: ' + l.Source_Details_New__c + ' - ' + 'strRecordTypeId: ' + strRecordTypeId );
                System.debug('Global_Lead_Subtype_New__c: ' + l.Global_Lead_Subtype_New__c);
                         
                i++;                                    
            } catch (Exception e) {
                l.addError('Error with Source Detail to Lead Subtype mapping. Please contact your administrator: ' + e);
            }
        } //for
    } catch(Exception e) {
        for(Lead l : Trigger.New){
            l.addError('Error with Source Detail to Lead Subtype mapping. Please contact your administrator: ' + e);
        }
    } //try        
        
    
} //class

Test method:

 

@isTest
private class testTrigger_ETO_SourceDetail_LeadSubType {
    static testMethod void myTestMethod() {
        Lead l = new Lead();
        
        l.recordTypeId = '01280000000Pxz2AAC';
        l.company = 'Test by Brian';
        l.firstName = 'Brian';
        l.lastName = 'Do';
        l.phone = '4087773456';
        l.email = 'briando@email.com';
        l.city = 'Chicago';
        l.state = 'IL';
        l.country = 'US';
        l.status = 'Open';
        l.Percent_to_Close__c = '0%';
        l.CurrencyIsoCode = 'USD';
        l.Sales_Rep_TMJ__c = 'Aaron Marks';
        l.OwnerId = '00580000003GON5';
        l.Source_Details_New__c = 'Austin eDM';       
        
        insert(l);
        
        Lead insertedLead = [SELECT Id, Global_Lead_Subtype_New__c FROM Lead WHERE Id = :l.Id LIMIT 1];
        
        
        System.assertEquals(insertedLead.Global_Lead_Subtype_New__c, l.Global_Lead_Subtype_New__c);
    }
} //end class



Thank you in advance.

 

Brian

Best Answer chosen by Admin (Salesforce Developers) 
iBr0theriBr0ther

You compared the object in memory against the object in database.

 

l.Global_Lead_Subtype_New__c is nothing because it is still in the memory. (only ID is populated after issue DML insert)

 

but

 

insertedLead .Global_Lead_Subtype_New__c is already populated, because you retrived it from database.

 

All Answers

iBr0theriBr0ther

You compared the object in memory against the object in database.

 

l.Global_Lead_Subtype_New__c is nothing because it is still in the memory. (only ID is populated after issue DML insert)

 

but

 

insertedLead .Global_Lead_Subtype_New__c is already populated, because you retrived it from database.

 

This was selected as the best answer
BrianWXBrianWX

You were right.  I saw that cloud bug now :)

BrianWXBrianWX

I modified the test class.  I tested in dev and it passed 77% threshold required by Salesforce.  No error found.

 

When I tried to use Deploy to deploy from my dev environment to production, and in production I use Inbound to Validate the deployment before actual deploing, it failed with Exception found.

 

I am wondering why my test passed on dev, but failed in deployment?  Or perhaps my test class actuall posed some error?

 

Thanks in advance.

 

Here is my codes:

 

@isTest
private class testTrigger_ETO_SourceDetail_LeadSubType {
    static testMethod void myTestMethod() {
        List<Lead> leads = new List<Lead>();
        
        try {
            for (Integer i = 0; i <= 1; i++) {
                Lead l = new Lead();
                l.recordTypeId = '01280000000Pxz2AAC';
                l.company = 'Test by Brian ' + i+1;
                l.firstName = 'Brian';
                l.lastName = 'Do';
                l.phone = '4087773456';
                l.email = 'briando@email.com';
                l.city = 'Chicago';
                l.state = 'IL';
                l.country = 'US';
                l.status = 'Open';
                l.Percent_to_Close__c = '0%';
                l.CurrencyIsoCode = 'USD';
                l.Sales_Rep_TMJ__c = 'Aaron Marks';
                l.OwnerId = '00580000003GON5';
                
                if (i == 0) {                
                    l.Source_Details_New__c = 'Austin eDM';
                    insert(l);
                    Lead insertedLead = [SELECT Id, Global_Lead_Subtype_New__c FROM Lead WHERE Id = :l.Id];
                    System.assertEquals(insertedLead.Global_Lead_Subtype_New__c, 'External Webform');      
                    
                    System.debug('testTrigger_ETO_SourceDetail_LeadSubType - External Webform');
                }
                else {                   
                    l.Source_Details_New__c = '';
                    insert(l);
                    Lead insertedLead = [SELECT Id, Global_Lead_Subtype_New__c FROM Lead WHERE Id = : l.Id];
                    System.assertEquals(insertedLead.Global_Lead_Subtype_New__c, 'Unknown');
                    System.debug('testTrigger_ETO_SourceDetail_LeadSubType - Unknown');
                }
                leads.add(l);
            } // for            
        } catch(Exception e) {
            System.debug(e.getMessage());
        }
        
        leads.clear();        
    }
} //end class

iBr0theriBr0ther

What did the error messge say?

BrianWXBrianWX

Failure Message: "System.AssertException: Assertion Failed: Expected: Unknown, Actual: External Webform", Failure Stack Trace: "Class.testTrigger_ETO_SourceDetail_LeadSubType.myTestMethod: line 28, column 21 External entry point"

iBr0theriBr0ther

Is there any Workflow: Field update on the destination org? You may check whether WF/Field Update working on the same field. Or could be new another trigger as well.

BrianWXBrianWX

You are right.  I still have an active workflow updating to the same field.  The trigger supposes to replace that workflow :)

 

Now I understand more on the meaning of the error message.

 

Thanks,

Brian