+ Start a Discussion
sarvesh001sarvesh001 

Custom settings

Hi,
I am using custom settings data in trigger to populate value in custom field.

I am using list custom settings and i have created field of datatype number in custom settings , My task is  i have to use this value in trigger and every time trigger fired it has to be incremented. I have done this one but the issue is the number value is incremented like 1, 2, 3 ....., .
I want the number  like 001,002,003.....


can any one suggest me.

Thanks,
Sarevsh.

Best Answer chosen by sarvesh001
Deepak Kumar ShyoranDeepak Kumar Shyoran
You can use some string tricks to prepend extra zero

for ex : 
integer sequence1=Integer.valueof(sequencenumber);
sequence1 = sequence1+1;

String temp = '' ;

if(sequence1 >= 100 && sequence1 < 999)
     temp ='0' ;
else if(sequence1 >= 10 && sequence1 < 99)
     temp +='00' ;
else if(sequence1 >= 1 && sequence1 < 9)
     temp +='000' ;		
else if(sequence1 == 0 )
     temp +='0000' ;

String sequence2 = String.valueof(sequence1 );	
sequence2 = temp + sequence1 ;
settings.leadsequencenumber__c=sequence2;
update settings;
You can modify your code like mentioned above.

All Answers

Deepak Kumar ShyoranDeepak Kumar Shyoran
Well in that case you need to take a Text type field in place of Number and then with the help of String to Number and Number to string conversion you can easily convert these values. To Number while fetching from Custom Setting for increment and convert back to string with your format for Saving it in Custom setting again.
Balaji BondarBalaji Bondar
Use the custom label and save incremented value in the custom label every time trigger fired.

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
sarvesh001sarvesh001
Hi deepak ,

I have tried what you suggested but first time its working fine next onwards numbers like 2,3,4.........

trigger customdata on Lead (before insert, before update) {

List<Lead> listL= new List<Lead>();
integer month =  Date.Today().Month();
  integer year =  Date.Today().Year();

Regions__c settings = Regions__c.getInstance('Region');
  string sequencenumber  = settings.leadsequencenumber__c;

for(Lead L:trigger.new){

if(L.Zone__c=='Africa'){

L.Lead_Sequence_No__c= 'AFA'+'-'+year+'-'+month+ '-'+sequencenumber  ;

      }
   }
 
   integer sequence1=Integer.valueof(sequencenumber);
  sequence1 = sequence1+1;
  String sequence2 = String.valueof(sequence1 );
  settings.leadsequencenumber__c=sequence2;

   update settings;

   }
Deepak Kumar ShyoranDeepak Kumar Shyoran
You can use some string tricks to prepend extra zero

for ex : 
integer sequence1=Integer.valueof(sequencenumber);
sequence1 = sequence1+1;

String temp = '' ;

if(sequence1 >= 100 && sequence1 < 999)
     temp ='0' ;
else if(sequence1 >= 10 && sequence1 < 99)
     temp +='00' ;
else if(sequence1 >= 1 && sequence1 < 9)
     temp +='000' ;		
else if(sequence1 == 0 )
     temp +='0000' ;

String sequence2 = String.valueof(sequence1 );	
sequence2 = temp + sequence1 ;
settings.leadsequencenumber__c=sequence2;
update settings;
You can modify your code like mentioned above.
This was selected as the best answer
sarvesh001sarvesh001
Hi Deepak,

Thanks for replay problem is solved.

Thanks,
Sarevsh.