Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have fields region, revision and Quote. The Quote field will be automatically generated based on selecting region and revision. Quote will be combination of region, alphabetic and count value. for example region is AP and revision is 0, then the quote is generated as "AP-B-001". then the second record Quote is "AP-B-002". etc... currently his flow continues and reached more than 1000 i.e AP-B-1001, AP-B-1002, ...

My requirement is after reaching 1000, i want change the sequence that starts from AP-C-001. After its reaching 1000, then it starts from AP-D-001.

Already "AP-B" sequence reached more than 1000, i don't want affect on these records.

My current code in the trigger is:


if(B.Region__c=='AP'  ){
    integer i1=1;
    String s1='AP';

    String Temp1=s1+'-B-'+i1;

    Integer count=[Select count() from PIF_GDS__c where Region__c=:'AP' and Rev_no__c =:0];

    String STemp1=s1+'-B-'+count;

    Integer rcount=[Select count() from PIF_GDS__c where Name=:STemp1];

        system.debug('--------count-If Count----**------------'+count);
    else if(count >0 && count < 10){
            system.debug('--------count-Ifelse1 Count----**------------'+count);
     else if(count > 10 && count < 100){
             system.debug('--------count-Ifelse1 Countafter2----**------------'+count);
} //end if condition
}// end for loop
}// end if condition
share|improve this question

You have the count already, so Math.mod(count,1000) is your last part. If you divide count like Math.floor(count/1000) it brings you a starting point to decide for B,C,D,... - here you can use some if()'s or a map to get the middle part.

Looking at your code, I suggest to substitute some of your if()-logics by calculations. It will make your code shorter and better readable.

Have a look at:

The string functions together with the math-class should bring you all you need to solve it in the end.

share|improve this answer
Hi Uwe Heim, thanks for your reply. I am new to salesforce. Can you explain more... – Vijay Kumar Jul 27 '14 at 18:21
Sorry I can't provide full code. Above you see the approach I would take. Best thing to read into the links and play with the functions a bit. – Uwe Heim Jul 27 '14 at 21:08
It's OK, no problem. Thanks for giving useful information. At last i got required code. – user5257 Jul 28 '14 at 7:36
If you get stuck and have specific questions to the provided functions, just ask or raise a new question. But try to go on, it's not too hard. – Uwe Heim Jul 28 '14 at 8:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.