You need to sign in to do that
Don't have an account?
Geetha Chandran 1
SOQL in for loop
Can someone please help me to get the SOQL (on opportunity object) from the for loop? I understand that we cannot have soql inside a for loop. Thanks a lot!!
for (CampaignMember cm: cms) {
oldcm= (CampaignMember)oldlist[i];
//Update Opportunity Amount
o = [Select ID, Contact__c, CloseDate, CampaignID, OE_Program_of_Interest__c, Amount, Cvent_Balance_Due__c, Amount_Paid__c, Description, StageName from Opportunity where Contact__c = :cm.ContactID and Campaign.ID = :cm.CampaignId limit 1];
if(o.size() == 1){
if(cm.Cost__c != oldcm.Cost__c){o[0].Amount = cm.Cost__c;}
if(cm.Amount_Paid__c != oldcm.Amount_Paid__c){o[0].Amount_Paid__c = cm.Amount_Paid__c;}
if(cm.Balance_Due__c != oldcm.Balance_Due__c){o[0].Cvent_Balance_Due__c = cm.Balance_Due__c;}
if(cm.Status != oldcm.Status && cm.Status == 'Registered'){o[0].StageName = 'Closed Won';}
if(cm.Payment_Status__c != oldcm.Payment_Status__c && cm.Payment_Status__c == 'Paid in Full'){o[0].StageName = 'Closed Paid';}
if(cm.Status != oldcm.Status && (cm.Status == 'Cancelled' || cm.Status == 'Unattended' || cm.Status == 'Withdraw')){o[0].StageName = 'Closed Lost';}
Updopps.add(o[0]);
}
i++;
}
for (CampaignMember cm: cms) {
oldcm= (CampaignMember)oldlist[i];
//Update Opportunity Amount
o = [Select ID, Contact__c, CloseDate, CampaignID, OE_Program_of_Interest__c, Amount, Cvent_Balance_Due__c, Amount_Paid__c, Description, StageName from Opportunity where Contact__c = :cm.ContactID and Campaign.ID = :cm.CampaignId limit 1];
if(o.size() == 1){
if(cm.Cost__c != oldcm.Cost__c){o[0].Amount = cm.Cost__c;}
if(cm.Amount_Paid__c != oldcm.Amount_Paid__c){o[0].Amount_Paid__c = cm.Amount_Paid__c;}
if(cm.Balance_Due__c != oldcm.Balance_Due__c){o[0].Cvent_Balance_Due__c = cm.Balance_Due__c;}
if(cm.Status != oldcm.Status && cm.Status == 'Registered'){o[0].StageName = 'Closed Won';}
if(cm.Payment_Status__c != oldcm.Payment_Status__c && cm.Payment_Status__c == 'Paid in Full'){o[0].StageName = 'Closed Paid';}
if(cm.Status != oldcm.Status && (cm.Status == 'Cancelled' || cm.Status == 'Unattended' || cm.Status == 'Withdraw')){o[0].StageName = 'Closed Lost';}
Updopps.add(o[0]);
}
i++;
}
1) http://amitsalesforce.blogspot.com/2016/09/collection-in-salesforce-example-using.html
Please try below code.
Let us know if this will help you
All Answers
1) http://amitsalesforce.blogspot.com/2016/09/collection-in-salesforce-example-using.html
Please try below code.
Let us know if this will help you
Quick question on the following three lines of code.
for (CampaignMember cm: cms)
{
setContID.add(cm.ContactID);
setCampID.add(cm.CampaignId);
}
List<Opportunity> listOpp = [ Select ID, Contact__c, CloseDate, CampaignID, OE_Program_of_Interest__c, Amount, Cvent_Balance_Due__c, Amount_Paid__c, Description, StageName from Opportunity where Contact__c in :setContID and CampaignID in :setCampID ];
How do we make sure that the above soql is retrieving opportunity record for the right combination of contactid and campaignid?
I am getting a complie error - Expression must be a list type: Opportunity from the following line:
if(o != null )
If I change o to an opportunity list, then I get compile error - Illegal assignment from Opportunity to List<Opportunity> from the following line:
o = mapCampWiseOpp.get(key);
Greatly appreciate you help!!
Geetha
Please disregard the question about the logic. I understand now how you are doing it. I now have to figure out the error I am getting.
Thanks again!
Geetha