Salesforce

An effort to bring a revolution in Salesforce.

Showing posts with label Rest API. Show all posts
Showing posts with label Rest API. Show all posts

Tuesday, 26 July 2016

difference between a normal wrapper and a REST API wrapper

20:30


Please find the difference between a normal wrapper and a REST API wrapper.


Wrapper Class : A Wrapper Class (or the Wrapper Pattern) is where you declare a Class as a container for an sObject to extend the functionality only for display or processing purposes (i.e. you don't intend for that attribute to be persisted) - the classic example is a checkbox to select records. I would say that a DTO is a slightly dumbed down version of such a wrapper (used in conventional OOP to pass structured data between the layers)

REST API Wrapper : A REST API wrapper is something slightly different. Salesforce exposes a REST API and if you were to invoke that say from C#, you would have to perform a set of common steps such as login, query, etc. To make this available in C# by abstracting the innards of the actual REST calls to salesforce and exposing only the developer relevant detail, you could write a rest wrapper which performs these commonly used functions - creates requests, parses responses, etc.

For use cases please refer to the below link.
https://www.godaddy.com/garage/webpro/wordpress/3-practical-use-cases-wp-rest-api/

Wednesday, 29 July 2015

How to Insert Account Using Rest API In Salesforce ?

23:36

 
@RestResource(urlMapping='/*')
global class InsertAccount
{
    @Httppost
    global static String InsertAccount()
    {  

        // Insert Account Using Post Method

        Account  acc = new Account();
        String jsonStr = null;
        if (null != RestContext.request.requestBody){
        
        jsonStr = RestContext.request.requestBody.toString();
        Map<String, object> m = (Map<String, object>)JSON.deserializeUntyped(jsonStr );
        system.debug('******'+m );
        acc.Name=(String)m.get('AccountName');
        insert acc;
       }
        return 'Account Inserted';
    }
    
    
     @HttpGet
    global static String InsertAccountRest()
    {   

               // Insert Account Using Get Method

        Account  acc1 = new Account();
        acc1.Name=RestContext.request.params.get('AccountName');
        insert acc1;
    
        return 'Account Inserted';
    }
}
 

> For Executing the REST API Please do following steps



  • Select your Environment and API Version then checked the terms of service
  • Then click on Login With Salesforce
 

 Using GetMethod

 
 
  

Using Post Method

 

How to use REST API in ? !! Salesforce !!

14:35

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
  • 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 )


Get Gmail, Docs, Drive, and Calendar for business