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
magandrezmagandrez 

Trigger error CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

Hi,

 

I got an error when executing the test method I wrote for a trigger. I saw many entries in the discussion boards about this, but no concrete answer and nothing I can make an abstraction and apply it to my problem.

 

 

I have a trigger where I create posts to a a specific group if the Opportunity is closed. The trigger looks like this:

 

trigger chatterUpdateTrigger on Opportunity (before update) {
	
	Id parentPostId = [SELECT Id 
					   FROM CollaborationGroup 
					   WHERE Name=:'All Group'].Id;
					   
	Id RecordType1= [SELECT Id 
								 FROM RecordType 
								 WHERE SOBJECTTYPE=:'Opportunity' 
								 AND DeveloperName=:'Record Type 1'].Id;
								 
	Id RecordType2= [SELECT Id 
						FROM RecordType 
						WHERE SOBJECTTYPE=:'Opportunity' 
						AND DeveloperName=:'Record Type 2'].Id;
	
	List<FeedPost> listPosts = new List<FeedPost>();
	
    for (Opportunity opp: Trigger.new){
    	
    	//List<RecordType> listOppRecordTypes = [SELECT DeveloperName FROM RecordType WHERE Id=:opp.RecordTypeId AND DeveloperName=:'Record Type 2' AND DeveloperName=:'Record Type 2'];
    	Opportunity beforeUpdate = System.Trigger.oldMap.get(opp.Id);

    		if(opp.RecordTypeId == RecordType1){
    			
    			if(beforeUpdate.StageName != opp.StageName && opp.StageName=='We WON!'){
    				
    				FeedPost post = new FeedPost(ParentId= parentPostId,Body='The opportunity ' +beforeUpdate.Name+ ' with a value of '+beforeUpdate.Amount+'€ has been won!');            
	        		listPosts.add(post);
	        		
    			}
    		}else{
    			
    			if(opp.RecordTypeId == RecordType2){
			
					if(beforeUpdate.StageName != opp.StageName && opp.StageName=='We WON!'){
	    				
	    				FeedPost post = new FeedPost(ParentId=parentPostId,Body='The opportunity ' +beforeUpdate.Name+ ' with a value of '+beforeUpdate.Amount_Quotation_Price__c+'€ has been won!');            
		    			listPosts.add(post);
		        		
    				}
			
    			}
    		} 
	}
	
	try{
		
		insert listPosts;
		
	}catch(Exception ex){
		Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] {'customer.service@myself.me'});
        mail.setSubject('Chatter Trigger ERROR');
        String body = 'This message was sent automatically from chatterUpdateTrigger from SF. \n'+
        'Apparently there was an error on the insertion of the Chatter Message. This is the exception: \n'+ex;  
        mail.setPlainTextBody(body);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
	}
	
}

 

And this is the test class:

 

