You need to sign in to do that
Don't have an account?
Sammy Shk
Test class for HTTP post Rest
Hello,
I'm new in Apex, I'm finding it difficult to write test class for Http Post, I'm sending some values from Salesforce to external system however I'm not sure how to write test class for this scenario. I did created test records but don't know how to add Http post Test class.
Can someone help me how to add Http post test class in the test class which i created?
I'm new in Apex, I'm finding it difficult to write test class for Http Post, I'm sending some values from Salesforce to external system however I'm not sure how to write test class for this scenario. I did created test records but don't know how to add Http post Test class.
Can someone help me how to add Http post test class in the test class which i created?
public class RadarUpdate { @future (callout=true) public static void postcallout(string Id) { Patient_Satisfaction__c c = [select id, Name, Reporter_Phone__c,Reporter_First_Name__c,Reporter_Last_Name__c, Reporter_Email__c, Description_of_Feedback__c from Patient_Satisfaction__c where Patient_Relation__c ='Referred to Privacy Office' order by lastmodifiedDate desc limit 1]; JSONGenerator gen = JSON.createGenerator(true); gen.writeStartObject(); gen.writeObjectField('name', c.Name); gen.writeObjectField('incident_group_id', 7387); gen.writeObjectField('description',c.Description_of_Feedback__c); gen.writeFieldName('submitted_by'); gen.writeStartObject(); gen.writeStringField('given_name',c.Reporter_First_Name__c); gen.writeStringField('surname', c.Reporter_Last_Name__c); gen.writeStringField('phone',c.Reporter_Phone__c); gen.writeStringField('email',c.Reporter_Email__c); gen.writeEndObject(); String jsonS = gen.getAsString(); System.debug('jsonMaterials'+jsonS); Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint('https://api.radarfirst.com/incidents'); request.setMethod('POST'); request.setHeader('Content-Type','application/json;charset=UTF-8'); request.setHeader('User-agent', 'Salesforce-integration-client'); request.setHeader('Authorization','Bearer 123'); request.setBody(jsonS); // Set the body as a JSON object 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()); } } }
@isTest private class RadarUpdateTest { @isTest public static void CreatePR(){ rkl__RK_Hierarchy_Node__c Hr = new rkl__RK_Hierarchy_Node__c(); Hr.Name= 'Hieararchy'; Hr.rkl__Node_Level__c = 1.0; Hr.rkl__Node_Name__c = 'Node Name'; insert Hr; Account ac = new Account(); ac.Name= 'Test'; insert ac; Contact con= new Contact(); con.AccountId= ac.Id; con.FirstName ='First Name'; con.LastName = 'Test Contact'; con.Phone= '7896541233'; con.Email= 'Test@123.com'; con.RUSH_Email__c= ''; insert con; Patient_Satisfaction__c Pr = new Patient_Satisfaction__c(); Pr.Primary_Campus__c = Hr.Id; Pr.Primary_Facility__c= Hr.Id; Pr.Primary_Location__c= Hr.Id; Pr.Reporter__c=con.Id; Pr.Reporter_Phone__c=con.Id; Pr.Reporter_Email__c=con.Id; Pr.Description_of_Feedback__c= 'Testing Privacy office'; Pr.Patient_Relation__c = 'Referred to Privacy Office'; insert Pr; update Pr; } }
Greetings!
Please find the sample test class code to cover the http request in the below document:
https://developer.salesforce.com/forums/?id=9060G000000XdDoQAK
https://salesforce.stackexchange.com/questions/185163/a-testclass-for-restresource-httppost
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Warm Regards,
Shirisha Pathuri