Thursday, 2 June 2016

Insert | Update | Delete record by Rest API | Workbench | Standard Rest API | Rest Explorer


In this blog we will learn how to insert, update or delete a record by Rest API without any code.

1) Create a record

Method :- Post
URL:- /services/data/v36.0/sobjects/Account/
Request Body :- 

1{
  "Name" : "Account from Rest API",
  "phone" : "1111111111",
  "website" : "www.salesforce1.com",
  "numberOfEmployees" : "100",
  "industry" : "Banking"
}



Response 
1{ "id" : "0019000001hE8apAAC", "success" : true, "errors" : [ ] }



2) Update a record

Method :- Patch
URL:- /services/data/v36.0/sobjects/Account/0019000001hE8apAAC
Request Body :- 

1{
  "Name" : "Account from Rest API",
  "phone" : "222222"
}



Response :-
 1HTTP/1.1 204 No Content







3) Delete a record

Method :- Delete
URL:- /services/data/v36.0/sobjects/Account/0019000001hE8apAAC

Response 
 1HTTP/1.1 204 No Content



4) Retrieve a record

Method :- Get
URL:- /services/data/v36.0/sobjects/Account/0019000001hE8af
Response 
 1{ "attributes" : { "type" : "Account", "url" : "/services/data/v36.0/sobjects/Account/0019000001hE8afAAC" }, "Id" : "0019000001hE8afAAC", "IsDeleted" : false, "MasterRecordId" : null, "Name" : "Express Logistics and Transport", "Type" : null, "ParentId" : null, "BillingStreet" : null, "BillingCity" : null, "BillingState" : null, "BillingPostalCode" : null, "BillingCountry" : null, "BillingLatitude" : null, "BillingLongitude" : null, "BillingGeocodeAccuracy" : null, "BillingAddress" : null, "ShippingStreet" : null, "ShippingCity" : null, "ShippingState" : null, "ShippingPostalCode" : null, "ShippingCountry" : null, "ShippingLatitude" : null, "ShippingLongitude" : null, "ShippingGeocodeAccuracy" : null, "ShippingAddress" : null, "Phone" : "1111111111", "Fax" : null, "AccountNumber" : null, "Website" : "www.salesforce1.com", "PhotoUrl" : "/services/images/photo/0019000001hE8afAAC", "Sic" : null, "Industry" : "Banking", "AnnualRevenue" : null, "NumberOfEmployees" : 100, "Ownership" : null, "TickerSymbol" : null, "Description" : null, "Rating" : null, "Site" : null, "OwnerId" : "00590000003XIfkAAG", "CreatedDate" : "2016-06-02T13:04:35.000+0000", "CreatedById" : "00590000003XIfkAAG", "LastModifiedDate" : "2016-06-02T13:04:35.000+0000", "LastModifiedById" : "00590000003XIfkAAG", "SystemModstamp" : "2016-06-02T13:04:35.000+0000", "LastActivityDate" : null, "LastViewedDate" : "2016-06-02T13:14:38.000+0000", "LastReferencedDate" : "2016-06-02T13:14:38.000+0000", "IsPartner" : false, "IsCustomerPortal" : false, "Jigsaw" : null, "JigsawCompanyId" : null, "CleanStatus" : "Pending", "AccountSource" : null, "DunsNumber" : null, "Tradestyle" : null, "NaicsCode" : null, "NaicsDesc" : null, "YearStarted" : null, "SicDesc" : null, "DandbCompanyId" : null, "CustomerPriority__c" : null, "SLA__c" : null, "Active__c" : null, "NumberofLocations__c" : null, "UpsellOpportunity__c" : null, "SLASerialNumber__c" : null, "SLAExpirationDate__c" : null, "Accountant__c" : null, "First_Level_Approval__c" : false, "Final_Approval__c" : false, "Old_Account_ID__c" : null, "Demo__c" : null, "SumOfOppAmount__c" : null }



