function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
deanodeano 

Error when creating a trigger: before update, CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

Hello everyone,

 

This is my first post here, I have web development background in the .net space and am diving into SFDC, apex and VF.

 

There are a few things i'm struggling with as I haven't done any of that before.

 

I have built a trigger that is supposed to fire on insert and update of lead records. I'm in a sandbox environment. I have written tests for all methods involved and have 100% coverage on all.

 

If I browse to my sandbox instance and insert or update leads the trigger performs the tasks that it should.

 

However, when running the tests it fails with the error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

 

If I remove the "before update" from the trigger signiture everything runs without a problem. Adding back in "before update" and the problem arises.

 

I'll past the code below. Countries is a class that contains static methods that return List<String>

 

 trigger SetNotification on Lead (after insert, after update) {

// dean@sqwarepeg.com

//This trigger checks if any of the selections in the source or destination countries multi picklists require investigation

//Countries require further investigation depending on what list they appear in in the Countries static methods

String errorMessage = 'Some of the selected countries are possibly not eligible';

Id businessClientRTId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Business Clients').getRecordTypeId();

Id privateClientRTId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Private Clients').getRecordTypeId();

for(Lead lead:trigger.new){

 

List<String> compareCountries = new List<String>();

if(lead.RecordTypeId == businessClientRTId){

compareCountries = Countries.GetCorporatePossiblyEligible();

compareCountries.addAll(Countries.GetCorporateNonEligible());

}else if(lead.RecordTypeId == privateClientRTId){

compareCountries = Countries.GetPrivatePossiblyEligible();

compareCountries.addAll(Countries.GetPrivateNonEligible());

}

List<String> selectedCountries = new List<String>();

//Source Country Check

String strCountries = String.valueOf(lead.Source_Country_s_A_L__c);

if(strCountries != null && strCountries != '')

selectedCountries.addAll(strCountries.split(';'));

strCountries = String.valueOf(lead.Source_Country_s_M_Z__c);

if(strCountries != null && strCountries != '')

selectedCountries.addAll(strCountries.split(';'));

Boolean matchFound = false;

for(String selectedCountry:selectedCountries)

{

for(String compareToCountry:compareCountries){

if(selectedCountry == compareToCountry){

matchFound = true;

break;

}

}

if(matchFound)

break;

}

if(matchFound)

lead.Source_Country_Notification__c = errorMessage.toUpperCase();

else

lead.Source_Country_Notification__c = '';

//Destination Country Check

selectedCountries.clear();

strCountries = String.valueOf(lead.Destination_Country_s_A_L__c);

if(strCountries != null && strCountries != '')

selectedCountries.addAll(strCountries.split(';'));

strCountries = String.valueOf(lead.Destination_Country_s_M_Z__c);

if(strCountries != null && strCountries != '')

selectedCountries.addAll(strCountries.split(';'));

matchFound = false;

for(String selectedCountry:selectedCountries)

{

for(String compareToCountry:compareCountries){

if(selectedCountry == compareToCountry){

matchFound = true;

break;

}

}

if(matchFound)

break;

}

if(matchFound)

lead.Destination_Country_Notification__c = errorMessage.toUpperCase();

else

lead.Destination_Country_Notification__c = '';

}

}

 

What is the issue with before update? I want the same functionality regardless of insert or update. 

 

Thank you for any assistance!!! 

Best Answer chosen by Admin (Salesforce Developers) 
deanodeano

Not to worry, solution found. It was the SOQL to select the record type id's. When I removed those and hard coded in the id's all worked.

 

Cheers 

All Answers

jdl2009jdl2009
Is there any workflow/action that might be modifying the record?
deanodeano

Hello,

 

No there isn't.

 

I've just modified another trigger slightly that used to work fine, but now i'm getting the same error when running test against this trigger. This trigger is only an insert one, so perhaps it has nothing to do with the update.

 

Here is the code for the other trigger. Please note that this trigger is against leads as well.

 

 trigger SetShortName on Lead (before insert) {

//dean@sqwarepeg.com

//this trigger ensures that upon creating a new private lead the shortname is set to the contacts first name plus last name

//and upon creating a new corporate lead the shortname is set to the compnay name

//if a lead already exists with the above, an incremental number is added to the end of the shortname starting from 0

//the trigger ensures that after the above processing the short name is no longer than 20 characters in length 

Id businessClientRTId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Business Clients').getRecordTypeId();

Id privateClientsRTId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Private Clients').getRecordTypeId();

for(Lead lead : trigger.new)

{

if(lead.RecordTypeId != businessClientRTId && lead.RecordTypeId != privateClientsRTId)

continue;

if(lead.RecordTypeId == businessClientRTId)

lead.SHORTNAME_test__c = lead.Company.toUpperCase();

else if(lead.RecordTypeId == privateClientsRTId)

lead.SHORTNAME_Test__c = lead.FirstName.toUpperCase() + lead.LastName.toUpperCase();

if(lead.SHORTNAME_test__c.length() > 20)

lead.SHORTNAME_test__c = lead.SHORTNAME_test__c.substring(0,19);

List<Lead> leads = new List<Lead>();

if(lead.RecordTypeId == privateClientsRTId)

leads.addAll([select ID from Lead where FirstName = :(lead.FirstName) and LastName = :(lead.LastName)]);

else

leads.AddAll([select ID from Lead where Company = :(lead.Company)]);

Integer existingCount = leads.size();

if(existingCount == 0)

continue;

String shortName = lead.SHORTNAME_Test__c;

if(shortName.length() == 20)

{

if(existingCount < 10)

shortName = shortName.substring(0,19);

else

shortName = shortName.substring(0,18);

}

lead.SHORTNAME_Test__c = shortName + leads.size();

}

}

 

