You need to sign in to do that
Don't have an account?
Clare 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
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
You can use trigger code like below to achieve this requirement: 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.
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
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
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;
}
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