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
RongzhongHuangRongzhongHuang 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, A workflow or approval field update caused an error

Hi guys,

 

I have a trigger(refer to the code below) on Account object. When I run the testing of trigger, I got the error 

message "CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, A workflow or approval field update caused an error when saving this record". 

 

I found that a workflow field update happening when testing. According to salesforce apex language reference, 

Triggers and Order of Execution

 

10. If there are workflow field updates, updates the record again.
11. If the record was updated with workflow field updates, fires before and after triggers one more time (and only one more time).

 

I don't understand why I get this error message.  Is there any person can help? 

 

 

trigger updataRegionMapping on Account (before insert, before update)
{
    //List<Account> accNew = trigger.new;
    List<String> accSuburbs = new List<String>();
    List<String> accStates = new List<String>();
    List<String> accPostCode = new List<String>();
    for (Account acc : trigger.new)
    {
        accSuburbs.add(acc.ShippingCity);
        accStates.add(acc.ShippingState);
        accPostCode.add(acc.ShippingPostalCode);
    }
   
    List<Region_Mapping__c> regionMapping = [select ID,Name,Regional_Metro__c, Region__c
                                             from Region_Mapping__c where PostCode__c in :accPostCode
                                             and State__c in :accStates and Suburb__c in :accSuburbs ];
    System.debug('regionMapping found: '+ regionMapping.size());
    if (regionMapping.size() == 1)
    {
        for (Integer i = 0;i<trigger.new.size();i++)
        {
            //trigger.new[i].Region__c = regionMapping[0].Region__c;
            //trigger.new[i].Region_Type__c = regionMapping[0].Regional_Metro__c;
            trigger.new[i].Region_Mapping__c = regionMapping[0].Id;
        }
    }
                                       
}

RongzhongHuangRongzhongHuang

My testing:

 

@isTest
private class updateRegionTest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        for (Region_Mapping__c reg : [SELECT Id,Region__c
                                      FROM Region_Mapping__c
                                      WHERE Name = '2150 PARRAMATTA'
                                     ])
        {  
            // change region
            reg.Region__c = 'Parramatta Test';
            update reg;    
        }
    }
}