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
ManojKumar MuthuManojKumar Muthu 

Apex Code for customer Rest API


Hi,
I am trying to list all case created to particular account here the parameter is the AccountId, below the apex code,

@RestResource(urlMapping='/CaseAccountid/*')
global with sharing class CaseAccountid{

@HttpGet
    global static List<Case> getCaseById() {
       RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String emailAddress = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
          List<case> result =  [Select id, subject, status, Version__c, type, priority, AccountId, OwnerId, CSM_ID__c, ContactEmail from Case where AccountId = :ID];
        return result;
    }
}

I get Error: Compile Error: Variable does not exist: AccountId at line 9 column 170

Can someone help me on this asap.

Thanks in advance.

 
Best Answer chosen by ManojKumar Muthu
PawanKumarPawanKumar
I tried below code, I am able to save. You also try and see. Changes are in bold.

@RestResource(urlMapping='/CaseAccountid/*')
global with sharing class CaseAccountid{

@HttpGet
    global static List<Case> getCaseById() {
       RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String ID= req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
          List<case> result =  [Select id, subject, AccountId, OwnerId, ContactEmail from Case where AccountId = :ID];
        return result;
    }
}

Please mark it best if it helps you. Thanks in advance.

Regards,
Pawan Kumar

All Answers

PawanKumarPawanKumar
I tried below code, I am able to save. You also try and see. Changes are in bold.

@RestResource(urlMapping='/CaseAccountid/*')
global with sharing class CaseAccountid{

@HttpGet
    global static List<Case> getCaseById() {
       RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String ID= req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
          List<case> result =  [Select id, subject, AccountId, OwnerId, ContactEmail from Case where AccountId = :ID];
        return result;
    }
}

Please mark it best if it helps you. Thanks in advance.

Regards,
Pawan Kumar
This was selected as the best answer
ManojKumar MuthuManojKumar Muthu
HI PawanKumar,

thanks for the timely help, and again thanks a ton.
ManojKumar MuthuManojKumar Muthu
Hi Pawan,

Same like the above, now I am trying to see all notes related to a Case, but it returns no value,

below the code,

RestResource(urlMapping='/NotesListView/*')
global with sharing class NotesListView{

@HttpGet
    global static List<Note> getNoteById() {
       RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String Id= req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
          List<Note> result = [Select Id, Title, Body from Note where ParentId= :Id];
        return result;
    }
}

In partnerid I pass caseid
PawanKumarPawanKumar
Please post as a separate questions. Plz.
ManojKumar MuthuManojKumar Muthu
Hi Pawan, 

Kindly find the below Link

https://developer.salesforce.com/forums/ForumsMain?id=9060G0000005hVrQAI#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=9060G0000005iaYQAQ