0

Currently I am passing one parameter into batch apex and querying the results. But, now i need to pass comma separated parameters and then i need to query records from all these passed parameters.

This is how i am doing currently.

public class getCampaignRecords implements Database.Batchable<sObject>{
    public String CampaignNumber;

    //Constructor
    public getCampaignRecords(String campNumber){
        CampaignNumber = campNumber;


    select Id, Name, LeadId, ContactID FROM CampaignMember WHERE Campaign.Campaign_Number__c=\''+CampaignNumber+'\'';

But how am i supposed to pass comma separated parameters and query the results

like this

public class getCampaignRecords implements Database.Batchable<sObject>{
    public String CampaignNumber;
public String CampaignNumber1;
public String CampaignNumber2;
public String CampaignNumber3;

    //Constructor
    public getCampaignRecords (String campNumber,String campNumber1,String campNumber2,String campNumber3){
        CampaignNumber = campNumber;`
    CampaignNumber1=campNumber1;
    CampaignNumber2=campNumber2;
    CampaignNumber3=campNumber3;
2

Preferred Solution

You should supply a collection, such as a Set<String> or List<String> containing your desired Campaign Numbers and perform the query against that collection using an Apex bind expression:

private Set<String> campaignNumbers;
public getCampaignRecords(Set<String> campaignNumbers) {
    this.campaignNumbers = campaignNumbers;
}

and then query in your start() method:

return Database.getQueryLocator([
    SELECT Id, Name, LeadId, ContactID FROM CampaignMember WHERE Campaign.Campaign_Number__c IN :campaignNumbers
]);

Literal Solution

If you're being told that the constructor must accept multiple String parameters rather than a collection, just build the collection yourself and apply the same solution. (I would call this requirement a mistake, myself).

public getCampaignRecords(String first, String second, String third) {
    this.campaignNumbers = new Set<String>{first, second, third};
}
4
  • Hello David, Thanks for your quick reply. but requirement is to pass comma separated values. What could be the best approach if we want to do this using comma separated values – Amit Rajput Apr 21 '19 at 9:54
  • @AmitRajput That requirement doesn't make sense. What do you mean by "comma separated values"? A string like "A,B,C" or multiple String variables? – David Reed Apr 21 '19 at 9:55
  • The requirement is to pass campaignnumber like this campaignnumber1,campaignnumber2,cmampaignnuber3 – Amit Rajput Apr 21 '19 at 10:07
  • Just split the input string then trim the individual values as you add them to the Set<String> that David suggested. developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – Phil W Apr 21 '19 at 10:24

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.