Having issue and recieving this error when trying to get my test class to work.
System.DmlException: Upsert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: 7010J000001RUj5: []
Class.EmailToLeadWilmslow.handleInboundEmail: line 30, column 1 Class.EmailToLeadTest.it_should_execute_assignment_rules: line 48, column 1
EmailToLeadWilmslow
global class EmailToLeadWilmslow implements Messaging.InboundEmailHandler {
/**
* Handling Inbound Email
* @param Messaging.InboundEmail Email object
* @param Messaging.InboundEnvelope Email enveloppe
* @return Messaging.InboundEmailResult
* @access global
*/
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
Lead lead = new Lead();
lead.FirstName = email.fromname.substring(0, email.fromname.indexOf(' '));
lead.LastName = email.fromname.substring(email.fromname.indexOf(' '));
lead.Description = email.plainTextBody;
String textBody = email.plainTextBody;
String companyName;
Integer startPos = textBody.indexOf('CompanyName:');
if (textBody.indexOf('CompanyName:') > -1) {
Integer endPos = textBody.indexOf('.', textBody.indexOf('CompanyName:'));
companyName = textBody.substring(startPos + 'CompanyName:'.length(), endPos);
}
lead.Company = companyName != NULL ? companyName : '(not provided)';
lead.LeadSource = 'Rightmove - Canford';
lead.Status = 'New';
upsert lead;
CampaignMember cm = new CampaignMember(campaignId='7010J000001RUj5', LeadId=lead.Id);
upsert cm;
LeadService.executeAssignmentAsync(new Set<Id>{lead.Id});
return result;
}
}
EmailToLeadTest
@isTest
private class EmailToLeadTest {
@isTest
static private void it_should_execute_assignment_rules()
{
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
//Create the email body
email.plainTextBody = 'This should become a note';
email.fromAddress = 'test@test.com';
email.fromName = 'John Doe';
String contactEmail = 'jsmith@salesforce.com';
email.subject = 'Dummy Account Name 123';
//create instance of class
EmailToLead e = new EmailToLead();
EmailToLeadCanford eCa = new EmailToLeadCanford();
EmailToLeadWilmslow eW = new EmailToLeadWilmslow();
Lead l = new Lead(
FirstName = 'John',
LastName = 'Doe',
Description = 'Test Description',
LeadSource = 'Rightmove',
Status = 'New'
);
upsert l;
Test.startTest();
Messaging.InboundEmailResult resultE = e.handleInboundEmail(email, envelope);
Messaging.InboundEmailResult resultEw = eW.handleInboundEmail(email, envelope);
Messaging.InboundEmailResult resultEca = eCa.handleInboundEmail(email, envelope);
LeadService.executeAssignmentAsync(new Set<Id>{l.Id});
Test.stopTest();
System.assert (resultE.success, 'InboundEmailResult returned a failure message');
System.assert (resultEw.success, 'InboundEmailResult returned a failure message');
System.assert (resultEca.success, 'InboundEmailResult returned a failure message');
// Since allTest is disabled, it is impossible to execute Assignment Rules.
}
}