@IsTest
 public class testChatterUpdateTrigger{
    
    static testMethod void testChatterUpdateTrigger(){

	
	
	Id RecordType1= [SELECT Id 
								 FROM RecordType 
								 WHERE SOBJECTTYPE=:'Account' 
								 AND DeveloperName=:'Record Type 1'].Id;
								 
	Id RecordType2= [SELECT Id 
						FROM RecordType 
						WHERE SOBJECTTYPE=:'Account' 
						AND DeveloperName=:'Record Type 2'].Id;

							
						
	Account ac1 = new Account(RecordTypeId = RecordType1,
							  Name = 'Test Account 1',
							  CurrencyIsoCode = 'EUR',
							  Industry = 'Test',
							  Account_Type__c = 'Agent',
							  BillingCountry = 'UK',
							  BillingCity = 'London',
							  BillingStreet = 'Aleski 123');
	insert ac1;
	
	Account ac2 = new Account(RecordTypeId = RecordType2,
							  Name = 'Test Account 2',
							  CurrencyIsoCode = 'EUR',
							  Industry = 'Test',
							  Account_Type__c = 'Agent',
							  BillingCountry = 'UK',
							  BillingCity = 'London',
							  BillingStreet = 'Aleski 123');
	
	insert ac2;							  
							  	
	
	Account account1= [SELECT Id,
									 Name
							  FROM Account
							  WHERE Name =: 'Test Account 1'
							  LIMIT 1];

	Account account2= [SELECT Id,
								 Name
							  FROM Account
							  WHERE Name =: 'Test Account 2'
							  LIMIT 1];

	Id oppRecordType1= [SELECT Id 
								 FROM RecordType 
								 WHERE SOBJECTTYPE=:'Opportunity' 
								 AND DeveloperName=:'Record Type 1'].Id;
								 
	Id oppRecordType2= [SELECT Id 
						FROM RecordType 
						WHERE SOBJECTTYPE=:'Opportunity' 
						AND DeveloperName=:'Record Type 2'].Id;								  							  
	
	Opportunity oppRecType1= new Opportunity(RecordTypeId = oppRecordType1,
											 Name = 'Test 1',
											 AccountId = account1.Id,
											 CurrencyIsoCode = 'EUR',
											 Amount = 100,
											 CloseDate = system.today(),
											 StageName = 'Pre-Study');

	insert oppRecType1;
	
	Opportunity opRec1= [SELECT StageName
						   FROM Opportunity
						   WHERE Name =:'Test 1'
						   LIMIT 1];

	Opportunity oppRecType2= new Opportunity(RecordTypeId = oppRecordType2,
										 Name = 'Test 2',
										 AccountId = account2.Id,
										 End_Customer_Country__c = 'UK',
										 Industry__c = 'Auto',
										 CurrencyIsoCode = 'EUR',
										 Vehicle_Types_pick_several__c = 'Test',
										 No_of_Vehicles__c = 0,
										 Load_weight__c = 0,
										 Lifting_height_mm__c = 0,
										 Operating_Conditions__c = 'Outside',
										 Quotation_Price__c = 1000,
										 Scope__c = 'Other',
										 StageName = 'Pre-Study',
										 Quotation_valid_until__c = system.today(),
										 CloseDate = system.today(),
										 Navigation__c = 'Other');

	insert oppRecType2;
	
	Opportunity opRec2= [SELECT StageName
						  FROM Opportunity
						  WHERE Name = 'Test 2'
						  LIMIT 1];

	List<Opportunity> listOpp = new List<Opportunity>();
	listOpp.add(opRec1);
	listOpp.add(opRec2);
	
	Integer feedEntriesBefore = [SELECT COUNT() 
					   FROM CollaborationGroup 
					   WHERE Name=:'All Group'];

	test.startTest();					   

	oppKoti.StageName = 'We WON!';
	update opRec1;
	
	AGVopp.StageName = 'We WON!';
	update opRec2;
	
    Integer feedEntriesAfter = [SELECT COUNT()
						   		FROM CollaborationGroup
						   		WHERE Name=:'All Group'];

	system.assertNotEquals(feedEntriesBefore, feedEntriesAfter);
	
	test.stopTest();

	}
	
}

 And I got this error when executing tests:

 

System.DmlException: Update failed. First exception on row ' with id XXXXXXXXXXXXX: first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, chatterUpdateTrigger: excecution of BeforeUpdate ON LINE 115 -> that is the update of opRec1 in the test method.

 

Does anyone know exactly what is happening and why?

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

My guess is that your error is somewhere in:

Id parentPostId = [SELECT Id 
					   FROM CollaborationGroup 
					   WHERE Name=:'All Group'].Id;
					   
	Id RecordType1= [SELECT Id 
								 FROM RecordType 
								 WHERE SOBJECTTYPE=:'Opportunity' 
								 AND DeveloperName=:'Record Type 1'].Id;
								 
	Id RecordType2= [SELECT Id 
						FROM RecordType 
						WHERE SOBJECTTYPE=:'Opportunity' 
						AND DeveloperName=:'Record Type 2'].Id;

 Most likely the Collaboration Group call.  I think that you need to create Collaboration Groups in code for tests class.  Put those exact lines in the very beginning of your test method and see if it fails there.