function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Cloud EliteCloud Elite 

global action button to call batch job

how to add a global action or button, that by clicking on it, it will start the job batch ? 

here is the code to start the job in console 

Id batchJobId = Database.executeBatch(new FindAccounts(), 100);

 
Best Answer chosen by Cloud Elite
Narender Singh(Nads)Narender Singh(Nads)
In that case use the old code:
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

var result = sforce.apex.execute("ValidateBefore","validate",{});

If this doesn't work  share your JS button code and the class ValidateBefore.

All Answers

Narender Singh(Nads)Narender Singh(Nads)
Hi,
Unfortunately it is not possible to call a batch class from Global/Quick action. However you can create a custom buttom(Javascript button) which you can use to call your class.

It will look something like this:
Apex Class:

​global with sharing class ValidateBefore {
webService static String validate(String param) {
      Database.executeBatch(new CreateOrderBatch(),1); // this is your call
}

Custom Button:

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

var result = sforce.apex.execute("ValidateBefore","validate",{"{!Account.Name}"});
if(result == 'OK') {
window.location = '/{!Account.Id}';
} else {
alert(result);
}
Cloud EliteCloud Elite
i am not able to add that class. i want to add the button on the lead object and be able to execute findaccounts class. whast am i missing ? 
Narender Singh(Nads)Narender Singh(Nads)
You will have to create a class(say ABC) and then class your batch apex method in that class.
Now call your ABC class in the javascript button.
Cloud EliteCloud Elite
batch job class label is findaccounts 

if i create a new class called calljob  how you will put that together ? in the button ? 
Narender Singh(Nads)Narender Singh(Nads)
Take a look at the code snippet I sent you.  
In that code, there is a class called ValidateBefore with a method called validate. In this validate method, the batch apex is called.

Now in your Javascript button, we write:

sforce.apex.execute("ValidateBefore","validate",{"{!Account.Name}"});

Syntax is:
sforce.apex.execute("YOUR CLASS NAME", "YOUR METHOD NAME",PASS YOUR PARAMETERS IN JSON);
Narender Singh(Nads)Narender Singh(Nads)
Important point
Make sure your method is of webservice type.
Apex class methods can be exposed as custom SOAP Web service calls. This allows an external application to invoke an Apex Web service to perform an action in Salesforce. 
 
Cloud EliteCloud Elite
it does not allow me to create the class i get errors 

global with sharing class ValidateBefore {
webService static String validate(String param) {
      Database.executeBatch(new FindAccounts(), 100);
}
 
Narender Singh(Nads)Narender Singh(Nads)
can you tell me what errors you are getting
Cloud EliteCloud Elite
User-added image
Narender Singh(Nads)Narender Singh(Nads)
Ok.

You this code:
global with sharing class validatebefore{

    webService static String validate(){
        id batchprocessid=Database.executeBatch(new FindAccounts(), 100);
        return batchprocessid;
    }
}
And in JS button, use this line:
 sforce.apex.execute("ValidateBefore","validate",{});
Narender Singh(Nads)Narender Singh(Nads)
Use this code*
Cloud EliteCloud Elite
User-added image
Narender Singh(Nads)Narender Singh(Nads)
Kindly share your JS button code.
Narender Singh(Nads)Narender Singh(Nads)
Also, what I would like to know is do you have namespace enabled for your org?
 
Cloud EliteCloud Elite
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

var result = sforce.apex.execute("ValidateBefore","validate",{});
thats the code i used in the button. 

and yes we do use namespace. 

thanks Narender 
 
Narender Singh(Nads)Narender Singh(Nads)
I see! 
In that case, use this code:
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

var result = sforce.apex.execute("PUT_YOUR_NAMESPACE_HERE.ValidateBefore","validate",{});
Also, please make sure that 'ValidateBefore' class is declared as global and 'validate' method is prefixed with webservice keyword.
Cloud EliteCloud Elite
I am getting the same error message. We do have domain name not name space thats after salesforce lightening force it on us. Like this https://companyNAME--dev1.cs70.my.salesforce.com
Narender Singh(Nads)Narender Singh(Nads)
In that case use the old code:
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

var result = sforce.apex.execute("ValidateBefore","validate",{});

If this doesn't work  share your JS button code and the class ValidateBefore.
This was selected as the best answer
Cloud EliteCloud Elite
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var result = sforce.apex.execute("ValidateBefore","validate",{});
 
global with sharing class validatebefore{
    webService static String validate(){
        id batchprocessid=Database.executeBatch(new lead_FindAccounts(), 100);
        return batchprocessid;
    }
}

 
Narender Singh(Nads)Narender Singh(Nads)
Hi,
Are you still getting the same error?

If yes then try changing the API version to 43 like this:

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

Let me know if it helps
Cloud EliteCloud Elite
still not working. 
Narender Singh(Nads)Narender Singh(Nads)
Hi,
I have a emailed you. Kindly check your mail.

Thanks