Auto Number (Display Format) + Trigger

This needs to display into another field of the other object. However, when that record is created the field is generated with the Autonumber but not the display format of AAA-{0000}.

Solution : - 
trigger AutoNumbertrg on Account (before insert) {   Integer intCounter = 0 ;   Account lstAcc = [SELECT Id,AutoNumber__c FROM Account   Order BY Createddate DESC LIMIT 1];   if (!String.Isblank (lstAcc.AutoNumber__c)){       String s1 = lstAcc.AutoNumber__c;       system.debug('Test debug S1:  ' + s1 );       String sNumbers = s1.substringafter( '-');       System.debug('sNumbers ' + sNumbers );       intCounter = integer.valueof (sNumbers);   }   else{       intCounter  = 0 ;   }   for(Account objAccount: trigger.new){      intCounter ++;      objAccount.AutoNumber__c = 'AAA - '+String.ValueOf(intCounter);   } }



Comments

Popular posts from this blog

Adding a red asterisk to required fields using label in LWC

The Developer Console didn't set the DEVELOPER_LOG trace flag on your user. Having an active trace flag triggers debug logging. You have 1,978 MB of the maximum 1,000 MB of debug logs. Before you can edit trace flags, delete some debug logs.

Salesforce: Serial and Parallel Approval