Standard Page Buttons To Run Apex Class (not a webservice class)
How do you solve the following requirement, there are lot of ways to do it but we feel this is best for our case.
The Requirement: When user clicks on "Close All Cases" custom button on Account Detail Page, It should update status of all the child case records.
Fortunately we already have a class that updates the case status to "Closed". all we need to so is call that class.
Our Solution: Have a custom field, a small trigger to call that class.
Step1: Create a custom checkbox "closecases".
Step 2: Create a custom detail page button "Close All The Cases" and show it on the page layout
with the following code:
================================================
a.id = "{!Account.Id}";
a.closecases__c=true;
var result = sforce.connection.update([a]);
// alert(result);
window.location.reload();
================================================
Step 3: Create a simple trigger when the value changes from false to true (only).
================================================
trigger triggeredfrombuttonclosecases on Account (after update) {
string oldbox;
string newbox;
// get the old box value
for(account old: trigger.old){
oldbox= old.closecases__c;
}
//get the updated value
for(account newt: trigger.new){
newbox= newt.closecases__c;
}
// when both are not equal call the method in the class. so that it wont be triggered all the time.
if(oldbox!=newbox && newbox==true){
bulkcases.updateAll(trigger.new[0].id)
}
}
================================================
Step 4. Create a WFR on case, when the new case is created update the parent field closecases__c to false.
So its complete without having to expose the existing functional class as a webservice class or create a new webservice class. pretty resourceful isnt it?
This solution can also be used to invoke classes like approvalprocesses, or so...
Please notice that this is 4 step process compared to "sforce.connection.execute", a single line.
As an alternative; you could create a webservice class instead of trigger to call the apex class, but I prefer to keep it in the same box instead of increasing the calls count unnecessarily.
HTH
Prabhan.
The Requirement: When user clicks on "Close All Cases" custom button on Account Detail Page, It should update status of all the child case records.
Fortunately we already have a class that updates the case status to "Closed". all we need to so is call that class.
Our Solution: Have a custom field, a small trigger to call that class.
Step1: Create a custom checkbox "closecases".
Step 2: Create a custom detail page button "Close All The Cases" and show it on the page layout
with the following code:
================================================
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}
var a = new sforce.SObject("Account");a.id = "{!Account.Id}";
a.closecases__c=true;
var result = sforce.connection.update([a]);
// alert(result);
window.location.reload();
================================================
Step 3: Create a simple trigger when the value changes from false to true (only).
================================================
trigger triggeredfrombuttonclosecases on Account (after update) {
string oldbox;
string newbox;
// get the old box value
for(account old: trigger.old){
oldbox= old.closecases__c;
}
//get the updated value
for(account newt: trigger.new){
newbox= newt.closecases__c;
}
// when both are not equal call the method in the class. so that it wont be triggered all the time.
if(oldbox!=newbox && newbox==true){
bulkcases.updateAll(trigger.new[0].id)
}
}
================================================
Step 4. Create a WFR on case, when the new case is created update the parent field closecases__c to false.
So its complete without having to expose the existing functional class as a webservice class or create a new webservice class. pretty resourceful isnt it?
This solution can also be used to invoke classes like approvalprocesses, or so...
Please notice that this is 4 step process compared to "sforce.connection.execute", a single line.
As an alternative; you could create a webservice class instead of trigger to call the apex class, but I prefer to keep it in the same box instead of increasing the calls count unnecessarily.
HTH
Prabhan.
Comments
Post a Comment