0

I am trying to update the code of a Contact Trigger handler using the Force.com IDE. However, deploying does not work because of an error in an unchanged 3rd-party class.

Even if I uncomment all changes I get the following error on deployment in the Force.com IDE:

Test Results:

Run Failures: TelemarketingControllerTest.testNormalFlow System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TargetX_Email.CampaignTrigger1: execution of AfterInsert

caused by: TargetX_Email.SecurityCheck.CRUDException: User does not have access to update Campaign

Class.TargetX_Email.SecurityCheck.crudError: line 73, column 1 Class.TargetX_Email.SecurityCheck.checkCRUD: line 60, column 1 Class.TargetX_Email.SecurityCheck.checkCRUD: line 43, column 1 Class.TargetX_Email.SecurityCheck.checkBoth: line 20, column 1 Class.TargetX_Email.SecurityCheck: line 10, column 1 Class.TargetX_Email.deleteFutureChildBroadcasts: line 99, column 1 Trigger.TargetX_Email.CampaignTrigger1: line 21, column 1: []
   Average test coverage across all Apex Classes and Triggers is 72%, at least 75% test coverage is required.

Somehow the test is triggering their code, it looks like.

And this is the offending line 31

@isTest
private class TelemarketingControllerTest {

     static testMethod void testNormalFlow(){
        //campaign
        Campaign testCampaign       = new Campaign();
        testCampaign.name           = 'TestCampaign';
        testCampaign.CallerTimeout__c = 'Null';
        testCampaign.Callbacks__c = '2 Callbacks';
        insert testCampaign; // << Line 31

I see the warning about the code coverage. But I guess the real problem is "User does not have access to update Campaign"

What can I do to avoid the problem and be able to deploy some changes to my Contact Trigger handler?

  • 2
    Does the User running the test has Marketing User permission set? – Jayant Das Dec 12 '18 at 19:03
  • You can always modify or make a new user with specific criteria inside a test. – battery.cord Dec 12 '18 at 19:05
  • @JayantDas nope, the force.com ide is connected using a SysAdmin User. I add it and try again. – wp78de Dec 12 '18 at 19:07
  • @wp78de For creating, updating Campaign records, the User needs to have Marketing User permission, check by adding it to the Sys Admin User you are trying with. That is most likely your issue here. – Jayant Das Dec 12 '18 at 19:08
  • 1
    @JayantDas you got it right! The deployment plan executed successfully. Thanks so much! – wp78de Dec 12 '18 at 19:16
3

Rolling up from comment.

In case you are trying to create/update Campaigns, you need to make sure the User has Marketing User permission set. In this case, the User did not have the permission and thus was not able to create a Campaign record.

Refer to Who has access to campaigns? documentation which explains this behavior.

Depending on your sharing settings, users in your org can view campaigns, view the advanced campaign setup, or run campaign reports. However, only designated marketing users with the appropriate user permissions can create, edit, and delete campaigns and configure advanced campaign setup. An administrator must select the Marketing User option on a user’s personal information to designate that user as a marketing user.

| improve this answer | |
  • You're welcome. Also take a look at Pranay's answer on fine tuning your test class, as how you can create test users within your test class so that it does not depend on manually performing this step. – Jayant Das Dec 12 '18 at 21:31
3

You should not depend on real users's permission to test the block of code that needs a special permission.

Ideally you should create your own test user with marketing permission(UserPermissionsMarketingUser) to alter campaigns and campaign memebers. You can do that using System.runAs

@isTest
private class TelemarketingControllerTest {

     static testMethod void testNormalFlow(){
        //campaign

         // Setup test data
        // This code runs as the system user
        Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User'];
        User u = new User(Alias = 'standt', Email = 'standarduser@testorg.com',
                EmailEncodingKey = 'UTF-8', LastName = 'Testing', LanguageLocaleKey = 'en_US',
                LocaleSidKey = 'en_US', ProfileId = p.Id,UserPermissionsMarketingUser=true,
                TimeZoneSidKey = 'America/Los_Angeles', UserName = 'standarduser@testorg.com');

            System.runAs(u) {   
            Campaign testCampaign       = new Campaign();
            testCampaign.name           = 'TestCampaign';
            testCampaign.CallerTimeout__c = 'Null';
            testCampaign.Callbacks__c = '2 Callbacks';
            insert testCampaign; // << Line 31
        }
    }
}

Using run as will ensure, that no matter if the test running user does/does not have permission your test will execute.

Src: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm

| improve this answer | |

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.