5) Create multiple record

Method :- post
URL:- /services/data/v34.0/composite/tree/Account/
Request Body :- 

1{
"records" :[{
    "attributes" : {"type" : "Account", "referenceId" : "ref1"},
    "name" : "SampleAccount1",
    "phone" : "1111111111",
    "website" : "www.salesforce1.com",
    "numberOfEmployees" : "100",
    "industry" : "Banking"  
    },{
    "attributes" : {"type" : "Account", "referenceId" : "ref2"},
    "name" : "SampleAccount2",
    "phone" : "2222222222",
    "website" : "www.salesforce2.com",
    "numberOfEmployees" : "250",
    "industry" : "Banking"
    },{
    "attributes" : {"type" : "Account", "referenceId" : "ref3"},
    "name" : "SampleAccount3",
    "phone" : "3333333333",
    "website" : "www.salesforce3.com",
    "numberOfEmployees" : "52000",
    "industry" : "Banking"
    },{
    "attributes" : {"type" : "Account", "referenceId" : "ref4"},
    "name" : "SampleAccount4",
    "phone" : "4444444444",
    "website" : "www.salesforce4.com",
    "numberOfEmployees" : "2500",
    "industry" : "Banking"
    }]
}



Response :-
 1{ "hasErrors" : false, "results" : [ { "referenceId" : "ref1", "id" : "0019000001hE8gOAAS" }, { "referenceId" : "ref2", "id" : "0019000001hE8gPAAS" }, { "referenceId" : "ref3", "id" : "0019000001hE8gQAAS" }, { "referenceId" : "ref4", "id" : "0019000001hE8gRAAS" } ] }



6) Update multiple record.
Method :- post
URL:- /services/data/v34.0/composite/batch
Request Body :- 

1{
"batchRequests" : [
    {
        "method" : "PATCH",
        "url" : "v34.0/sobjects/account/0019000001hE8gOAAS",
        "richInput" : { "name" : "SampleAccount1",
                        "phone" : "1111111117",
                        "website" : "www.salesforce1.com",
                        "numberOfEmployees" : "100",
                        "industry" : "Banking"
                        }
    },
   
    {
        "method" : "PATCH",
        "url" : "v34.0/sobjects/account/0019000001hE8gPAAS",
        "richInput" : { "name" : "SampleAccount2",
                        "phone" : "2222222227",
                        "website" : "www.salesforce1.com",
                        "numberOfEmployees" : "100",
                        "industry" : "Banking"
                        }
    }
    ]
}



Response :-
 1{ "hasErrors" : false, "results" : [ { "statusCode" : 204, "result" : null }, { "statusCode" : 204, "result" : null } ] }



NOTE:- Please replace id in URL according to your org

Please check below post for more information
1) https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_what_is_rest_api.htm



Let us know if this will help you

<<PREVIOUS       NEXT>>


Thanks
Amit Chaudhary

8 comments:

  1. Thank you so much sir.... Its very helpful for us.

    ReplyDelete
  2. getting error
    message: Cannot deserialize instance of datetime from VALUE_STRING value 2015-12-01 02:45 PM or request may be missing a required field at [line:1, column:25]
    errorCode: JSON_PARSER_ERROR

    ReplyDelete
    Replies
    1. Date Time field should be in the "yyyy-mm-ddTmm:ss:sssz" format.

      Delete
  3. Is there any way that we can update an existing record with POST method in salesforce instead of patch?

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. The article discussing SAP ECC end of support in 2027 provides valuable insights into the implications for businesses still using the platform. It highlights the urgency for organizations to transition to SAP S/4HANA to maintain security and functionality. The comprehensive breakdown of the timelines and considerations for migration offers a clear roadmap for those navigating this transition. Overall, this article serves as a vital resource for companies planning their strategy in light of the upcoming end of support for SAP ECC.

    ReplyDelete
  7. Nice post. Thank you to provide us this useful information.

    ReplyDelete