[SalesForce] pass set of IDs from javascript button to a web service

I need to create a custom button on the list view of a custom object. When user selects multiple records on the list view and I need to send those record IDs to be processed by a batch process (I chose batch process because user may possibly select lot of records from the list view.But if someone has other pointers on this design, feel free to comment).

After googling around, i figured this is what i have to do –
Button Click -> call a web service -> web service calls my batch process

My problem is with the first step. From the javascript button, i am not sure how to pass those record IDs to the web service. If i can pass it as a string or something, i can then do further manipulation in my batch process to covnert it to Set.

My javascript knowledge is limited to googling for less than 24 hrs, and i am a relatively new developer, so pardon me if these questions are conceptual. I did my homework best to my capabilities before posting question here.

Code for javascript button on the list view-

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
var records = {!GETRECORDIDS($ObjectType.Acquisition_POC__c)}; 
var result = sforce.apex.execute("acquisitionListViewDocusign","callAcquisitionBatch",{receivedAcquisitionIDSet : records});

Needless to say, that last line doesn't work. It throws errors.

Code for my web service

global with sharing class acquisitionListViewDocusign {
    webService static String callAcquisitionBatch(String receivedAcquisitionIDSet) {
    //If i can get the parameter properly, i can do more stuff, such as call my batch process
    return receivedAcquisitionIDSet;
    }
}

So how do i pass that javascript array (in the custom button) as a parameter to a webservice.

If anyone has a completely different design input, please let me know.

Best Answer

Thanks Keith. It worked. Now i feel stupid. It was something so simple. Overnight, i got it to work by using JSON.stringify() in the javascript button, and then deserializing it in my batch class. Guess i needed a good night sleep.

I will post my complete working code here just in case someone needs to look at it in future.

Javascript button on the list view

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

var records = {!GETRECORDIDS($ObjectType.Acquisition_POC__c)}; 

var result = sforce.apex.execute("acquisitionListViewDocusign","callAcquisitionBatch",{receivedAcqIDs : records});

window.location.reload(); 

Button calls a web service. Code for web service -

global with sharing class acquisitionListViewDocusign {


    webService static void callAcquisitionBatch(Id[] receivedAcqIDs) {

        Database.executeBatch(new Acquisition_POC_BatchClass(receivedAcqIDs), 100);
    }
}

Web service calls the batch job (as obvious from above code). Code for batch job-

global class Acquisition_POC_BatchClass Implements Database.Batchable<Sobject>{

    global final String Query;
    global final String DOCUSIGN_STATUS_COMPLETED = 'Completed';
    global final String DOCUSIGN_STATUS_SENT = 'Sent';
    global final String DOCUSIGN_STATUS_DELIVERED = 'Delivered';
    global List<Id> acquisitionIDs = new List<Id>();

    //constructor that is used by a scheduler apex to run this batch process at regular intervals
    global Acquisition_POC_BatchClass(){

        Query = 'SELECT ID,Template__c, Signer_Email__c, Signer__r.Name, Picked_up_by_batch_process__c, Docusign_Status__c '+
                'FROM Acquisition_POC__c '+        
                'WHERE  Docusign_Status__c != :DOCUSIGN_STATUS_COMPLETED AND Docusign_Status__c != :DOCUSIGN_STATUS_SENT AND Docusign_Status__c != :DOCUSIGN_STATUS_DELIVERED';
    }


    //overloaded constructor used by a button on the list view
    global Acquisition_POC_BatchClass(List<Id> receivedAcqIDs){


        this.acquisitionIDs = receivedAcqIDs;

        Query = 'SELECT ID,Template__c, Signer_Email__c, Signer__r.Name, Picked_up_by_batch_process__c, Docusign_Status__c '+
                'FROM Acquisition_POC__c '+        
                'WHERE  ID in : acquisitionIDs' ;

    }

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(Query);
    }

    global void execute(Database.BatchableContext BC,List<Acquisition_POC__c> listofAcquisitions){
        //call batch helper class to do whatever operation necessary
        Acquisition_POC_BatchHelper.processAcquisitions(listofAcquisitions);

    }

    global void finish(Database.BatchableContext BC){


    }

}

I would still like to ask again - to pass multiple records via list view button, into a batch process ..... is web service the only way? Is there any other way? Infact, from what i found through google, in order to call any class via javascript button, we have to go through web service. Is that true?

Related Topic