How to get Case Id of current case when button is clicked on case page?

I made a class that assigns cases to the correct person’s queue when they click a button. That button runs some javascript that executes a method in a global class. This button resides on the Case page layout. How can i get the case number of the current case using a query. I tried something like shown below, but it returns null. Is there a way to do this? Any thoughts? Thanks!

c = [SELECT Id FROM Case WHERE Id =: '{!Case.Id}'];

This is my Javascript:

{!REQUIRESCRIPT("/soap/ajax/27.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/27.0/apex.js")}
sforce.apex.execute("assignToMyQueue","AssignCaseMethod", {sUserID:"{!User.Id}"}, {caseId:'{!Case.Id}'});

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

While you can do queries from within your JavaScript, I recommend you simply pass the Id to your global class method and then do the query in the Apex code. This encapsulates more of your buttons logic in the server code as well, which is good best practice.

So your JavaScript button code would look something like this…

{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/26.0/apex.js")}
var result = sforce.apex.execute('CaseWebService', 'assignToMyQueue', {caseId:'{!Case.Id}', userId:'{!User.Id}'});
alert('Result is ' + result);

Your Apex code to retrieve the Case number would then be..

global with sharing class CaseWebService
{
    webService static String assignToMyQueue(ID caseId, ID userId)
    {
        Case caseRecord = [Select Id, CaseNumber from Case Where Id =: caseId];
        String caseNumber = caseRecord.CaseNumber;
        return 'Case Number is ' + caseNumber + ', User Id is ' + userId;
    }
}

This results in a Javascript popup showing the Case Number and User Id.

enter image description here


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x