I am trying to make email campaigns for two people both people need 6 buy campaigns and 6 sell campaigns. They have 5 parameters 3 of which are the same for all 12 of each person campaigns, the other 2 segment list, and campaign name are unique to each email campaign. When I run this code it create as many of the 1st buyer campaign for the first person until the callout times out. So instead of 12 campaigns for each person, I get 75 to 94 buyer 1 campaigns for the first person. The debug I and i2 show it never gets off 0, but I don't know why that is.

public class campdata {
    //Campiagn Maker vars
    public integer  key {get; set;}
    public integer sndrid {get; set;}
    public list<integer> lids = new list<integer>();
    public list<integer> segids = new list<integer>();
    public list<integer> sgi = new list<integer>();
    public list<string> tempname = new list<string>();
    list<string> name = new list<string>{'shay', 'austin'};

    public void sorts(){

        for(integer i=0; i <= 1; i++){
            system.debug('i@@@@@@@ '+i);
            string placeholder = name[i];
            sendgridsettings__c sgs = sendgridsettings__c.getInstance(placeholder);
            //Campaign Names
            string campstring = sgs.campaign_names__c; system.debug(campstring);
            tempname = campstring.split(','); system.debug('tempname@@@@@ '+tempname);

            //unsub group ids
            string unsubstring = sgs.unsubgroup__c;
            List<string> unsub = unsubstring.split(',');
            for(string usb :unsub){
                integer j = integer.valueOf(usb.trim()); system.debug(j+' =jjjj@@@@@@@@@'); system.debug('sgi&&&'+sgi);

                system.debug('sgi #@#@#@#   '+sgi);

            // segment id 
            string segstring = sgs.segment_id__c;
            List<String> segs = segstring.split(',');
            for(String str :segs){
                integer k = Integer.valueOf(str.trim());

                system.debug('segids #@#@#@#   '+segids);

            for(integer i2=0; i2<11; i++){
                system.debug('i2@@@@@@@ '+i2);
                //sender id retrieved from custom setting
                sndrid = integer.valueof(sgs.Sender__c);
                // list id 
                integer lid  = integer.valueof(sgs.list_id__c); system.debug('lid@@@@@@'+ lid);

                if(i2<=5 && i!=1){
                    key = 0;
                if(i2>=6 && i==1){
                    key = 1;
                integer senderid = sndrid;
                integer listid = lids[i];
                integer segmentid = segids[i2];
                integer unsubgroupid = sgi[key];
                string campaignname = tempname[i2];

                SendGrid_CampaignMaker SGCM = new SendGrid_campaignMaker();
                SGCM.builder(senderid, listid, segmentid, unsubgroupid, campaignname);
  • 2
    Consistency in code formatting is important for other people to be able to read [your code]. If your formatting (like indentation) is all over the place, like it was in this question, it can turn a lot of people off on trying to help you. Just something to keep in mind for the future.
    – Derek F
    Sep 15, 2017 at 18:39

1 Answer 1


Here is your problem. You increment a variable (i) which is different from the loop variable (i2):

for(integer i2=0; i2<11; i++){
//          ^^    ^^     ^
//                       this one is different

I recommend you just reuse i as your loop variable, or if you prefer, use a more distinct name like j.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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