我正试图整合两个Salesforce组织。如何从其他Salesforce组织的apex代码中传递方法的httppost参数,请找到它尝试过的以下代码,但我的状态为400 badrequest。 GET方法工作正常,如何从其他组织传递名称参数?
源组织的映射类:
// The @RestResource annotation is used at the class level and enables you to expose an Apex class as a REST resource.
// more details -> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_rest_resource.htm
@RestResource(urlMapping='/v1/getContact/*')
global with sharing class getContact
{
@Httpget
global static list<contact> fetchContact()
{
RestRequest req = RestContext.request;
RestResponse res = Restcontext.response;
//Get input conId from the request string.
Id conId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
// Get the list of contacts by passing input conId. In this case, it will return only 1 record.
list<contact> lstcontact =[Select id,Name,Email,MobilePhone from contact where id=:conId ];
// return back the contact list to calling system.
return lstcontact ;
}
@HttpPost
global static string createNewContact(String name)
{
Contact c=new Contact();
c.LastName=name;
insert c;
return 'contact sucessfully created';
}
@HttpDelete
global static string deleteContact()
{
RestRequest req=RestContext.request;
String ids=req.params.get('Id');
List<Contact> c=[select id from contact where id=:ids];
delete c;
return 'contact deleted successfully';
}
@Httpput
global static string putContact(String ids,String Name)
{
Contact c=[select id from contact where id=:ids];
c.LastName=Name;
update c;
return 'contact updated successfully';
}
}
目标组织的控制器类:
public class apexintegration {
public string getContactid {get;set;}
public List<contact> outputlst {get;set;}
public List<Contact> contact {get; set;}
public string JSONResponse {get;set;}
private final string clientid='xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
private final string clientsecret='xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
private final string username='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
private final string password='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
public class deserializeResponse
{
public string id;
public string access_token;
}
public string returnAccesstoken(apexintegration a)
{
String reqBody='grant_type=password&client_id='+clientid+'&client_secret='+clientsecret+'&username='+username+'&password='+password;
Http h=new http();
HttpRequest req=new HttpRequest();
req.setBody(reqBody);
req.setMethod('POST');
req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
HttpResponse res=h.send(req);
deserializeResponse resp1=(deserializeResponse)JSON.deserialize(res.getBody(), deserializeResponse.class);
return resp1.access_token;
}
public void getContact()
{
apexintegration a=new apexintegration();
String accesstoken;
accesstoken=a.returnAccesstoken(a);
if(accesstoken!=null)
{
String endPoint='https://curious-wolf-71m22l-dev-ed.my.salesforce.com/services/apexrest/v1/getContact/';
Http h2 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization','Bearer ' + accessToken);
req1.setHeader('Content-Type','application/json');
req1.setHeader('accept','application/json');
req1.setMethod('GET');
req1.setEndpoint(endPoint);
HttpResponse res1 = h2.send(req1);
// store raw JSON response
JSONResponse = res1.getBody().unescapeCsv().remove('\\');
contact = (List<Contact>) JSON.deserialize(JSONResponse, List<Contact>.class);
}
outputlst= contact;
}
public void createContact()
{
apexintegration a=new apexintegration();
String accesstoken;
accesstoken=a.returnAccesstoken(a);
if(accesstoken!=null)
{
String endPoint='https://curious-wolf-71m22l-dev-ed.my.salesforce.com/services/apexrest/v1/getContact/';
String body= 'name=saivinaymnprm';
Http h2 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization','Bearer ' + accessToken);
req1.setHeader('Content-Type','application/json');
req1.setHeader('accept','application/json');
req1.setMethod('POST');
req1.setBody(body);
req1.setEndpoint(endPoint);
HttpResponse res1 = h2.send(req1);
JSONResponse = res1.getBody().unescapeCsv().remove('\\');
String s=(String)JSON.deserialize(JSONResponse, String.class);
System.debug(s);
}
}
}