1

I am looking to update all of the related Campaign Members' Statuses from a Campaign based on the execution of a JavaScript button and an APEX Class. The button below works to update the status from 'Added' to 'Sent'; however, I hit the DML governor limits for a query where campaign members with an 'added' status is >200.

Javascript button

{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")} 
var id= '{!Campaign.Id}'; 
alert('Updating Campaign Members...'); 
sforce.apex.execute('updatesentcampmembers','updateRecord',{recId :id}); 
window.location.href = window.location.href;

Apex Class

global class updatesentcampmembers {
    webservice static void updateRecord(Id recId){
        List campmember = [SELECT id, status FROM CampaignMember WHERE CampaignId =:recId and status='Added'];
        for (CampaignMember campm : campmember){
        campm.status='Sent';
        }
        update campmember;
    }
}

I believe the problem lies with the APEX class. I was trying to convert it using Best Practice #6: Querying Large Data Sets seems to throw me an error that 'campm' doesn't exist.

Failed bulkify class

global class updatesentcampmembers {
    webservice static void updateRecord(Id recId){
        for (List campm : [SELECT id, status FROM CampaignMember WHERE CampaignId =:recId and status='Added']){
        campaignmember.status='distributed';
        }
        update campm;
    }
}

Note: The final script would have additional IF statements only when certain criteria is met within the campaign so a marketer does not accidentally execute it.

As you may have guessed, I'm pretty new to the development scene. Thanks for any help!

Your Answer

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

Browse other questions tagged or ask your own question.