I have the following Apex Rest Endpoint that is working from remote systems.
@RestResource(urlMapping='/EmailOpens/*')
global with sharing class RsmEmailOpens {
// inbound JSON payload is an array/List named "opens" of this type
global class CnameEmail {
public String cname;
public String email;
}
// outbound response type
global class Response {
public String message;
public Integer updates;
public String cname;
public String email;
public String exception_msg;
}
@HttpPatch
global static Response RsmIncrementEmailOpens(List<CnameEmail> opens) {
Response response = new Response();
Integer updates = 0;
String error_msg = '';
for (CnameEmail i : opens) {
try {
error_msg = 'Lead.Email not found'; // message if the following fails
Lead lead = [SELECT Id FROM Lead WHERE Email =: i.email];
error_msg = 'Lead.Email found; Campaign.Name not found';
Campaign campaign = [SELECT Id FROM Campaign WHERE Name =: i.cname];
error_msg = 'Lead.Email & Campaign.Name found; CampaignMember with (Campaign,Lead) pair not found';
CampaignMember cm = [SELECT rsm_EmailOpenCount__c FROM CampaignMember
WHERE CampaignId =: campaign.Id AND LeadId =: lead.Id];
if(cm.rsm_EmailOpenCount__c == null) cm.rsm_EmailOpenCount__c = 0;
cm.rsm_EmailOpenCount__c += 1;
update cm;
} catch(Exception e) {
response.message = error_msg;
response.updates = updates;
response.cname = i.cname;
response.email = i.email;
response.exception_msg = e.getMessage();
return response;
}
updates++;
}
response.message = 'Successful';
response.updates = updates;
response.cname = Null;
response.email = Null;
response.exception_msg = Null;
return response;
}
}
I've also started a test case for this below. I'm having problems passing the JSON string into a parameter to RsmIncrementEmailOpens(). If I can figure this out, I believe I will have a complete solution.
@istest
private class RsmEmailOpensTestClass {
@testSetup static void setup() {
List<Lead> leads = new List<Lead>{
new Lead(Company = 'Unknown', LastName = 'Mike', Email = '[email protected]'),
new Lead(Company = 'Unknown', LastName = 'John', Email = '[email protected]')
};
insert leads;
Campaign c = new Campaign(Name = 'Test Devotion');
insert c;
List<CampaignMember> cm = new List<CampaignMember>{
new CampaignMember(CampaignId = c.Id, LeadId = leads[0].Id, rsm_EmailOpenCount__c = 100),
new CampaignMember(CampaignId = c.Id, LeadId = leads[1].Id, rsm_EmailOpenCount__c = 200)
};
insert cm;
}
@isTest static void testEmailOpen() {
String JSONMsg = '{"opens":[{"cname":"Test Devotion","email":"[email protected]"},{"cname":"Test Devotion","email":"[email protected]"}]}';
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/EmailOpens/';
req.httpMethod = 'PATCH';
req.requestBody = Blob.valueof(JSONMsg);
RestContext.request = req;
RestContext.response= res;
Test.startTest();
// I understand that for unit testing the method should be called directly
// I am receiving a Type Mismatch on the call below
// How can I correct this?
//RsmEmailOpens.RsmIncrementEmailOpens(JSONMsg);
Test.stopTest();
}
}
Thank you for your help.