0

I've written a method to identify any lead inserted to 2 specific queues and set the record type accordingly - which is different than the expected API user's default. I've debugged line by line (provided below) and noticed two things:

  • The record type is set for a lead that is not committed to the database, yet I'm not seeing where the value came from. I'm assuming this is an Order of Executions things. Can anyone confirm that?

  • I set the record type ID on an lead from a Before Insert Trigger, but the value is not returned to the assertion in the Test Method. This I am very confused about and could use help.

Test Method:

    static testMethod void test_LeadTrigger_queueOwnership(){
    //build test queue Leads for insert
    System.debug(logginglevel.INFO,'## LeadHelper.queueOwnership data setup begin');
    list<Lead> queueLeads = new List<Lead>();
    list<Lead> nonQueueLeads = new List<Lead>();
    Id queuePR = [SELECT g.id FROM Group g WHERE g.DeveloperName='Public_Relations'].id;
    System.debug(logginglevel.INFO,'##queuePR = '+queuePR);
    Id queueCO = [SELECT g.id FROM Group g WHERE g.DeveloperName='Company'].id;
    System.debug(logginglevel.INFO,'##queueCO = '+queueCO);
    Id coRT = Util.getRecordTypeId('Lead', 'Client');
    System.debug(logginglevel.INFO,'##coRT = '+coRT);

    for(Integer i = 0; i <= 250; i++){
        Lead a = new Lead(LastName='Test'+i, Email='test'+i+'@test.com', OwnerId=queuePR);
        Lead b = new Lead(LastName='CO'+i, Email='test'+i+'@co.com', OwnerId=queueCo);
        queueLeads.add(a);
        queueLeads.add(b);
    }
    lead qld = queueLeads.remove(0);

    for(Integer i = 0; i <= 250; i++){
        Lead a = new Lead(LastName='Non-Test'+i, Email='Non-test'+i+'@test.com', OwnerId=UserInfo.getUserId());
        Lead b = new Lead(LastName='Non-CO'+i, Email='Non-test'+i+'@co.com', OwnerId=UserInfo.getUserId());
        nonQueueLeads.add(a);
        nonQueueLeads.add(b);
    }
    lead ld = nonQueueLeads.remove(0);

    System.debug(logginglevel.INFO,'## LeadHelper.queueOwnership data setup end');

    //Start Test
    Test.startTest();

    /**** LeadHelper.queueOwnership Test ****/
    //Single Record Insert
    Try{
        System.debug(logginglevel.INFO,'##qld Insert Begin; OwnerId: '+qld.ownerId+', RecordTypeId: '+qld.RecordTypeId+', Email: '+qld.Email);
        insert qld;
        System.debug(logginglevel.INFO,'##qld Insert End; OwnerId: '+qld.ownerId+', RecordTypeId: '+qld.RecordTypeId+', Email: '+qld.Email);
    } Catch (exception e){
        System.debug(logginglevel.INFO,'##leadHelper.queueOwnership Positive Single Test Insert Exception: '+e.getMessage());
    }
    System.assertEquals(coRT, qld.RecordTypeId);
  //This continues further but it's irrelevant

Trigger:

trigger LeadTrigger on Lead (before insert, before update, after insert, after undelete, after update) {

if (trigger.isBefore && trigger.isInsert) {
    LeadHelper.generateMBOId(trigger.new);
    MBOPartnersHelper.updateLeadSuppression(trigger.new, trigger.oldMap);
    System.debug(logginglevel.INFO,'## LeadHelper.queueOwnership Start; OwnerId: '+trigger.new[0].ownerId+', RecordTypeId: '+trigger.new[0].recordTypeId+', Email: '+trigger.new[0].Email);
    LeadHelper.queueOwnership(trigger.new);
}
//Again, continues further but irrelevant

Method:

    public static void queueOwnership(list<Lead> leadList){

    If(leadList != NULL && !leadList.isEmpty()){
        System.debug(logginglevel.INFO,'## leadList Not NULL or Empty');
        Id queuePR = [SELECT g.id FROM Group g WHERE g.DeveloperName='Public_Relations'].id;
        System.debug(logginglevel.INFO,'## queuePR = '+queuePR);
        Id queueCO = [SELECT g.id FROM Group g WHERE g.DeveloperName='Company'].id;
        System.debug(logginglevel.INFO,'## queueCO = '+queueCO);
        Id coRT = util.getRecordTypeId('lead', 'Client');
        System.debug(logginglevel.INFO,'## coRT = '+coRT);

        For(lead a :leadList){
            System.debug(logginglevel.INFO,'## a.OwnerId = '+a.OwnerId+'; a.RecordTypeId = '+a.RecordTypeId);
            IF(a.OwnerId==queuePR||a.OwnerId==queueCO){
                System.debug(logginglevel.INFO,'## a.RecordTypeId set to: '+coRT);
                a.RecordTypeId=coRT;
                system.debug(logginglevel.INFO,'## a.RecordTypeId = '+a.RecordTypeId);
            }
        }
    }
}

Finally, here is the debug log - I've adjusted the logging to Error for everything except... DB: WARN, APEX: INFO, System: WARN.

15:37:05.803 (103803368375)|USER_DEBUG|[138]|INFO|## LeadHelper.queueOwnership data setup begin
15:37:05.807 (103807573202)|USER_DEBUG|[142]|INFO|##queuePR = 00GC0000002Cdb3MAC
15:37:05.810 (103810566184)|USER_DEBUG|[144]|INFO|##queueCO = 00GC0000002Cdb2MAC
15:37:05.816 (103816159732)|USER_DEBUG|[146]|INFO|##coRT = 012C0000000Q7rlIAC
15:37:05.950 (103950545979)|USER_DEBUG|[164]|INFO|## LeadHelper.queueOwnership data setup end
15:37:05.955 (103955229195)|USER_DEBUG|[172]|INFO|##qld Insert Begin; OwnerId: 00GC0000002Cdb3MAC, RecordTypeId: null, Email: test0@test.com
15:37:06.017 (104017606292)|ENTERING_MANAGED_PKG|awduplicate2
15:37:06.033 (104033380075)|CODE_UNIT_STARTED|[EXTERNAL]|01qC0000000Td3C|LeadTrigger on Lead trigger event BeforeInsert for [new]
15:37:06.042 (104042471723)|USER_DEBUG|[13]|INFO|## LeadHelper.queueOwnership Start; OwnerId: 00GC0000002Cdb3MAC, RecordTypeId: 012C0000000Q7rmIAC, Email: test0@test.com
15:37:06.042 (104042627403)|USER_DEBUG|[212]|INFO|## leadList Not NULL or Empty
15:37:06.046 (104046140111)|USER_DEBUG|[214]|INFO|## queuePR = 00GC0000002Cdb3MAC
15:37:06.049 (104049540583)|USER_DEBUG|[216]|INFO|## queueCO = 00GC0000002Cdb2MAC
15:37:06.052 (104052784261)|USER_DEBUG|[218]|INFO|## coRT = 012C0000000Q7rlIAC
15:37:06.052 (104052904759)|USER_DEBUG|[221]|INFO|## a.OwnerId = 00GC0000002Cdb3MAC; a.RecordTypeId = 012C0000000Q7rmIAC
15:37:06.053 (104053005205)|USER_DEBUG|[223]|INFO|## a.RecordTypeId set to: 012C0000000Q7rlIAC
15:37:06.053 (104053143247)|USER_DEBUG|[225]|INFO|## a.RecordTypeId = 012C0000000Q7rlIAC
15:37:00.978 (104068268336)|CUMULATIVE_LIMIT_USAGE
15:37:00.978|TESTING_LIMITS
15:37:00.978|CUMULATIVE_LIMIT_USAGE_END
15:37:06.068 (104068454878)|CODE_UNIT_FINISHED|LeadTrigger on Lead trigger event BeforeInsert for [new]
15:37:06.082 (104082166691)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Lead:new
15:37:06.085 (104085677212)|CODE_UNIT_FINISHED|Validation:Lead:new
15:37:06.124 (104124956727)|ENTERING_MANAGED_PKG|awduplicate2
15:37:06.166 (104166119118)|CODE_UNIT_STARTED|[EXTERNAL]|01qC0000000Td3C|LeadTrigger on Lead trigger event AfterInsert for [00QM00000064d0b]
15:37:01.078 (104168198976)|CUMULATIVE_LIMIT_USAGE
15:37:01.078|TESTING_LIMITS
15:37:01.078|CUMULATIVE_LIMIT_USAGE_END
15:37:06.168 (104168231802)|CODE_UNIT_FINISHED|LeadTrigger on Lead trigger event AfterInsert for [00QM00000064d0b]
15:37:06.173 (104173109916)|ENTERING_MANAGED_PKG|lmscons
15:37:06.211 (104211737219)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Lead
15:37:06.231 (104231450471)|ENTERING_MANAGED_PKG|awduplicate2
15:37:06.245 (104245178069)|CODE_UNIT_STARTED|[EXTERNAL]|01qC0000000Td3C|LeadTrigger on Lead trigger event BeforeUpdate for [00QM00000064d0b]
15:37:01.166 (104255596982)|CUMULATIVE_LIMIT_USAGE
15:37:01.166|TESTING_LIMITS
15:37:01.166|CUMULATIVE_LIMIT_USAGE_END
15:37:06.255 (104255667852)|CODE_UNIT_FINISHED|LeadTrigger on Lead trigger event BeforeUpdate for [00QM00000064d0b]
15:37:06.272 (104272821212)|ENTERING_MANAGED_PKG|awduplicate2
15:37:06.285 (104285372622)|CODE_UNIT_STARTED|[EXTERNAL]|01qC0000000Td3C|LeadTrigger on Lead trigger event AfterUpdate for [00QM00000064d0b]
15:37:01.197 (104287381481)|CUMULATIVE_LIMIT_USAGE
15:37:01.197|TESTING_LIMITS
15:37:01.197|CUMULATIVE_LIMIT_USAGE_END
15:37:06.287 (104287413624)|CODE_UNIT_FINISHED|LeadTrigger on Lead trigger event AfterUpdate for [00QM00000064d0b]
15:37:06.290 (104290585670)|ENTERING_MANAGED_PKG|lmscons
15:37:06.326 (104326651765)|CODE_UNIT_FINISHED|Workflow:Lead
15:37:06.326 (104326952628)|USER_DEBUG|[174]|INFO|##qld Insert End; OwnerId: 00GC0000002Cdb3MAC, RecordTypeId: null, Email: test0@test.com
15:37:06.327 (104327045102)|EXCEPTION_THROWN|[178]|System.AssertException: Assertion Failed: Expected: 012C0000000Q7rlIAC, Actual: null
15:37:06.327 (104327196677)|FATAL_ERROR|System.AssertException: Assertion Failed: Expected: 012C0000000Q7rlIAC, Actual: null Class.MBOPartnersTests.test_LeadTrigger_queueOwnership: line 178, column 1
15:37:06.327 (104327208932)|FATAL_ERROR|System.AssertException: Assertion Failed: Expected: 012C0000000Q7rlIAC, Actual: null Class.MBOPartnersTests.test_LeadTrigger_queueOwnership: line 178, column 1

The only thing I'm seeing execute between the debug is something in the AWDuplicate package. I'm going to play with the log levels more to get see if I can pinpoint that, but I wouldn't expect anything in there to set or remove the record type ID - it's purely a de-dup app. Plus there was no exception thrown when I tried the insert.

3
  • 2
    I'm sure you have to do a query to the database on that lead before asserting. Jan 29, 2015 at 21:40
  • @MihaiNeagoe, go ahead and throw an answer up there if you'd like - I'll give you credit for it. Punching myself over something obvious like that now. Still not sure why the Method initialized the lead with no RecordType but the debug on the Trigger showed it with a RecordType.
    – zainogj
    Jan 29, 2015 at 21:52
  • No worries, it can happen to all of us. That's why this forum exist, for another pair of eyes. I've been looking at your code for the last 20 minutes and couldn't figure where you did the mistake. Also, nice use of system.debug, will borrow it in my code in the future. Will convert my comment to an answer. Jan 29, 2015 at 21:55

1 Answer 1

1

You have to do a query on the lead before asserting.

Lead qldAssert = [Select RecordTypeId From Lead Where Id = :qld.Id]
System.assertEquals(coRT, qldAssert.RecordTypeId);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.