Hello I will be glad if anyone can help me. I created a custom number field on the Lead Object. Sales_Ready_Counter__c

I wrote a trigger to increment Sales_Ready_Counter__c by 1 whenever a custom picklist (Sales_Ready__c) value changes as follows:

If Sales_Ready__c == 'yes' then increment Sales_Ready_Counter__c by 1
Sales_Ready_Counter__c starts from Zero (0). I want it to start from 1 instead of starting from 0. I can't figure out what I am doing wrong, please help review, my code is below:

trigger updateLeadSalesReadyCounter on Lead (before insert, before update) {
    for(Lead l :Trigger.new)
    Lead oldSR = Trigger.oldMap.get(l.Id);
    Boolean oldStatus = oldSR.Sales_Ready__c.equals('Yes');
    Boolean newStatus = l.Sales_Ready__c.equals('Yes');

        if (!oldStatus && newStatus)
                if(l.Sales_Ready__c != Null)
                    if(l.Sales_Ready__c == 'Yes')
                            l.Sales_Ready_Counter__c = l.Sales_Ready_Counter__c != Null ? l.Sales_Ready_Counter__c +1: 0;
    catch(System.NullPointerException e)
  • Does your code going inside if (!oldStatus && newStatus) condition? and if it is going, you want code should be started from 1, instead of zero, you can add 1 at the end. l.Sales_Ready_Counter__c = l.Sales_Ready_Counter__c != Null ? l.Sales_Ready_Counter__c +1: 1;
    – Ysr Shk
    Mar 10, 2016 at 8:24
  • @Novarg, (!oldStatus && newStatus) is just to test the old status and the new status to make sure the Counter only increment by 1 when the value actually changes. e.g if Sales_Ready__c is = 'Yes' and you edit the record, Sales_Ready_Counter__c should not be incremented, unless it change from another value to yes. I added the 1 to the end and it worked but only the second time when the Sales_Ready__c is set to yes. I want it to be set to 1 from the first time the value changes to yes. Mar 10, 2016 at 8:46
  • As an aside, using try/catch as you are hides errors and just makes debugging much harder in the long run.
    – Keith C
    Mar 10, 2016 at 9:44

1 Answer 1


If you give your line as

l.Sales_Ready_Counter__c = l.Sales_Ready_Counter__c != Null ? l.Sales_Ready_Counter__c +1: 1;

Then for the first time when the field is null, the value will be set to 1. In every other situation the value in there will be incremented by 1. Hope my understanding is correct. Thanks

  • thanks for your comment. When a record is created, the default value is for Sales_Ready_Counter__c is ' Null', When the record is edited and the value for Sales_Ready__c us set to 'Yes', Sales_Ready_Counter__c still remains 'Null'. It only increase to 1 when the Value of Sales_Ready__c is edited for the second time. I want it to be set to 1 the first time Sales_Ready__c is set to 'Yes' and not the second time. Mar 10, 2016 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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