You need to sign in to do that
Don't have an account?
CW83
Test Code Help!
We had a developer write an apex plugin to use in a flow but he didn't write any test for code and now my overall code coverage is less than 75% so I can't deploy anything via a changeset. Below is the code. Any help is appreciated.
global class mLSendEmail implements Process.Plugin {
global Process.PluginResult invoke(Process.PluginRequest request) {
// Get the subject of the Chatter post from the flowgj
//String emailS = (String) request.inputParameters.get('emailListString');
String campaignId = (String) request.inputParameters.get('campaignId');
String tempId = (String) request.inputParameters.get('emailTempId');
//system.debug(idS);
String userIdVar = UserInfo.getUserId();
//List<String> emailList = emailS.split(', ');
User userObject = [SELECT Name, Email
FROM User
WHERE Id = :userIdVar.left(15)
LIMIT 1]; //(String) request.inputParameters.get('userName');
List<CampaignMember> test = [SELECT ContactId, CampaignId
FROM CampaignMember
WHERE CampaignId = :campaignId];
List<Contact> contacts = [SELECT Id, Name, Email
FROM Contact
WHERE Id IN (SELECT ContactId
FROM CampaignMember
WHERE CampaignId = :campaignId AND ContactId != null)
];
List<Lead> leads = [SELECT Id, Name, Email
FROM Lead
WHERE Id IN (SELECT LeadId
FROM CampaignMember
WHERE CampaignId = :campaignId AND LeadId != null)
];
system.debug('EMAIL TEMPLATE ID: ' + tempId);
system.debug('VALUE OF CONTACTS: ' + contacts);
List<Messaging.SingleEmailMessage> mails =
new List<Messaging.SingleEmailMessage>();
if (!contacts.isEmpty()) {
system.debug('mLSendEmail.cls - contacts != null');
for (integer i = 0; i <= contacts.size()-1; i++) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(tempId);
mail.setReplyTo(userObject.Email);
mail.setSenderDisplayName(userObject.Name);
mail.setTargetObjectId(contacts.get(i).Id);
mails.add(mail);
}
} else if (!leads.isEmpty()) {
system.debug('mLSendEmail.cls - leads != null');
for (integer i = 0; i <= leads.size()-1; i++) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(tempId);
mail.setReplyTo(userObject.Email);
mail.setSenderDisplayName(userObject.Name);
mail.setTargetObjectId(leads.get(i).Id);
mails.add(mail);
}
} else {
system.debug('mLSendEmail.cls - else');
}
Messaging.sendEmail(mails);
// return to Flow
Map<String,Object> result = new Map<String,Object>();
return new Process.PluginResult(result);
}
global Process.PluginDescribeResult describe() {
Process.PluginDescribeResult result = new Process.PluginDescribeResult();
result.Name = 'Marketing Email Plugin';
result.Tag = 'ML Email';
result.inputParameters = new
List<Process.PluginDescribeResult.InputParameter>{
new Process.PluginDescribeResult.InputParameter('emailTempId',
Process.PluginDescribeResult.ParameterType.STRING, true),
new Process.PluginDescribeResult.InputParameter('campaignId',
Process.PluginDescribeResult.ParameterType.STRING, true)
};
result.outputParameters = new
List<Process.PluginDescribeResult.OutputParameter>{ };
return result;
}
}
global class mLSendEmail implements Process.Plugin {
global Process.PluginResult invoke(Process.PluginRequest request) {
// Get the subject of the Chatter post from the flowgj
//String emailS = (String) request.inputParameters.get('emailListString');
String campaignId = (String) request.inputParameters.get('campaignId');
String tempId = (String) request.inputParameters.get('emailTempId');
//system.debug(idS);
String userIdVar = UserInfo.getUserId();
//List<String> emailList = emailS.split(', ');
User userObject = [SELECT Name, Email
FROM User
WHERE Id = :userIdVar.left(15)
LIMIT 1]; //(String) request.inputParameters.get('userName');
List<CampaignMember> test = [SELECT ContactId, CampaignId
FROM CampaignMember
WHERE CampaignId = :campaignId];
List<Contact> contacts = [SELECT Id, Name, Email
FROM Contact
WHERE Id IN (SELECT ContactId
FROM CampaignMember
WHERE CampaignId = :campaignId AND ContactId != null)
];
List<Lead> leads = [SELECT Id, Name, Email
FROM Lead
WHERE Id IN (SELECT LeadId
FROM CampaignMember
WHERE CampaignId = :campaignId AND LeadId != null)
];
system.debug('EMAIL TEMPLATE ID: ' + tempId);
system.debug('VALUE OF CONTACTS: ' + contacts);
List<Messaging.SingleEmailMessage> mails =
new List<Messaging.SingleEmailMessage>();
if (!contacts.isEmpty()) {
system.debug('mLSendEmail.cls - contacts != null');
for (integer i = 0; i <= contacts.size()-1; i++) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(tempId);
mail.setReplyTo(userObject.Email);
mail.setSenderDisplayName(userObject.Name);
mail.setTargetObjectId(contacts.get(i).Id);
mails.add(mail);
}
} else if (!leads.isEmpty()) {
system.debug('mLSendEmail.cls - leads != null');
for (integer i = 0; i <= leads.size()-1; i++) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(tempId);
mail.setReplyTo(userObject.Email);
mail.setSenderDisplayName(userObject.Name);
mail.setTargetObjectId(leads.get(i).Id);
mails.add(mail);
}
} else {
system.debug('mLSendEmail.cls - else');
}
Messaging.sendEmail(mails);
// return to Flow
Map<String,Object> result = new Map<String,Object>();
return new Process.PluginResult(result);
}
global Process.PluginDescribeResult describe() {
Process.PluginDescribeResult result = new Process.PluginDescribeResult();
result.Name = 'Marketing Email Plugin';
result.Tag = 'ML Email';
result.inputParameters = new
List<Process.PluginDescribeResult.InputParameter>{
new Process.PluginDescribeResult.InputParameter('emailTempId',
Process.PluginDescribeResult.ParameterType.STRING, true),
new Process.PluginDescribeResult.InputParameter('campaignId',
Process.PluginDescribeResult.ParameterType.STRING, true)
};
result.outputParameters = new
List<Process.PluginDescribeResult.OutputParameter>{ };
return result;
}
}
All Answers
Please use below test class. Before you use modify on line 15 ( mention record id of email tempate )
If you face any issues please let me know.
Please try below code ( As I dont have your code base so I can not validate the syntax)
Seems line 10 is getting the inactive user. can you please replace the line #10 with below and see if this brings in active user and it works well.
User userObject = [SELECT Name, Email FROM User Where IsActive=true LIMIT 1];