3

Hello I will be glad if anyone can help me. I created two custom number fields on the Lead Object. as below:

SQL_Counter__c
MQL_Counter__C

I am trying to write a trigger that will increase any of these counters whenever Lead Status changes as follows:

If Lead Status == 'Open SQL' then increment SQL_Counter__c by 1  
If Lead Status == 'Open MQL' then increment MQL_Counter__c by 1

My I compile, I do not get any error, but when I edit a Lead record, the fields does not change. Please help review the code.

trigger updateLeadStatusChangeCounter on Lead (after insert, before update) {
    for(Lead l : Trigger.new)
    try{
        if (l.Status!=Null){

        if (l.Status =='Open SQL'){

            l.SQL_Counter__c +=1;
        }
        else if (l.Status =='Open MQL'){

            l.MQL_Counter__c +=1;
        }

    }
    }
     catch (System.NullPointerException e)
     {

     }

}
6
  • When I compile, I do not get any errors, but the trigger is not updating the Counter fields Commented Feb 18, 2016 at 16:43
  • instead after insert use before insert
    – Ratan Paul
    Commented Feb 18, 2016 at 16:45
  • @Ratan I tried that but still no success Commented Feb 18, 2016 at 16:49
  • l.SQL_Counter__c +=1; instead use l.SQL_Counter__c = l.SQL_Counter__c != null ? l.SQL_Counter__c +1: 0;
    – Ratan Paul
    Commented Feb 18, 2016 at 16:52
  • 1
    @Ratan, You are a star! this works, thank you very much.. Commented Feb 18, 2016 at 17:00

2 Answers 2

1

Solution is .... Basically you used try catch so you are not getting error

Issue in your field l.SQL_Counter__c is null that was the reason....

trigger updateLeadStatusChangeCounter on Lead (before insert, before update) {
    for(Lead l : Trigger.new){

        if (l.Status =='Open SQL'){
            l.SQL_Counter__c = l.SQL_Counter__c != null ? l.SQL_Counter__c +1: 1;
        }
        else if (l.Status =='Open MQL'){
            l.MQL_Counter__c = l.MQL_Counter__c != null ? l.MQL_Counter__c + 1: 1;
        }
    }
}
9
  • I need on more help to get a 100% code coverage, My test class is giving me only 63%, I don't know what I am doing wrong. private class updateLeadStatusChangeCounter { @isTest static void UpdateCounter() { Lead l = new Lead(); l.FirstName = 'Jack'; l.LastName = 'Daniels'; l.Status = 'Open'; l.Company = 'Snow White Consulting'; l.LeadSource = 'Event'; insert l; l.Company = 'Snow Global Consulting'; l.Status = 'Prospect'; update l; } } Commented Feb 18, 2016 at 17:38
  • @user3722925 pls ask a new question with you trigger and test class.. I will take a look into your issue
    – Ratan Paul
    Commented Feb 18, 2016 at 17:39
  • @user3722925 try this private class updateLeadStatusChangeCounter { @isTest static void UpdateCounter() { Lead l = new Lead(); l.FirstName = 'Jack'; l.LastName = 'Daniels'; l.Status = 'Open SQL'; l.Company = 'Snow White Consulting'; l.LeadSource = 'Event'; insert l; system.assertEquals([SELECT SQL_Counter__c FROM Lead WHERE Id =: l.Id ].SQL_Counter__c, 1); l.Company = 'Snow Global Consulting'; l.Status = 'Open MQL'; update l; system.assertEquals([SELECT MQL_Counter__c FROM Lead WHERE Id =: l.Id ].MQL_Counter__c , 1); } }
    – Ratan Paul
    Commented Feb 18, 2016 at 17:51
  • 1
    Wow! it works again... 100% coverage! you are really a star! thank you very much. Commented Feb 18, 2016 at 17:54
  • not sure why I got downvote. If I am doing anything wrong pls let me know I will correct that.. :-)
    – Ratan Paul
    Commented Feb 19, 2016 at 15:55
0

Well, I think this should work in before insert trigger, Added braces in line three and second last line. Also make sure that the default values are there in MQL_Counter__c and SQL_Counter__c. You may want to mark default as zero.

trigger updateLeadStatusChangeCounter on Lead (before insert, before update) {
    for(Lead l : Trigger.new){ //ADD Start Bracket Here
    try{
        if (l.Status!=Null){

        if (l.Status =='Open SQL'){

            l.SQL_Counter__c +=1;
        }
        else if (l.Status =='Open MQL'){

            l.MQL_Counter__c +=1;
        }

    }
    }
     catch (System.NullPointerException e)
     {

     }
}//Add this Here
}

UPDATE

First try doing without using try catch block. That will help you to uncover any issues masked by exception handling.

Modified Code without try catch

trigger updateLeadStatusChangeCounter on Lead (before insert, before update) {
        for(Lead l : Trigger.new){ //ADD Start Bracket Here
        //try{ Commented
            if (l.Status!=Null){

            if (l.Status =='Open SQL'){

                l.SQL_Counter__c +=1;
            }
            else if (l.Status =='Open MQL'){

                l.MQL_Counter__c +=1;
            }

        }
        /*}Commented
         catch (System.NullPointerException e)
         {

         }*/
    }//Add this Here
    }
1
  • Without the try and catch, I was having 'Attempt to dereference a null object' errors which even made it worst Commented Feb 18, 2016 at 17:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .