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
enossirenossir 

Send field data over API Callout

https://trailhead.salesforce.com/content/learn/modules/apex_integration_services/apex_integration_rest_callouts?trail_id=force_com_dev_intermediate

Looking at this....i see you can hardcode text to send to an external service but i need to send field data.

Basically i have a lightning component, as a button. User clicks the button and it takes a field from the current record and is supposed to send that to a third party service......So instead of sending 
 
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody('{"name":"mighty moose"}');
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() != 201) {
    System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
} else {
    System.debug(response.getBody());
}

Straight text like it does here, how do i send the text of a field. 
GovindarajGovindaraj
Hi Enossir,

We can form the json and include that in the setBody.
Public String caseDetails(string strName) {
	Case caseObj = [Select Status ,Priority from Case WHERE Name =: strName Limit 1] ;
    /START : Form JSON */
	JSONGenerator gen = JSON.createGenerator(true);    
	gen.writeStartObject();      
	gen.writeStringField('Case Status',caseObj.Status);
	gen.writeStringField('Case Priority',caseObj.Priority);
	gen.writeEndObject();    
	String jsonS = gen.getAsString();
    /STOP : Form JSON */

	Http http = new Http();
	HttpRequest request = new HttpRequest();
		request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
		request.setMethod('POST');
		request.setHeader('Content-Type', 'application/json;charset=UTF-8');
		//Include that string here
		request.setBody(jsonS);
	HttpResponse response = http.send(request);
		if (response.getStatusCode() != 201) {
    			System.debug('The status code returned was not expected: ' +
        		response.getStatusCode() + ' ' + response.getStatus());
			} else {
    					System.debug(response.getBody());
				}
}
Thanks,
Govindaraj.S