You need to sign in to do that
Don't have an account?
Michael M
Static method cannot be referenced from a non static context: void EMedCalloutsExtension.makePostCallout1(List<Id>)
Why am I getting this error: "Static method cannot be referenced from a non static context: void EMedCalloutsExtension.makePostCallout1(List<Id>)" ?
The error appears when I try to save this test class:
@isTest
private class EmedscalloutTest2 {
@isTest static void testCallout() {
date d = date.today();
lead l = new lead(firstname = 'test', lastname = 'test', patient_dob__c = d);
insert l;
list<string> refids = new list<string>();
refids.add(l.id);
// Set mock callout class
Test.setMock(HttpCalloutMock.class, new EmedsCalloutTest());
// Call method to test.
// This causes a fake response to be sent
// from the class that implements HttpCalloutMock.
EMedCalloutsExtension tw = new EMedCalloutsExtension();
tw.makePostCallout1(refids);
}
}
Here is my first test class/method:
@isTest
global class EmedsCalloutTest implements HttpCalloutMock {
// Implement this interface method
global HTTPResponse respond(HTTPRequest req) {
// Optionally, only send a mock response for a specific endpoint
// and method.
System.assertEquals('http://example.com/example/test', req.getEndpoint());
System.assertEquals('GET', req.getMethod());
// Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"example":"test"}');
res.setStatusCode(200);
return res;
}
}
and my actual code:
trigger ReferralCreateContact on Lead (after insert, after update) {
if(!Test.isRunningTest()) {
if (trigger.isInsert){
List<string> leadIds = new List<string>();
for (lead ref : trigger.new){
if(System.IsBatch() == false && System.isFuture() == false){
if (ref.Epaces_Checked__c != true){
lead newl = new lead();
newl.Id = ref.id;
leadIds.add(newl.id);
EMedCalloutsExtension.makePostCallout1(leadIds);
}
}}} }
public class EMedCalloutsExtension {
static string EmedrequestId;
static string requestStatus;
@future(callout = true)
public static void makePostCallout1(list<id> refIds) {
List <Lead> refs =[Select Id, firstname, lastname, gender__c, patient_dob__c, patient_ssn__c, Epaces_Checked__c, Emed_Request_Id__c, medicaid_id__c, medicare_id__c from Lead where Id in :refIds];
List<lead> refToUpdate = new List<Lead>();
for (lead ref : refs){
//Set up reassignment of static resource string items
String ssnReplace;
if (ref.Patient_SSN__c != null){
ssnReplace = ref.Patient_SSN__c.replace('-','');
}
String genderLetter;
if (ref.Gender__c == 'Male'){
genderLetter = 'M';
} else if (ref.Gender__c == 'Female'){
genderLetter = 'F';
}
String first;
String second;
String third;
String d = String.valueOf(ref.Patient_DOB__c);
system.debug('***dob string raw: ' + d);
first = d.substring(0,4);
second = d.substring(5,7);
third = d.substring(8,10);
String dob = first + second + third;
system.debug('***dob formatted: ' + dob);
//REQUEST #1
string reqbody;
StaticResource r =[Select Id,Body from StaticResource where Name='EMedCalloutBody' limit 1];
reqBody=r.body.toString();
// reqBody=reqBody.replace('{{FirstName}}', ref.firstname);
// reqBody=reqBody.replace('{{LastName}}', ref.lastname);
if (ref.Medicaid_ID__c != null){
// reqBody=reqBody.replace('{{MedicaidId}}', ref.medicaid_id__c);
reqBody = reqBody.replace('{{residentInfo}}', '{"first_name": "'+ ref.firstname +'", "last_name": "' + ref.lastname +'", "id_type":"MI", "subscriber_id":"' + ref.medicaid_id__c + '"}');
}
else if (ref.gender__c != null && ref.Patient_DOB__c != null && ref.Patient_SSN__c != null){
reqBody = reqBody.replace('{{residentInfo}}', '{"first_name": "'+ ref.firstname +'", "last_name": "' + ref.lastname +'", "gender":"' + genderLetter + '", "birthdate": "' + dob + '", "social_security_number": "' + ssnReplace + '"}');
}
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type', 'application/json');
req.setMethod('POST');
req.setBody(reqBody);
req.setEndpoint('callout:Emed');
req.setTimeout(2 * 60 * 1000);
system.debug('ENDPOINT: ' + req.getendpoint());
system.debug('BODY: '+ req.getBody());
Http http = new Http();
HttpResponse response = http.send(req);
if (response.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
system.debug(response.getstatuscode());
System.debug(response.getBody());
string requestId = response.getbody().substringAfter('"request_id": ').substringBefore(',');
// ref.Emed_Request_Id__c = requestId;
EmedrequestId = requestId;
string reqbodyResp;
StaticResource r2 =[Select Id,Body from StaticResource where Name='EmedResponseBody' limit 1];
reqbodyResp=r2.body.toString();
reqbodyResp=reqbodyResp.replace('{{requestId}}', EmedrequestId);
HttpRequest req2 = new HttpRequest();
req2.setHeader('Content-Type', 'application/json');
req2.setMethod('POST');
req2.setBody(reqbodyResp);
req2.setEndpoint('callout:Emed_Response');
req2.setTimeout(2 * 60 * 1000);
system.debug('ENDPOINT2: ' + req2.getendpoint());
system.debug('BODY2: '+ req2.getBody());
Http http2 = new Http();
HttpResponse response2 = http2.send(req2);
if (response2.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +
response2.getStatusCode() + ' ' + response2.getStatus());
} else {
system.debug(response2.getstatuscode());
System.debug(response2.getBody());
Id refId = ref.id;
// instantiate a new instance of the Queueable class
EmedFollowupCallout makeAnotherFollowUp = new EmedFollowupCallout(response2.getBody(), refId, requestId);
// enqueue the job for processing
ID jobID = System.enqueueJob(makeAnotherFollowUp);
}
}
return;
}
}
}
The error appears when I try to save this test class:
@isTest
private class EmedscalloutTest2 {
@isTest static void testCallout() {
date d = date.today();
lead l = new lead(firstname = 'test', lastname = 'test', patient_dob__c = d);
insert l;
list<string> refids = new list<string>();
refids.add(l.id);
// Set mock callout class
Test.setMock(HttpCalloutMock.class, new EmedsCalloutTest());
// Call method to test.
// This causes a fake response to be sent
// from the class that implements HttpCalloutMock.
EMedCalloutsExtension tw = new EMedCalloutsExtension();
tw.makePostCallout1(refids);
}
}
Here is my first test class/method:
@isTest
global class EmedsCalloutTest implements HttpCalloutMock {
// Implement this interface method
global HTTPResponse respond(HTTPRequest req) {
// Optionally, only send a mock response for a specific endpoint
// and method.
System.assertEquals('http://example.com/example/test', req.getEndpoint());
System.assertEquals('GET', req.getMethod());
// Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"example":"test"}');
res.setStatusCode(200);
return res;
}
}
and my actual code:
trigger ReferralCreateContact on Lead (after insert, after update) {
if(!Test.isRunningTest()) {
if (trigger.isInsert){
List<string> leadIds = new List<string>();
for (lead ref : trigger.new){
if(System.IsBatch() == false && System.isFuture() == false){
if (ref.Epaces_Checked__c != true){
lead newl = new lead();
newl.Id = ref.id;
leadIds.add(newl.id);
EMedCalloutsExtension.makePostCallout1(leadIds);
}
}}} }
public class EMedCalloutsExtension {
static string EmedrequestId;
static string requestStatus;
@future(callout = true)
public static void makePostCallout1(list<id> refIds) {
List <Lead> refs =[Select Id, firstname, lastname, gender__c, patient_dob__c, patient_ssn__c, Epaces_Checked__c, Emed_Request_Id__c, medicaid_id__c, medicare_id__c from Lead where Id in :refIds];
List<lead> refToUpdate = new List<Lead>();
for (lead ref : refs){
//Set up reassignment of static resource string items
String ssnReplace;
if (ref.Patient_SSN__c != null){
ssnReplace = ref.Patient_SSN__c.replace('-','');
}
String genderLetter;
if (ref.Gender__c == 'Male'){
genderLetter = 'M';
} else if (ref.Gender__c == 'Female'){
genderLetter = 'F';
}
String first;
String second;
String third;
String d = String.valueOf(ref.Patient_DOB__c);
system.debug('***dob string raw: ' + d);
first = d.substring(0,4);
second = d.substring(5,7);
third = d.substring(8,10);
String dob = first + second + third;
system.debug('***dob formatted: ' + dob);
//REQUEST #1
string reqbody;
StaticResource r =[Select Id,Body from StaticResource where Name='EMedCalloutBody' limit 1];
reqBody=r.body.toString();
// reqBody=reqBody.replace('{{FirstName}}', ref.firstname);
// reqBody=reqBody.replace('{{LastName}}', ref.lastname);
if (ref.Medicaid_ID__c != null){
// reqBody=reqBody.replace('{{MedicaidId}}', ref.medicaid_id__c);
reqBody = reqBody.replace('{{residentInfo}}', '{"first_name": "'+ ref.firstname +'", "last_name": "' + ref.lastname +'", "id_type":"MI", "subscriber_id":"' + ref.medicaid_id__c + '"}');
}
else if (ref.gender__c != null && ref.Patient_DOB__c != null && ref.Patient_SSN__c != null){
reqBody = reqBody.replace('{{residentInfo}}', '{"first_name": "'+ ref.firstname +'", "last_name": "' + ref.lastname +'", "gender":"' + genderLetter + '", "birthdate": "' + dob + '", "social_security_number": "' + ssnReplace + '"}');
}
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type', 'application/json');
req.setMethod('POST');
req.setBody(reqBody);
req.setEndpoint('callout:Emed');
req.setTimeout(2 * 60 * 1000);
system.debug('ENDPOINT: ' + req.getendpoint());
system.debug('BODY: '+ req.getBody());
Http http = new Http();
HttpResponse response = http.send(req);
if (response.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
system.debug(response.getstatuscode());
System.debug(response.getBody());
string requestId = response.getbody().substringAfter('"request_id": ').substringBefore(',');
// ref.Emed_Request_Id__c = requestId;
EmedrequestId = requestId;
string reqbodyResp;
StaticResource r2 =[Select Id,Body from StaticResource where Name='EmedResponseBody' limit 1];
reqbodyResp=r2.body.toString();
reqbodyResp=reqbodyResp.replace('{{requestId}}', EmedrequestId);
HttpRequest req2 = new HttpRequest();
req2.setHeader('Content-Type', 'application/json');
req2.setMethod('POST');
req2.setBody(reqbodyResp);
req2.setEndpoint('callout:Emed_Response');
req2.setTimeout(2 * 60 * 1000);
system.debug('ENDPOINT2: ' + req2.getendpoint());
system.debug('BODY2: '+ req2.getBody());
Http http2 = new Http();
HttpResponse response2 = http2.send(req2);
if (response2.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +
response2.getStatusCode() + ' ' + response2.getStatus());
} else {
system.debug(response2.getstatuscode());
System.debug(response2.getBody());
Id refId = ref.id;
// instantiate a new instance of the Queueable class
EmedFollowupCallout makeAnotherFollowUp = new EmedFollowupCallout(response2.getBody(), refId, requestId);
// enqueue the job for processing
ID jobID = System.enqueueJob(makeAnotherFollowUp);
}
}
return;
}
}
}
Instead of below code:
EMedCalloutsExtension tw = new EMedCalloutsExtension();
tw.makePostCallout1(refids);
Write below code:
EMedCalloutsExtension.makePostCallout1(refids);
All Answers
Instead of below code:
EMedCalloutsExtension tw = new EMedCalloutsExtension();
tw.makePostCallout1(refids);
Write below code:
EMedCalloutsExtension.makePostCallout1(refids);