How to use REST API in Salesforce ?
This is the Class For REST API
APEX Class
@RestResource(urlMapping=’/*’)global with sharing class LocationRESTAPI {
global class Location_Result {
DOuble latitude{get;set;}
Double longitude{get;set;}
string userLocation{get;set;}
Boolean result{get;set;}
}
@RestResource(urlMapping=’/setLocation’)
@httppost
global static void setLocationDetails(Decimal latitude, Decimal longitude,String conId) {
Boolean result = false;
RestRequest request = RestContext.request;
RestResponse response = RestContext.response;
system.debug(‘==latitude==>’+latitude‘longitude====>’+longitude);
try{
Contact con = new Contact();
con.Location__latitude__s = latitude;
con.Location__Longitude__s = longitude;
con.lastname = ‘test';
insert con;
result = true;
}catch(Exception e) {
result = false;
}
response.responseBody = Blob.valueOf(‘{ “result”:’+ result +’ }’);
return;
}
global static String getUserInfo(){
return UserInfo.getProfileId();
}
@RestResource(urlMapping=’/getLocation’)
@HttpGet
global static Location_Result getLocation(){
RestRequest request = RestContext.request;
RestResponse response = RestContext.response;
/* Location_Result location_Result = new Location_Result();
String contactId = request.params.get(‘ConId’);
system.debug(‘==contactId ==>’+contactId );
Contact con = new Contact(); */
String uId = userinfo.getUserId();
system.debug(‘===uId====>’+uId
User u = new User();
u = [Select Id,ContactId from User where id =: uid];
Location_Result location_Result = new Location_Result();
Contact con = new Contact();
try {
con = [SELECT Name,Title,Account.Name,OwnerId,Location__Longitude__s,Location__latitude__s FROM Contact WHERE id =: u.ContactId]; // Name =: contactName OR Account.Name =: companyName OR Title =: jobTitle limit 1];
location_Result.latitude = con.Location__latitude__s;
location_Result.longitude = con.Location__Longitude__s;
location_Result.result = true;
}catch(Exception e) {
location_Result.result = false;
}
return location_Result;
}
}
> For Executing the REST API Please do following steps
- Go to this Link > https://workbench.developerforce.com/login.php
- Select your Environment and API Version then checked the terms of service
- Then click on Login With Salesforce
- After the logged into Workbench you can Choose HTTP Method to perform on the REST API service, (Here I Choose GET Method and Fetch Longitude and Latitude From Contact Record).
- And also you can see the Raw Response of that particular record by clicking on SHOW RAW RESPONCE ( Here I already select that Link so it’s show me the HIDE RAW RESPONSE )
No comments:
Post a Comment