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
SFDC GuestSFDC Guest 

apex rest outbound class for rest inbound class

Hi, I am trying to write the apex rest outbound program for below apex inbound rest service. Can you please help me on writing the outbound apex rest service. Thanks..

Apex inbound service:

@RestResource(urlMapping='/RestServiceContactCreation/*')

global class RestServiceContactCreation{
    global class ContactRequestDetails{
        webservice String contactLastName;
    }
    
    global class ContactResponseDetails{
        webservice String contactLastName;
        webservice String contactCreationStatus;
    }
    
    global static ContactResponseDetails insertContactDetails(ContactRequestDetails contRequest){
        ContactResponseDetails contResp = new ContactResponseDetails();
            Contact contactObject = new Contact();
            contactObject.LastName = contRequest.contactLastName;
            try
            {
                insert contactObject;
                contResp.contactCreationStatus = 'Contact is created successfully....!!!!! Contact Id: ' +contactObject.Id;
                return contResp;
                }
            catch(Exception ex){
                String errorMessage = 'Error occurred' +ex.getMessage();
                contResp.contactCreationStatus = errorMessage;
                return contResp;
            }
    }
    
    @HttpPost    
    global static ContactResponseDetails createContact(ContactRequestDetails req){
        ContactResponseDetails res = insertContactDetails(req);
        return res;
    }
}

Apex rest outbound:

public class ContactCreationRestservice_Outbound{

    public String response{get;set;}
    
    public class ContactRequestDetails{
        String contactLastName;
    }
    
    public PageReference CreateContact() {
        ContactRequestDetails contReq = new ContactRequestDetails();
        //find access token using Auth 2.0 
        String Access_Token='access token';

        Httprequest req=new httprequest();
        String domainName= '<domainName>';
        String endPointURL='https://domainName-dev-ed.my.salesforce.com/services/apexrest/RestServiceContactCreation/';
        req.setendpoint(endPointURL);
        req.setHeader('Content-Type', 'application/xml; charset=utf-8');
        req.setBody('<?xml version="1.0" encoding="UTF-8" ?><request><name>'+contactLastName+'</name></request>');
        req.setmethod('POST');
        
        req.setHeader('Authorization','Authorization: Bearer '+Access_Token);
        Http http = new Http();
        HTTPResponse res = http.send(req);
        response=res.getbody();
        System.debug('****************res.getStatusCode();'+res.getStatusCode());
        System.debug('****************res.getbody();'+res.getbody());
        return null;
    }
}
NagendraNagendra (Salesforce Developers) 
Hi,

Note that its best to build the request body using the DOM Classes https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_xml_dom.htm because they will take care of any necessary escaping. The code can also end up cleaner too.
I'm guessing here, but based on what happens for the JSON case (where the field names are used), the XML you need to send is probably either:
<?xml version="1.0" encoding="UTF-8" ?> <req><contactLastName>Smith</contactLastName></req>
or:
<?xml version="1.0" encoding="UTF-8" ?> <contactLastName>Smith</contactLastName>
Hope this helps.

Kindly mark this as solved if this is resolved.

Thanks,
Nagendra