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
Dchris222Dchris222 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

I am recieving the following error when I try to deploy my apex class and test class: visualforce CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, opptySwarm: execution of AfterUpdate.  Is this because I am trying to update too many field at once?? Or some other issue? What is my best option to fix this?

 

Thank you in advance, I am new to Salesforce development.

 

Chris


Apex Class:

 

 

global class OppPoliceController {
 
  public void Opportunity1() {
  	
  	 List<Opportunity> Opptys2 = [SELECT CreatedDate, StageName, LastActivityDate, Activity_Status__c, (SELECT CreatedDate FROM Feeds ORDER BY CreatedDate DESC limit 1), (SELECT Status FROM Tasks) FROM Opportunity WHERE StageName != 'Closed Won' AND StageName != 'Closed Lost' AND StageName != 'Closed Other' AND StageName != 'Qualified Lost' AND StageName != 'Prospect' AND CloseDate != Today ORDER BY LastActivityDate DESC];
 
	
        for(Opportunity Op : Opptys2){
        
        datetime d = Date.today();  //Current Date Formated
        datetime LastActivityDate = Op.LastActivityDate; //Last Activity Date in datetimeformat
        
        datetime CreatedDate1;
        if(Op.Feeds != Null && !Op.Feeds.isEmpty()) {
        	
           CreatedDate1 = Op.Feeds[0].CreatedDate;  //Created Date in datetime format 	
          
        }
        else {
        	system.debug('<--------------Error CreatedDate for Feeds is Null---------------->');
        	CreatedDate1 = Op.CreatedDate;
        	
        }
         	 
        string Status1;
        if(Op.Tasks != Null && !Op.Tasks.isEmpty())  {
        	
        	Status1 = Op.Tasks[0].Status;
        	
        }
        else {
        	system.debug('<----------------ERROR Staus for Tasks is Null-------------------->');
        	Status1 = null;
        	
        }
        

        System.debug('This is the created date: ' + CreatedDate1);
        System.debug('This is the status: ' + Status1);
        System.debug('This is todays date: ' + d); 
        
            if(LastActivityDate == Null) { 
            	
            	if(Status1 == 'In Progress' || Status1 == 'Waiting on someone else' || Status1 == 'Deferred') {
            		
            		 Op.Activity_Status__c = 'Low'; 
            		 	 
            	}
            	 else if(Status1 == null || Status1 == 'Not Started' || d > CreatedDate1.addDays(14)) {
            	
            	 Op.Activity_Status__c = 'High'; 
            	     	 
            	}
            	 else if(d >= CreatedDate1.addDays(7) && d < CreatedDate1.addDays(14)) {
         
                 Op.Activity_Status__c = 'Medium'; 
               
                  }
                  else {
                  	
                  	 Op.Activity_Status__c = 'error';  
                  }
                          	
            }
           
            else if(d > LastActivityDate.addDays(14) || d > CreatedDate1.addDays(14)) {
              
              Op.Activity_Status__c = 'High';
              
            }
            
            else if((d >= CreatedDate1.addDays(7) && d < CreatedDate1.addDays(14))
            	|| (d >= LastActivityDate.addDays(7) && d < LastActivityDate.addDays(14)) ) {
         
               Op.Activity_Status__c = 'Medium'; 
               
            }
              
            else if(d < LastActivityDate.addDays(7) || d < CreatedDate1.addDays(7) || Status1 == 'In Progress' || Status1 == 'Waiting on someone else' || Status1 == 'Deferred') {
            	
                Op.Activity_Status__c = 'Low';
                
            }
            	
            else {
            
                Op.Activity_Status__c = 'Error';
                   
            }
        }    
           update Opptys2;
  
  }   
}

 

 

 

Apex Test Class

 

 

@isTest
private class OppPoliceTestClass {

    static testMethod void testOpportunity1() {
        
      date d = Date.today() + 7;
      
      System.debug('The close date is: ' + d);
        
      Opportunity Opp = new Opportunity();
      Opp.name = 'Op-Test9';
      Opp.StageName = 'Open';
      Opp.CloseDate = d;
   
      insert Opp;
      
      Task t = new Task();
      t.Subject='Send out Notice';
      t.Status = 'Not Started';
      t.Priority = 'Normal';
      t.WhatId = Opp.id;
      
      insert t;
      
      System.debug('This is the Opp id: ' + Opp.id);
      System.debug('This is the WhatId: ' + t.WhatId);
      
      OppPoliceController myClass = new OppPoliceController();
      myClass.Opportunity1();
      
      List<Opportunity> OppStatus = [select o.name, o.Activity_Status__c from Opportunity o WHERE o.name= 'Op-Test9' LIMIT 1];
      
      string OpName = OppStatus[0].name;
      System.debug('The name of the Opp is ' + OpName);    
      
      for(Opportunity Op2 : OppStatus){
          
          System.assertEquals('High',Op2.Activity_Status__c);
          System.debug('The Activity Status for the Test is: ' + Op2.Activity_Status__c);
          
      }                              
   }
}



 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

If its Production you're deploying to, you can't edit code in Production. You will have to "deploy" any and all code changes. Might be an idea to check with whoever put those validation rules in and who wrote those other tests. You might want to update the test classes and deploy - as they seem to be only simple data related validation errors.

All Answers

Ankit AroraAnkit Arora

Hi,

 

I don't think there is any problem with your code. Its just there is some problem at FLS(Field Level Security) of Activity. Please make sure all fields of Activity , Task and Event is visible for System Administrator on both orgs (Also opportunity fields which your are using). Hopefully this will resolve your problem.

 

