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
Clare Heath 15Clare Heath 15 

Increment a number when criteria is met

HI

I need to create sequential numbering on an object but not every record requires a number.

I think this may require some code and I am not a developer so wondered if anyone can help.

Here is what I need to do

I have an Object called 'Sales Order'
I have a field on the object called 'Invoice Number' - This is a number field
I have a field on the object called 'Posting Entity Type'


What I need to do is to increment the Invoice Number but only for records where the Posting Entity Type = Invoice

Is this possible and if so could someone help me create a trigger if that is required.

Many Thanks
Clare
ShivankurShivankur (Salesforce Developers) 
Hi Clare,

You can use trigger code like below to achieve this requirement:
trigger SalesOrderTrigger on Sales_Order__c(before insert, before update) { 
             if(Trigger.old[0] != 'Invoice' && Trigger.new[0] == 'Invoice'){
                   integer Count = [Select Count() from Sales_Order__c where Posting_Entity_Type__c = 'Invoice'];
                   Invoice_Number__c = Count +1;
              }
 }
You may need to modify the logic a little bit to fit in with your requirement but should be achievable thing via trigger.

Hope above information helps. Please mark as Best Answer so that it can help others in future.

Thanks.
mukesh guptamukesh gupta
Hi Clare,

This can be achieved in two ways.
1. Using Process Builder + Flow
2. Using a Before insert Trigger + Custom Settings

Below is the logic for using before trigger.

Before update
 
trigger SalesOrderTrigger on Sales_Order__c(before insert, before update) { 
CustomSettings__c cSettings = new CustomSettings__c();
cSetting = [select latest_Running_Number__c from CustomSettings__c LIMIT 1];
Integer LatestNumber = cSettings.Latest_Running_Number__c;

for(Sales_Order__c SO: Trigger.New){
if(Trigger.isInsert()){

     if(SO.Posting_Entity_Type__c == 'Invoice'){
            SO.Invoice_Number__c = LatestNumber + 1;
     }
}
if(Trigger.isUpdate()){

     if(Trigger.oldMap.get(SO.Id).Posting_Entity_Type__c != 'Invoice' && SO.Posting_Entity_Type__c == 'Invoice'){             
            SO.Invoice_Number__c = LatestNumber + 1;      
   }
}
}

cSettings.Latest_Running_Number__c = LatestNumber;
update cSettings;
}

If you want to implement the same in Process Builder and Flow.
1. Create a process builder with the same condition
2. Use Fast Lookup to get the Custom settings value
3. Increment the value and use fast update,

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

 
Clare Heath 15Clare Heath 15
Hi Mukesh

Thanks for the reply

I have copied the code but am getting the following error


Error: Compile Error: Invalid type: CustomSettings__c at line 2 column 1

Below is what I have in the trigger:  I am afraid I do not know anything about triggers or what 'Custom Settings'.
Any help is greatly appreciated.

trigger InvoiceNumber on OrderApi__Sales_Order__c (before insert, before update) {
CustomSettings__c cSettings = new CustomSettings__c();
cSetting = [select latest_Running_Number__c from CustomSettings__c LIMIT 1];
Integer LatestNumber = cSettings.Latest_Running_Number__c;

for(Sales_Order__c SO: Trigger.New){
if(Trigger.isInsert()){

     if(SO.Posting_Entity_Type__c == 'Invoice'){
            SO.Invoice_Number__c = LatestNumber + 1;
     }
}
if(Trigger.isUpdate()){

     if(Trigger.oldMap.get(SO.Id).Posting_Entity_Type__c != 'Invoice' && SO.Posting_Entity_Type__c == 'Invoice'){             
            SO.Invoice_Number__c = LatestNumber + 1;      
   }
   }
}

cSettings.Latest_Running_Number__c = LatestNumber;
update cSettings;
}
Prachi Gadewar 12Prachi Gadewar 12
Hello Clare,

You need to create Custom setting API named CustomSettings__c and also field "latest_Running_Number__c" in it.

Then it will resolved an issue.

KR
Prachi