Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counter... Please help For Loop???

Status
Not open for further replies.

zishan876

Programmer
Mar 19, 2007
61
0
0
US
Hi I am doing a counter, which if the for loop reaches 100 records to add into my array and then update the records in the database... But if there are more then 100 records then to clear our the list and do the for loop again to make sure everything is updated...
I have no idea what am I doing wrong but it is not working...
Also I need it to break if there are no more records...
Code:
    Integer x;
    Integer y;
    
    List<Contact_Membership__c> cntMembers = new List<Contact_Membership__c>();
    for (x=0; x<2; x++)
    {
    System.Debug('step1- ' + x);
    for(y=0; y<100; y++)
    {
    for(Contact_Membership__c oCMs : [select Id, Account_Membership__c  from Contact_Membership__c where Account_Membership__c in :acctMemberMap.keySet()])
    {
    System.Debug('step2- ' + y);
        Account_Membership__c accMem = acctMemberMap.get(oCMs.Account_Membership__c);
       
        if(accMem.Status__c == 'Member')
        {
            oCMs.Status__c = 'Member';
            oCMs.Type__c = 'Corporate';
            oCMs.Joined__c  = accMem.Joined__c;
            oCMs.Expires__c = accMem.Expires__c;
            oCMs.OwnerId =  accMem.OwnerId;
             
        }
        else if(accMem.Status__c == 'Expired Member')
        {

            oCMs.Status__c = 'Expired Member';
            oCMs.Type__c = 'Pre-Qualified';
            oCMs.Joined__c  = accMem.Joined__c;
            oCMs.Expires__c = accMem.Expires__c;
            oCMs.OwnerId = accMem.OwnerId;
           
        }
         cntMembers.add(oCMs); 
         System.Debug('Step3 ' + y);
        }
        System.Debug('Step4 ' + y + 'x is ' + x);
        if(y==100){
        update cntMembers;
        cntMembers.clear();}
    }

                  

    }
Thanks
 
Are you sure that this is a Java program, it looks more like c# to me? I've never seen a syntax, like your:

for(Contact_Membership__c oCMs : [select Id, Account_Membership__c from Contact_Membership__c where Account_Membership__c in :acctMemberMap.keySet()])

Java also doesn't use a single quote ' for strings, but double quotes " and when you compare strings, you do that not with == but with the String.equals() method.

Further more it is better and faster to use the primitive int in loops than the Integer class.

Anyway your counter never reaches the 100, because you have a for..loop from 0 to 99, which you then execute twice (for whatever reason). So basically, if I understand you code well, you are reading all Contact_Membership__c in the acctMemberMap.keySet() 200 times. Since y never reaches the 100 mark, your database will not updated.

if you want to write to your database from the KeySet() then you should start counting from inside the "for(Contact_Membership__c oCMs : [select Id....." loop.
like:

int counter = 0;

List<Contact_Membership__c> cntMembers = new List<Contact_Membership__c>();
for(Contact_Membership__c oCMs : [select Id, Account_Membership__c from Contact_Membership__c where Account_Membership__c in :acctMemberMap.keySet()]) {
Account_Membership__c accMem = acctMemberMap.get(oCMs.Account_Membership__c);

if (accMem.Status__c == 'Member') {
oCMs.Status__c = 'Member';
oCMs.Type__c = 'Corporate';
oCMs.Joined__c = accMem.Joined__c;
oCMs.Expires__c = accMem.Expires__c;
oCMs.OwnerId = accMem.OwnerId;
} else {
if (accMem.Status__c == 'Expired Member') {
oCMs.Status__c = 'Expired Member';
oCMs.Type__c = 'Pre-Qualified';
oCMs.Joined__c = accMem.Joined__c;
oCMs.Expires__c = accMem.Expires__c;
oCMs.OwnerId = accMem.OwnerId;
}
}

counter++;
cntMembers.add(oCMs);
// check if we reached the 100 mark
if (counter == 100){
update cntMembers;
cntMembers.clear();
counter = 0;
}
}
}

// update remaining records
if (counter > 0) {
update cntMembers;
cntMembers.clear();
}

 
Integer x;
for (x=0; x<2; x++)

Wouldn't that be a type mismatch? Were you able to compile your code?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top