I'm still pretty new to this, and I've looked around and haven't been able to find the solution that combines everything like I'm trying to do. I've tried quite a few things, but here's where I stopped once I realized I was heading into an endless knowledge loop:
Main class:
global class SendtoExternalAPI {
@InvocableMethod(label= 'Send values to ExternalAPI' category='Intake__c' callout=true)
global static void ProcessIntakes(List<ID> intakeIds) {
List<Intake__c> intakes = [SELECT First_Name__c, Name__c, Email__c, Street_for_API__c, City_for_API__c, State_for_API__c, Zip_Code_for_API__c, Phone__c, Date_of_Birth_for_API__c FROM Intake__c WHERE Id in :intakeIds];
String FirstName;
String LastName;
String Email;
String Address1;
String City;
String State;
String Zip;
String Phone;
String DOB;
for (Intake__c intake : intakes) {
FirstName = intake.First_Name__c;
LastName = intake.Last_Name__c;
Email = intake.Email__c;
Address1 = intake.Street_for_API__c;
City = intake.City_for_API__c;
State = intake.State_for_API__c;
Zip = intake.Zip_Code_for_API__c;
Phone = intake.Phone__c;
DOB = intake.Date_of_Birth_for_API__c;
String username = 'username';
String password = 'nicetrySE';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC' + EncodingUtil.base64Encode(headerValue);
System.debug(FirstName);
System.debug(LastName);
System.debug(Email);
Http http = new Http();
HttpRequest request = new HttpRequest();
//Please set the endpoint of API gateway which we are going to create in later section
request.setEndpoint('https://www.ExternalAPI.com');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setHeader('Accept', 'application/json');
request.setHeader('Authorization', authorizationHeader);
request.setBody('{"SourceID":"SourceID", "ClientID" : "ClientID", "FirstName":"' + FirstName + '", "LastName":"' + LastName + '", "Address1":"' + Address1 + '" ,"City":"' + City + '","State":"' + State +'","Zip":"'+ Zip + '","Email":"' + Email + '","Phone":"'+ Phone + '","DOB":"'+ DOB + '"}');
HttpResponse response = http.send(request);
System.debug('response==='+response.getBody());
System.debug('request==='+request.getBody());
// 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());
}
}
}
}
Mock HTTPCallout API
@isTest global class MockHttpResponseGeneratorAPI implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
// Optionally, only send a mock response for a specific endpoint
// and method.
System.assertEquals('https://www.ExternalAPI.com', req.getEndpoint());
System.assertEquals('POST', req.getMethod());
// Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"First_Name__c":"Random", "Last_Name__c"="Person", "Email__c"="[email protected]", "Phone__c"="855-867-5309"}');
res.setStatusCode(200);
return res;
}
}
Test Class (promise I had gotten further than this, but my computer restart and undid the changes RIP):
@isTest private class SendExternalAPITest {
@isTest static void testIntake() {
Intake__c intake = new Intake__c(First_Name__c='Random', Last_Name__c='Person', Email__c='Email__c"="[email protected]', Phone__c='855-867-5309');
insert intake;
Test.startTest();
SendtoExternalAPI request = new SendtoExternalAPI();
Test.stopTest();
}
}