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
ZANE PRATERZANE PRATER 

Dynamically create record name

I have a batch job that looks at when a contract end date is greater than today, that it then creates a new record for renewal.  I am trying to dynamically set the name of the record of the original contract and append each time/counter it has been renewed.  For example, contract A is past the end date, contract B is created and Contract B name is 'contract A name' + renewed 01.  Then if contract B is past the end date, contract C is created and Contract C name is 'contract B name' + renewed 02.  Etc...
Tejender Mohan 9Tejender Mohan 9
Hey
Every time the batch runs it query all the contracts which as passed the date.
So on your batch code's Execute() method, where the scope is being processed.
Inside the scope, traverse loop where new contracts are being created and add a logic there to name them as 
//This might not work if a new contract is having "renewed" word in it's name.

If ( oldContract.Name.indexOf('renewed') != -1){
   newContract.name = oldContract.Name+'renewed'+' '+'01';
}else{
  String suffixNumber = oldContract.Name.substringAfterLast('renewed ');
  integer number = integer.valueof(suffixNumber);
  number++;
  suffixNumber = string.valueof(number);
  newContract.name =  oldContract.Name.substringBefore('renewed')+'renewed'+' '+suffixNumber;
}


Thanks
Tejender Mohan
ZANE PRATERZANE PRATER
Thanks for the suggestion.  So I tried it and get Fatal Error: Invalid Integer
 
If ( paragmt.Name.indexOf('renewed') != -1){
   					TC3.name = paragmt.Name+'renewed'+' '+'01';
				}else{
  						String suffixNumber = paragmt.Name.substringAfterLast('renewed');
  						integer num = integer.valueof(suffixNumber);
      					num++;
  						suffixNumber = string.valueof(num);
  						TC3.name =  paragmt.Name.substringBefore('renewed')+'renewed'+' '+suffixNumber;
                    }

 
ZANE PRATERZANE PRATER
1. I was able to get this to work where setting the indexOf = -1 since checking for first instance of substring 'renewed'.
2. I got the substring of the paragmt name using IdexOf 'renewed' +8. This will prevent adding renewed to the name when renewed is already in the Name.
 
So here is the working code.  Thanks for giving me the direction.
 
If ( paragmt.Name.indexOf('renewed') == -1){
   					TC3.name = paragmt.Name+' renewed'+' '+'01';
				}else{
  						String suffixNumber =paragmt.Name.substring(paragmt.Name.indexOf('renewed')+8);
                        system.debug('suffix^^^^^^^^^^^' + suffixNumber);
                       If(suffixNumber.length()>0) {
                        
  						integer num = integer.valueof(suffixNumber);
                        system.debug('*********' + num);
      					num++;
  						suffixNumber = string.valueof(num);
                        TC3.name =  paragmt.Name.substringBefore('renewed')+'renewed'+' '+suffixNumber;}
                    }


 
Tejender Mohan 9Tejender Mohan 9
@Zane Awesome (y)