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
Nitesh TrivediNitesh Trivedi 

how to increment numeric value of string

Andrew GAndrew G
context would be nice

but if playing in formula, use of ISNUMBER() and VALUE() could resolve the issue
 
IF(ISNUMBER(Text__c), VALUE(Text__c)+1,Text__c)

If playing in apex, something like:
 
Integer textToNumber= Integer.ValueOf(textfield__c.trim());
textToNumber++;
textfield__c= '' + textToNumber;

hth

Andrew
 
Foram Rana RForam Rana R
Hi Nitesh,
 
Example :

String str = '100';

if(str.isNumeric()) {
	Integer numbervalue = Integer.valueof(str);
	numbervalue = numbervalue +1;
	System.debug('@@ Result '+numbervalue);
}

Thanks,
Foram rana
Nitesh TrivediNitesh Trivedi

Hi Foram,

there is field which is text type and value is combination of alphabet and numeric and i want to increase numeric value for next entered record. 

Footprints on the MoonFootprints on the Moon
Hi Nitesh,
Can you provide example of this alphanumeric string? 
Nitesh TrivediNitesh Trivedi
trigger autoincrementemployeeid on Employee__c (before insert,before update) {
     Employee__c e =[select Employee_Id__c from Employee__c ORDER BY createdDate desc limit 1 ][0];
    system.debug('====old idd'+e.Employee_Id__c);
    for(Employee__c emp : trigger.new){
        string id=e.Employee_Id__c;
        system.debug('===ggh=='+id);
        integer index = id.lastIndexOf('-');
        
        string text = id.substring(index);
        system.debug('===text=='+ text);
         Integer textToNumber= Integer.ValueOf(text);
        system.debug('===text to number===='+ textToNumber);
textToNumber++;
emp.Employee_Id__c= 'KC-' + textToNumber;   
        system.debug('=======================================id===='+emp.Employee_Id__c);
        
    }
}

my number is not incremented properly it only revolve around 0 and 1
Andrew GAndrew G
here are some extra steps that may help
String id= 'KC-012345';
System.debug('===raw=='+id);
Integer index = id.lastIndexOf('-');

String text = id.substring(index+1);
System.debug('===text=='+ text);
Integer textToNumber= Integer.valueOf(text);
System.debug('===text to number==='+ textToNumber);
textToNumber++;
System.debug('===new text to number==='+ textToNumber);
String numberToText = String.ValueOf(textToNumber);
System.debug('---padded number ---' + numberToText.leftPad(6,'0'));
String newEmp = 'KC-'+ numberToText.leftPad(6,'0');
System.debug('new ID '+ newEmp);

Pardon all the debugs , but if you post it to an Execute Anonymous window you will see exactly what is happening.


Regards

Andrew