function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
farukh sk hdfarukh sk hd 

create a record using rest api error.

Can somebody help me over here,I am getting error and I am not able to create a record using rest api from one salesforce org to other.

Here is my code,

public void getConList()
{

list<account> accList1=new list<account>();
String accToken;
string responseBody;
string endPoint='https://ap5.salesforce.com/services/apexrest/getAccountOnExternalId';
restApiClassSalesforceorg1 obj=new restApiClassSalesforceorg1();
accToken=obj.getRequestToken();
system.debug('access token'+ accToken);
if(accToken!='')
{
String accName1='Test from org 1 to org 2';
string Jsonstring='{"Name":"Acc from org 1"}';
Http h1=new Http();
HttpRequest req1=new HttpRequest();
req1.setHeader('Authorization','Bearer '+accToken);
req1.setHeader('Content-Type','application/json');
//req1.setHeader('accept','application/json');
req1.setMethod('POST');
req1.setBody(Jsonstring);
req1.setEndpoint(endPoint);
HttpResponse hresp1=h1.send(req1);
system.debug('hresp1'+ hresp1);
//listWrap=(list<resultWrapper>) JSON.deserialize(hresp1.getBody(),list<resultWrapper>.class);

}

// return listWrap;

}





@RestResource(urlMapping='/getAccountOnExternalId/*')
   global with sharing class getAccount {
     @HttpPost
      global Static string fetchAccount(string name1){
      Account obj=new account();
      obj.name=name1;
      Insert obj;
        
        return 'Success';
      }
   }
Best Answer chosen by farukh sk hd
Tikam.SangwaniTikam.Sangwani
Hi @faruk, The parameter name of fetchAccount method should be same as you are passing key in JSON Request.

For above code either change the request from  {"Name":"Acc from org 1"} to {"name1":"Acc from org 1"}

or 

change parameter name in fetchAccount method as Name:
global Static string fetchAccount(string Name)

Please mark this as best answer if it helps you . 

Best Regards 
Tikam Sangwani

All Answers

Tikam.SangwaniTikam.Sangwani
Hi @faruk, Can you please share what error you are getting.
farukh sk hdfarukh sk hd
System.HttpResponse[Status=Bad Request, StatusCode=400]
Tikam.SangwaniTikam.Sangwani
Hi @faruk, The parameter name of fetchAccount method should be same as you are passing key in JSON Request.

For above code either change the request from  {"Name":"Acc from org 1"} to {"name1":"Acc from org 1"}

or 

change parameter name in fetchAccount method as Name:
global Static string fetchAccount(string Name)

Please mark this as best answer if it helps you . 

Best Regards 
Tikam Sangwani
This was selected as the best answer
farukh sk hdfarukh sk hd
Great it works thanks for your help.