1

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.
  }

}
3

You can't hard code your Campaign Id in your InboundEmail class. This following line is a big problem:

 CampaignMember cm = new CampaignMember(campaignId='7010J000001RUj5', LeadId=lead.Id);
  1. It's not best practice to hard code record ID in code. You would better create a custom setting/metadata type to store this information.

  2. By default a test class run independently from your organisation data. That means, 7010J000001RUj5 doesn't exist as a campaign when you test class run. That's why you have a INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY error. You will need to create your campaign in your test class and populate that line dynamically.

  3. You can use real data in test class if really necessary by using @isTest(SeeAllData=true)

  • 1
    point 3 - if really really really really necessary - No reason to use seeAllData in this use case. I saw an earlier version of this OP and surmised there is a specific Campaign the app is trying to exploit - in which case the campaign name should be in a custom setting or custom metadata type - and the apex should query for that – cropredy Dec 9 '16 at 2:42
  • @cropredy I probably didn't insist enough on the "if necessary". ;-) – Cloud Ninja Dec 9 '16 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.