To create a new Apex class, access Setup in SalesForce, and type in "Apex" in the quick search box. Select "Build ... Develop ... Apex Classes". ![](/download/attachments/327943/image2018-3-16%2016%3A55%3A53.png?version=1&modificationDate=1521215754000&api=v2)
Access Setup, and search for "Apex" in the search box. ![](/download/thumbnails/327943/image2019-3-16_18-14-41.png?version=1&modificationDate=1552752910000&api=v2)
Create a new class. Add the following code:
Translations Ignore |
---|
Code Block |
---|
language | java |
---|
title | Sample Apex getCallerInformation |
---|
| @RestResource(urlMapping='/jtelACD/getCallerInformation')
global with sharing class JTELACD_GetCallerInformation {
@HttpGet
global static String doGet() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
// This is how to access URLParams if required
// List<String> URLParams = req.requestURI.split('/');
// Example: ["","jtelACD","getCallerInformation"]
// Extract the request parameters
String CallerID = req.params.get('CallerID');
String ServiceNumber = req.params.get('ServiceNumber');
String ServiceName = req.params.get('ServiceName');
// From release 3.25 - 2021-07-02 and upwards
// Perform SOSL Query over contacts and leads,String searchingFunction for the phone number with *wildcards*= req.params.get('Function');
String soslSearchStringUserData = '*' + CallerID + '*'req.params.get('UserData');
List<List<SObject>>String searchListUserData2 = [FIND :soslSearchString IN ALL FIELDS
req.params.get('UserData2');
String UserData3 = req.params.get('UserData3');
// Specifying a Function is intended to allow the developer to perform different types of query.
// As an RETURNINGexample,
we just return the data back if a function is specified.
// In a real world case, a query for example to search for and return a ticket, contract or some other record would be Contact,performed.
if( Function != null && Function != '' ) {
JSONGenerator gen = JSON.createGenerator( true );
Lead];gen.writeStartObject();
gen.writeStringField( 'CallerID', CallerID );
List<Contact> contacts = ( gen.writeStringField(List<Contact>) searchList[0] 'ServiceNumber', ServiceNumber );
List<Lead> leads = ( (List<Lead>) searchList[1]gen.writeStringField( 'ServiceName', ServiceName );
gen.writeStringField( 'Function', Function );
// Multple hits?
if( contactsgen.sizewriteStringField() + leads.size() > 1 ) 'UserData', UserData );
{
gen.writeStringField( 'UserData2', // Multiple hitsUserData2 );
res.statusCode = 300gen.writeStringField( 'UserData3', UserData3 );
return nullgen.writeEndObject();
}
return gen.getAsString();
if( contacts.size() > 0 )
}
{
// Perform SOSL Query over contacts and // Queryleads, searching for exactthe contact
phone number with *wildcards*
String IdsoslSearchString = (String) contacts.get(0).get('Id')'*' + CallerID + '*';
List<List<SObject>> searchList = [FIND Set<String>:soslSearchString fieldSetIN = schema.describeSObjects(new String[] { 'Contact' } )[0].fields.getMap().keyset();
ALL FIELDS
List<String> fieldList = new List<String>( fieldSet );
String query = 'SELECT ' + String.join( fieldList, ',' ) + ' FROM Contact WHERE Id = \'' + Id + '\'';
RETURNING
Contact contact = Database.query( query );
//Contact,
200 OK will be returned by default
return JSON.serialize( contact );
}
Lead]; if( leads.size() > 0 )
{
List<Contact> contacts = ( // Query for exact lead
(List<Contact>) searchList[0] );
List<Lead> leads String Id = (String (List<Lead>) leads.get(0).get('Id')searchList[1] );
Set<String> fieldSet = schema.describeSObjects(new String[] { 'Lead' } )[0].fields.getMap().keyset(); // Multple hits?
List<String> fieldList = new List<String>( fieldSet );
if( contacts.size() + leads.size() > 1 )
{
String query = 'SELECT ' + String.join( fieldList, ',' ) + ' FROM Lead WHERE Id = \'' + Id + '\'' // Multiple hits
res.statusCode = 300;
Lead lead = Database.query( query )return null;
}
// 200 OK will be returned by default
return JSON.serialize( lead );if( contacts.size() > 0 )
}
{
// Nothing found
// Query for exact contact
res.statusCode String Id = 404(String) contacts.get(0).get('Id');
return null;
Set<String> fieldSet = }
} |
|
First of all, obtain an OAUTH Token, see Testing with CURL. schema.describeSObjects(new String[] { 'Contact' } )[0].fields.getMap().keyset();
List<String> fieldList = new List<String>( fieldSet );
String query = 'SELECT ' + String.join( fieldList, ',' ) + ' FROM Contact WHERE Id = \'' + Id + '\'';
Contact contact = Database.query( query );
// 200 OK will be returned by default
return JSON.serialize( contact );
}
if( leads.size() > 0 )
{
// Query for exact lead
String Id = (String) leads.get(0).get('Id');
Set<String> fieldSet = schema.describeSObjects(new String[] { 'Lead' } )[0].fields.getMap().keyset();
List<String> fieldList = new List<String>( fieldSet );
String query = 'SELECT ' + String.join( fieldList, ',' ) + ' FROM Lead WHERE Id = \'' + Id + '\'';
Lead lead = Database.query( query );
// 200 OK will be returned by default
return JSON.serialize( lead );
}
// Nothing found
res.statusCode = 404;
return null;
}
} |
|
First of all, obtain an OAUTH Token, see Testing with CURL. The following CURL command can be used to test this API: Translations Ignore |
---|
Code Block |
---|
title | CURL - getCallerInformation Test |
---|
| curl.exe --silent -i -X GET --header "Authorization: Bearer <OAUTH_TOKEN>" --header "Connection: Close" "https://<SALES_FORCE_INSTANCE_URL>/services/apexrest/jtelACD/getCallerInformation?CallerID=4989461495000&ServiceNumber=4980012345678&ServiceName=test" |
|
The following CURL command can be used to test this API with the additional parameters from Release 3.25 - 2021-07-02 and upwards: Translations Ignore |
---|
Code Block |
---|
title | CURL - getCallerInformation Test |
---|
| curl.exe --silent -i -X GET --header "Authorization: Bearer <OAUTH_TOKEN>" --header "Connection: Close" "https://<SALES_FORCE_INSTANCE_URL>/services/apexrest/jtelACD/getCallerInformation?CallerID=4989461495000&ServiceNumber=4980012345678&ServiceName=test&Function=testFunction&UserData=testUserData&UserData2=testUserData2&UserData3=testUserData3" |
|
|