If this still doesn't resolves your problem then make sure CRUD rights are given to objects which you are using in this code.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Dchris222Dchris222

Where do I find the FLS settings specific to Activities to check?

Dchris222Dchris222

nm found it. Will check for correct settings.

Ritesh AswaneyRitesh Aswaney

hey chris

could you post the entire error that you're receiving

Dchris222Dchris222

Should I make sure that none of the fields have Read-Only checked for the System admin?

Ankit AroraAnkit Arora

Need not to check that, just make sure all fields should be visible.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Dchris222Dchris222

@Ritesh Here is the entire error:

 

OppPoliceTestClass.testOpportunity1 System.DmlException: Update failed. First exception on row 854 with id 0063000000eGB2AAAW; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, opptySwarm: execution of AfterUpdate

Ritesh AswaneyRitesh Aswaney

The error may be occuring because of various reasons - 

sharing rules : the Opportunity record which is failing update may be owned by a user, such that it is not writeable by anyone else. So it kinda depends on the Role of the User that you are executing as.

 

When you're testing a class (similar to batch apex), which processes a bulk of records, you want to pass in a criteria to the class, which constrains the selection query to the test data that you've created, rather than have it trawl through the records in your org.

 

You could add a little bit of code to run in test mode

 

 

global class OppPoliceController {
public boolean isTest = false;
public Id testOppId {get; set;}
List<Opportunity> Opptys2;
if (isTest && testOppId != null)  //ADDED TO THE WHERE CLAUSE TO MATCH ON TESTID
Opptys2 = [SELECT CreatedDate, StageName, LastActivityDate, Activity_Status__c, (SELECT CreatedDate FROM Feeds ORDER BY CreatedDate DESC limit 1), (SELECT Status FROM Tasks) FROM Opportunity WHERE StageName != 'Closed Won' AND StageName != 'Closed Lost' AND StageName != 'Closed Other' AND StageName != 'Qualified Lost' AND StageName != 'Prospect' AND CloseDate != Today AND Id = :testOppId ORDER BY LastActivityDate DESC];
else
Opptys2 = [SELECT CreatedDate, StageName, LastActivityDate, Activity_Status__c, (SELECT CreatedDate FROM Feeds ORDER BY CreatedDate DESC limit 1), (SELECT Status FROM Tasks) FROM Opportunity WHERE StageName != 'Closed Won' AND StageName != 'Closed Lost' AND StageName != 'Closed Other' AND StageName != 'Qualified Lost' AND StageName != 'Prospect' AND CloseDate != Today ORDER BY LastActivityDate DESC];
........}
And in your test class, when you're invoking your class
      OppPoliceController myClass = new OppPoliceController();
       myClass.isTest = true;
myClass.testOppId = Opp.Id;
      myClass.Opportunity1();

 

Dchris222Dchris222

Ok so i finaly sorted out all of the issues with my code. Now I realize that there are other tests being run against my code that have been deployed to Salesforce why is that and can I disable the test that I don't want ran?

 

Here are my Run Failures:

 


  CaseSwarmTest.testOnlyAccountsIOwn2 System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Billing Address required: []


  OpportunitySwarmTest.testOnlyAccountsIOwn2 System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Billing Address required: []


  VFAssigntoLeasing.TestCtrBn System.DmlException: Update failed. First exception on row 0 with id 0063000000YtVwbAAF; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please Insert the winning price.: []


  VFAssignToSales.TestCtrBn System.DmlException: Update failed. First exception on row 0 with id 0063000000YtVwbAAF; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please Insert the winning price.: []

Ritesh AswaneyRitesh Aswaney

Nice one Chris !

 

If you are deploying to Production, then all of the test classes will execute.

 

These tests are failing owing to Validation Errors encountered in the test classes mentioned.

 

Might be an idea to just run tests on the Production org without deploying your code first, to check if the problem already exists. Perhaps someone has put these validations rules in after the tests were deployed.

Dchris222Dchris222

Here is an idea that I just had, what if i comment out the test classes mentioned above. Then I can upload my Apex Class and after uncomment the tests. Would that work or is that an extrmelly bad idea!?

Ritesh AswaneyRitesh Aswaney

If its Production you're deploying to, you can't edit code in Production. You will have to "deploy" any and all code changes. Might be an idea to check with whoever put those validation rules in and who wrote those other tests. You might want to update the test classes and deploy - as they seem to be only simple data related validation errors.

This was selected as the best answer
APEX 123APEX 123

I am getting the error like this on the test case can any body help me to reslove it

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, quoteUpdateopportunity: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

Trigger.quoteUpdateopportunity: line 10, column 1: []

 

This is my trigger wrote on quote

 

trigger quoteUpdateopportunity on quote__c( after insert) {
    List<opportunity> oppToUpdate = new List<opportunity>();

    for(quote__c quo : trigger.new){
        if (quo.Quote_Number__c!= Null )
            oppToUpdate.add(new opportunity(id= quo.Opportunity_Name__c, StageName = 'offer'));
    }

    if (oppToUpdate != null && !oppToUpdate.isEmpty())
        update(oppToUpdate);
}

this is the test class i wrote 

 

@isTest
private class quoteupdateopportunity{
    static testMethod void testquoteupdateopportunity(){
    quote__c qo=new quote__c(Author_Email_New__c='manoj.jena@kvpcorp.com');
       insert qo;
       
  opportunity op= new opportunity(name='test 002');
  insert op;
  
op= [SELECT stageName FROM opportunity Where Id = :op.Id];
System.assertEquals ('offer',op.stagename); 
   }   
   }

 

Help me to find out get riid of this error