Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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(trigger.isinsert){
for(PIF_GDS__c B:trigger.new){


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];
    system.debug('--------count-L----------------'+count);

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

    Integer rcount=[Select count() from PIF_GDS__c where Name=:STemp1];
    system.debug('--------count-L----**------------'+rcount);

    if(count==0){ 
        count++;
        system.debug('--------count-If Count----**------------'+count);
        STemp1=s1+'-B-'+count;
        B.Name=STemp1;
    }
    else if(count >0 && count < 10){
            system.debug('--------count-Ifelse1beforeCount----**------------'+count);         
            count++;
            system.debug('--------count-Ifelse1 Count----**------------'+count);
            B.Name=s1+'-B-00'+count;
     }
     else if(count > 10 && count < 100){
             system.debug('--------count-Ifelse1beforeCount2----**------------'+count);
             count++;
             system.debug('--------count-Ifelse1 Countafter2----**------------'+count);
            B.Name=s1+'-B-0'+count;
     }
     else{
        system.debug('--------count-elseendbefore----**------------'+count);
            count++;
             system.debug('--------count-elseendafter----**------------'+count);
            B.Name=s1+'-D-'+count;
     }    
} //end if condition
}// end for loop
}// end if condition
share|improve this question

1 Answer 1

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 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 at 21:08
    
It's OK, no problem. Thanks for giving useful information. At last i got required code. –  user5257 Jul 28 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 at 8:10

Your Answer

 
discard

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.