Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Access Setup, and search for "Apex" in the search box.

Classic and Lightning

We provide two examples here. 

The first example queries email Task objects from the Salesforce systemCreate a new class. Add the following code:

Code Block
titleSample Apex genericQuery
@RestResource(urlMapping='/jtelACD/genericQuerygetEmailTasks')
global with sharing class JTELACD_GenericQueryGetEmailTasks {
    // Class to decode the parameters
    public class GenericQueryParams {
        public String Param1;
        public String Param2;
        public String Param3;
        public String Param4;
        public String Query;
    }
    
    @HttpPost
    global static Stringvoid doPost() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        //
 Deserialize the JSON (not used in this example// -Deserialize just for illustration purposes)the JSON
        GenericQueryParams genericQueryParams = (GenericQueryParams) System.JSON.deserialize( req.requestBody.toString(), GenericQueryParams.class );
 
        List<Task> tasks = [
            SELECT FIELDS(STANDARD),
                   TYPEOF Who
                       WHEN Lead THEN FirstName, LastName, Email
                       WHEN Contact THEN FirstName, LastName, Email
                   END
            FROM Task
            WHERE Status = 'Inbound Email'
            WITH USER_MODE
            LIMIT 1
        ];
                          
 // Find next task to schedule in jtelif system.(tasks.size() > 0) {
        // Note - the exactres.statusCode logic= of200;
 this will be dependent on the Salesforce installation - this is JUSTres.responseBody AN EXAMPLE.= Blob.valueOf(JSON.serialize(tasks[0]));
            return;
        List<Task> tasks = [SELECT FIELDS(STANDARD),}

        res.statusCode = 404;
        res.responseBody = Blob.valueOf('{"error":"No task found"}');
    }
}

The second example sets Task objects to "In Progress" in Salesforce:

Code Block
titleSample Apex genericQuery
@RestResource(urlMapping='/jtelACD/setEmailTaskStatus')
global with sharing class JTELACD_SetEmailTaskStatus {

    // Class to decode the parameters
    public class GenericQueryParams {
        public String Param1;
        public String Param2;
        public TYPEOFString WhoParam3;
        public String Param4;
        public String Query;
    }

    @HttpPost
    global static void doPost() {

      WHEN Lead THENRestRequest FirstName,req LastName, Email= RestContext.request;
        RestResponse res = RestContext.response;

        res.addHeader('Content-Type', 'application/json');

        // Deserialize the JSON
        GenericQueryParams genericQueryParams WHEN= Contact THEN FirstName, LastName, Email(GenericQueryParams) JSON.deserialize( req.requestBody.toString(), GenericQueryParams.class );

        if (String.isBlank(genericQueryParams.Param1)) {
            res.statusCode = 400;
            ENDres.responseBody = Blob.valueOf('{"error":"Missing task id"}');
            return;
        }

         FROM Taskif (String.isBlank(genericQueryParams.Param2)) {
            res.statusCode = 400;
            WHERE Statusres.responseBody = 'Inbound Email'
Blob.valueOf('{"error":"Missing status"}');
            return;
        }

        WITH USER_MODE
List<Task> tasks = [
            SELECT FIELDS(STANDARD)
            FROM Task
         LIMIT 1];
  WHERE Id = :genericQueryParams.Param1
            LIMIT 1
        ];

        if ( tasks.sizeisEmpty() >= 0 ) {) {
            res.statusCode = 404;
            res.responseBody = Blob.valueOf('{"error":"Task not found"}');
            return;
        }

    return JSON.serialize(    Task task = tasks[0] );
;
        task.Status = genericQueryParams.Param2;

        update }task;

        res.statusCode = 404200;
        return nullres.responseBody = Blob.valueOf(JSON.serialize(task));
    }
}

CURL Test

First of all, obtain an OAUTH Token, see Testing with CURL.

...