I have thoroughtly tested both manually by inserting records manually, and both do exactly what I want without any problem.

 

I think the issue is with the tests themselves. I haven't done any tests before so I know these are ugly I was just trying to get past the 75% limmit.

 

 @isTest

private class UTLeads {

 

        

    public static testMethod void Test_Trigger_SetShortName() {

        

        Id privateClientsRTId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Private Clients').getRecordTypeId();

        Id businessClientRTId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Business Clients').getRecordTypeId();

        

     String firstName = 'TestFirstName';

     String lastName = 'TestLastName';

     String reason = 'Test';

    

        Lead One = new Lead();

        One.RecordTypeId = businessClientRTId;

        One.Status = 'Open';

        One.FirstName = firstName;

        One.LastName = lastName;

        One.Company = 'CompanyName';

        One.Reason_for_Destination_Country_Selection__c = reason;

        One.Reason_for_Source_Country_Selection__c = reason;

        

        Lead Two = new Lead();

        Two.RecordTypeId = privateClientsRTId;

        Two.Status = 'Open';

        Two.FirstName = firstName;

        Two.LastName = lastName;

        Two.Reason_for_Destination_Country_Selection__c = reason;

        Two.Reason_for_Source_Country_Selection__c = reason;

        

        Lead Three = new Lead();

        Three.RecordTypeId = privateClientsRTId;

        Three.Status = 'Open';

        Three.FirstName = firstName;

        Three.LastName = lastName;

        Three.Reason_for_Destination_Country_Selection__c = reason;

        Three.Reason_for_Source_Country_Selection__c = reason;

        

   insert One;

   insert Two;

   insert Three;

   

   //List<Lead> leads = [select Lead.ShortName_Test__c from Lead where Lead.FirstName = :firstName and Lead.LastName = :lastName];

        

        //System.assertEquals(leads[0].get('SHORTNAME_Test__c'), firstName.toUpperCase() + lastName.toUpperCase());

        //System.assertEquals(leads[1].SHORTNAME_Test__c, firstName.toUpperCase() + lastName.toUpperCase() + 1);

        //System.assertEquals(leads[2].SHORTNAME_Test__c, firstName.toUpperCase() + lastName.toUpperCase() + 2);

        

        

        

    }

    

    public static testMethod void Test_Trigger_SetNotification() {

     Id businessClientRTId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Business Clients').getRecordTypeId();

     Id privateClientRTId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Private Clients').getRecordTypeId();

     String firstName = 'TestFirstName';

     String lastName = 'TestLastName';

     String reason = 'Test';

    

     Lead Four = new Lead();

        Four.RecordTypeId = businessClientRTId;

        Four.Status = 'Open';

        Four.FirstName = firstName;

        Four.LastName = lastName;

        Four.Reason_for_Destination_Country_Selection__c = reason;

        Four.Reason_for_Source_Country_Selection__c = reason;

        Four.Source_Country_s_A_L__c = Countries.GetCorporateEligible()[0];

        Four.Source_Country_s_M_Z__c = Countries.GetCorporateNonEligible()[0];

        Four.Destination_Country_s_A_L__c = Countries.GetCorporateEligible()[0];

        Four.Destination_Country_s_M_Z__c = Countries.GetCorporateNonEligible()[0];

        

        Lead Five = new Lead();

        Five.RecordTypeId = privateClientRTId;

        Five.Status = 'Open';

        Five.FirstName = firstName;

        Five.LastName = lastName;

        Five.Reason_for_Destination_Country_Selection__c = reason;

        Five.Reason_for_Source_Country_Selection__c = reason;

        Five.Destination_Country_s_A_L__c = Countries.GetPrivateEligible()[0];

        Five.Destination_Country_s_M_Z__c = Countries.GetPrivatePossiblyEligible()[0];

        

        insert Four;

   insert Five;

    }

    

    public static testMethod void Test_Class_Countries() {

     Countries.GetCorporateEligible();

     Countries.GetCorporateNonEligible();

     Countries.GetCorporatePossiblyEligible();

     Countries.GetPrivateEligible();

     Countries.GetPrivateNonEligible();

     Countries.GetPrivatePossiblyEligible();

    }

    

}

 

 

When I add back in the before update on the setNotification trigger I drop well below the 75%. If I remove it, i'm almost at 100%.

 

Thank you so much for your help!!! 

deanodeano

Not to worry, solution found. It was the SOQL to select the record type id's. When I removed those and hard coded in the id's all worked.

 

Cheers 

This was selected as the best answer