5

I'm learning basics of using Rest API and using workbench to explore it .

As of now , I'm correctly able to make get Call to salesforce for e.g

/services/data/v34.0/sobjects/Account/001K00000XXXXX

is bringing me details of the record mentioned above

Now I'm testing POST via Rest API To SFDC. This is the area where I'm having problems and doubts.

My first question.

as I'm trying to create Account record via post request , is there a standard similar way, like above(which is GEt request to retrieve data) to create record and get ID back via POST ?

I tried hard to search but what I got are Salesforce articles is : this can be done by creating your own rest resource(Custom class exposed as Rest Resource like in this way

@RestResource(URLMapping = '/SingleAccounInsert/*')

 Global class CreateSingleAccount {

 @httpPost
  Global static string  CreateAcc( string nm, string Descrip, String Type, String area){
  Account acc = new Account();
  acc.name = nm ;
  acc.Description = Descrip;
  acc.Type = Type;
  acc.Area__c= area;
  insert acc ;
  return acc.Id;
 } }

Second question :

Even If try to use above rest resource and post following data to SFDC on URL :

/services/apexrest/SingleAccounInsert/

    {
  "name" : "Sansui Corp",
  "description" : "Electronics Buys",
  "Type" : "XYZ",
  "Area__c" : "USA"
}

I Get below JSON PARSER ERROR

message: Unexpected parameter encountered during deserialization: name at [line:2, column:13] errorCode: JSON_PARSER_ERROR

I'm stuck now, not sure how to test POST Requests.

Articles referred :

https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex_rest_2.htm

https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex_rest_1.htm

8

Yes there is standard way to create any record in Salesforce via POST

/services/data/v34.0/sobjects/OBJECTNAME/

replace OBJECTNAME with object name for which you want to insert the record.

for example Account record can be created as follows

enter image description here

If you want to update account you need to pass Account id in url and select PATCH as method type

/services/data/v34.0/sobjects/ACCOUNT/RECORDID

JSON in body

{

"NAME":"Erase item"
}

enter image description here

  • Thanks , this actually works But have one question. If Suppose I've to update same account, should I've to use PATCH as POST is not working and getting following error : METHOD_NOT_ALLOWED errorCode: METHOD_NOT_ALLOWED message: HTTP Method 'POST' not allowed. Allowed are HEAD,GET,PATCH,DELETE. I tried to use PATCH also, through update is happening, but getting 204 as response – sfdc_Qrius Dec 13 '15 at 10:56
  • check the updated answer – Himanshu Dec 13 '15 at 16:58
6

You can use standard REST API as showed by above answer .Also i would like to point out mistake you are doing in the custom REST API

The JSON parameters that you send should match the parmeters of the @HTTP POST function of apex .Hence your proper JSON as per function you have written would be as below

 {
 "nm" : "Sansui Corp",
 "Descrip" : "Electronics Buys",
  "Type" : "XYZ",
 "area" : "USA"
}
  • Thanks Mohith for pointing out this mistake!! I could only mark only as answer so for now, could only give + 1 to your answer . – sfdc_Qrius Dec 13 '15 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.