0

I’m trying to integrate two salesforce orgs. How to pass httppost parameters of method from other salesforce org in apex code please find the below codes it tried but I am getting status 400 badrequest. GET method is working fine, How to pass name parameter from other org?

Mapped class of source org:

 // 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';
        }
    }

Controller class of destination org:

    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);
}

}

3

The only thing you need to do is retrieving body of the post request, this can be done like in the example below:

@HttpPost
global static void processPost() {
    Map<String, Object> requestBody = (Map<String, Object>) JSON.deserializeUntyped(RestContext.request.requestBody.toString());
    //do something with the resulting map - this is the body of the POST request
}

And about request body in the destination organization, you need to pass it like this:

String body = JSON.serialize(new Map<String, Object>{
        'name' => 'some_name'
});

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.