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
plai_highwireplai_highwire 

executing apex via a custom button

I have a junction object called Predecessor Cases that has two lookup fields back to cases.  The two fields are called successor case and predecessor case.
I created a custom button that executes some apex code.  When i click on the custom button, nothing is happening.  Am I not passing the right info to execute the apex code?

Apex Class:
global class AdjustDate {
    WebService static void AdjustDate(Id CaseId){         
        Set<ID> sid = new Set<ID>();     
        Integer DaysToAdd = 1;
        //Get a list of all Predecessor objects that has a match on the  predecessor field to the case ID            
        List<Predecessor_Case__c> successor = [SELECT ID,Predecessor_Case__c, Successor_Case__c, Successor_case__r.id FROM Predecessor_Case__c WHERE Predecessor_Case__r.id =: CaseId ];
        //Add the successor case ids that match those predecessor cases to a set 
        for(Predecessor_Case__c pc : successor){
            sid.add(pc.Successor_Case__r.id);
        }
        //Get a list of all cases that match that of the sid
        List<Case> successcases = [SELECT ID, Estimated_Launch_Date__c, Days_To_Add__c FROM Case WHERE ID IN: sid];
        // Do the calculations for those cases
        for(Case c: successcases){            
            c.Estimated_Launch_Date__c += DaysToAdd;
            c.Actual_Approval_Date__c = date.today();
        }  
    }
}


Custom button:
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}


sforce.apex.execute(
"AdjustDate",
"adjustdate",
{id:"{!Case.Id}"})
Elie.RodrigueElie.Rodrigue
{id:"{!Case.Id}"})
The name of your parameter is CaseId not id

Your method name should be AdjustDate not adjustdate

I also assume that you put the button in a case layout not in a predecessor case layout. otherwise the {!Case.Id} wont work.

You could also update the api version to something newer just in case there has been some bug fix :


Custom button:
{!REQUIRESCRIPT("/soap/ajax/28.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/28.0/apex.js")}


sforce.apex.execute(
"AdjustDate",
"AdjustDate",
{CaseId:"{!Case.Id}"})


plai_highwireplai_highwire
Thanks Elie, I've made the adjustments you have suggested and still nothing happens when I click on the button. The button is on the case layout. Do you have any other suggestions? Thanks.
plai_highwireplai_highwire
Update: I made some adjustments to the class as well. Turned out I didn't have an update command to actual do any updating. *List updatecase = new List();* // Do the calculations for those cases for(Case c: successcases){ c.Estimated_Launch_Date__c += DaysToAdd; c.Actual_Approval_Date__c = date.today(); *updatecase.add(c); * } * update updatecase;* Thanks again Elie for taking a look at my problem.
Elie.RodrigueElie.Rodrigue
No problem