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
RajnisfRajnisf 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY...need help urgent...

M getting the following error...so code coverage is less than 75%...not able to deploy...Thanks in advance for help...

 

error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Minutescase: execution of AfterInsert caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com

 

 

 

@isTest(SeeAllData=true)
private class testMyTaskTrigger {
 
    public static testMethod void testTTrigger(){
      
        
        Account acc  = new Account();
        acc.name = 'Test';
        insert acc;

        Contact con = new Contact();
        con.LastName = 'Test Contact';
        insert con;
       
       
        Task t = new Task();
        t.OwnerId = UserInfo.getUserId();
        t.Subject='Donni';
        t.Status='Not Started';
        t.Priority='Normal';
        t.Minutes_on_tasks__c = 2000;
        t.status = 'Completed';
        insert t;
 
        Task tas = new Task();
        tas.OwnerId = UserInfo.getUserId();
        tas.Subject='Donni';
        tas.Status='Not Started';
        tas.Priority='Normal';
        tas.Minutes_on_tasks__c = 2000;
        tas.status = 'Completed';
        tas.whoid = con.id;

        Sales_Resources__c sr = new Sales_Resources__c ();
        sr.Name = 'Test Sales Resources';
        insert sr;
        
        Task p = [select id from task where Ownerid = '00500000006scDCAAY' limit 1];
         try{
        delete p;
        }
        Catch(Exception e)
        {

          system.debug('Error:'+e);
        }
 
       
        Test.startTest();
        insert tas;
        try{
        delete t;
        }
        Catch(Exception e)
        {

          system.debug('Error:'+e);
        }

        Test.stopTest();
       
     }

}

 

 

same error in this test class also...  Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, EB_Case_Owner_Trigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Minutescase

 

 

@isTest(SeeAllData=true)
private class EB_Case_Owner_Trigger_Test_Class {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        EB_SB_Builder__c ebsb = new  EB_SB_Builder__c ();
        Case accs1 = new Case();
        insert ebsb;
        Datetime dt = Datetime.now();
        Case accs = new Case(RecordTypeId='0120000000098Po',Event_Survey_Builder__c = ebsb.Id, Origin= 'Email', Subject='Testing Task', Minutesoncase__c =10, Reason='508 compliance', Product_Type__c = 'Event Tool');
        //Case accs = new Case(ContactId = '003P000000Cu7pU',RecordTypeId='0120000000095Nn',Event_Survey_Builder__c = ebsb.Id, Origin= 'Email', Subject='Testing Task', Minutesoncase__c =10, Reason='508 compliance', Product_Type__c = 'Event Tool' , AccountId='001P000000GF1fq');
            insert accs;
    }
    

}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
trigger Minutescase on Task (after insert, after update, before delete, after delete)
{
  public Set<Id> srids = new Set<Id>();
  public List<Sales_Resources__c> ttt = new List<Sales_Resources__c>();

  if(Trigger.isInsert || Trigger.isUnDelete || Trigger.isUpdate)
  {
    for(Task t: Trigger.new)
    {
      if(t.WhatId <> null && t.Minutes_on_tasks__c <> null && t.IsClosed == True)
      {
        srids.add(t.WhatId);
      }
    }
  }

  if(Trigger.isDelete)
  {
    for(Task t: Trigger.old)
    {
      if(t.WhatId <> null && t.Minutes_on_tasks__c <> null && t.IsClosed == True)
      {
        srids.add(t.WhatId);
      }
    }
  }

  Map<Id, AggregateResult> arMap = new Map<Id, AggregateResult>([SELECT WhatId Id, SUM(Minutes_on_tasks__c) summ FROM Task WHERE Status = 'Completed' AND WhatId IN :srIds GROUP BY WhatId]);

  for(Sales_Resources__c sc : [SELECT Id, Total_Time_Spent_Hrs__c FROM Sales_Resources__c WHERE Id != null AND Id IN :srids])
  {
    sc.Total_Time_Spent_Hrs__c = Decimal.valueOf((arMap.get(sc.Id)).get('summ'));
    ttt.add(sc);
  }
  update ttt;
}

 hey try this.. let me know if it doesnt work....

 

All Answers

Devendra@SFDCDevendra@SFDC
Hi,

AccountId field is missing while inserting an Contact.

Account acc = new Account();
acc.name = 'Test';
insert acc;

Contact con = new Contact();
con.LastName = 'Test Contact';
con.AccountId = acc.Id; // AccountId of the contact
insert con;

Thanks,
Devendra
RajnisfRajnisf

Still giving the same error....  error is with 

code in green...


        Task t = new Task();
        t.OwnerId = UserInfo.getUserId();
        t.Subject='Donni';
        t.Status='Not Started';
        t.Priority='Normal';
        t.Minutes_on_tasks__c = 2000;
        t.status = 'Completed';
        insert t;
 

 

Devendra@SFDCDevendra@SFDC

Hi,

There is a trigger code which executes after insert of a Task.

The query inside a trigger returning more than 100K records. You need to make changes into a trigger code. For example you can use filter conditions inside a query etc.

Thanks,
Devendra

RajnisfRajnisf

here is the trigger code:

 

trigger Minutescase on Task (after insert, after update, before delete, after delete) {
     public set<Id> srids = new Set<Id>();
     public list<Sales_Resources__c> timeupdate = new List<Sales_Resources__c>();
      aggregateResult[] results  = [Select SUM(Minutes_on_tasks__c)summ From Task where status = 'Completed'];
      Decimal decimalRevenue = 0 ;
            if(results.size() > 0)
                {
                    String str = '' + results[0].get('summ') ;
                    decimalRevenue = Decimal.ValueOf(str) ;
                    System.debug('decimalRevenue ::::: ' + decimalRevenue) ;
                }
      if(Trigger.isInsert || Trigger.isUnDelete || Trigger.isUpdate){
      for(Task t: Trigger.new){
            if(t.WhoId<>null && t.Minutes_on_tasks__c <> null && t.IsClosed==True)
            {               
                srids.add(t.WhoId);
            }   
            }
        }
       
         if(Trigger.isDelete){
      for(Task t: Trigger.old){
            if(t.WhoId<>null && t.Minutes_on_tasks__c <> null && t.IsClosed==True)
            {               
                srids.add(t.WhoId);
            }   
            }
        }
       
       
         timeupdate  = ([Select c.Id,c.Total_Time_Spent_Hrs__c,(Select Id, Minutes_on_tasks__c From Tasks where IsClosed = True AND id in: srids)From Sales_Resources__c c]);
                             
                              for(Sales_Resources__c sc : timeupdate)
                             
                                 {
                                
                                sc.Total_Time_Spent_Hrs__c = decimalRevenue;
                                
                                 }             
                        
                            update timeupdate;
                           
 }

RajnisfRajnisf
m not able to understand where do i need to change...and m not able to deploy this trigger...while deploying it is giving the same errors in the existing test classes ..m really fed up...
davidjgriffdavidjgriff
Your task query is not selective enough since you are essentially querying for all completed tasks in the Org. Add some more filters to that query.

Also, you may want to consider removing the "SeeAllData=true" bit, if possible. It's best to create all your test data within the test itself.
Naidu PothiniNaidu Pothini
trigger Minutescase on Task (after insert, after update, before delete, after delete)
{
	public set<Id> srids = new Set<Id>();

	public list<Sales_Resources__c> timeupdate = new List<Sales_Resources__c>();

	AggregateResult[] results  = [Select SUM(Minutes_on_tasks__c)summ From Task where status = 'Completed'];

	Decimal decimalRevenue = 0 ;

	if(results.size() > 0)
	{
		String str = '' + results[0].get('summ') ;
		decimalRevenue = Decimal.ValueOf(str) ;
		System.debug('decimalRevenue ::::: ' + decimalRevenue) ;
	}

	if(Trigger.isInsert || Trigger.isUnDelete || Trigger.isUpdate)
	{
		for(Task t : Trigger.new)
		{
			if(t.WhoId <> null && t.Minutes_on_tasks__c <> null && t.IsClosed == True)
			{               
				srids.add(t.WhoId);
			}   
		}
	}

	if(Trigger.isDelete)
	{
		for(Task t: Trigger.old)
		{
			if(t.WhoId<>null && t.Minutes_on_tasks__c <> null && t.IsClosed==True)
			{               
				srids.add(t.WhoId);
			}   
		}
	}

	timeupdate  = ([Select c.Id,c.Total_Time_Spent_Hrs__c,(Select Id, Minutes_on_tasks__c From Tasks where IsClosed = True AND id in: srids)From Sales_Resources__c c]);  // this line will cause issues... so add some filters here... 
for(Sales_Resources__c sc : timeupdate) { sc.Total_Time_Spent_Hrs__c = decimalRevenue; } update timeupdate; }

 Is there any particular reason for querying all the Sales Resources records from the database and updating the total time spent ?

 

Are you supposed to update all the sales resources records, if there is an event on single task record?

RajnisfRajnisf

i don't require all the records of sales_resources...

requirement is...Sum of value of Minute_ON_Case of all the closed tasks should be on Sales_resources__c's  field Total_time_Spents_hrs...

RajnisfRajnisf

Now i changed the code as below...

 

errors are:

 

 

EB_Case_Owner_Trigger_Test_Class.myUnitTest() Class 55 1 Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, EB_Case_Owner_Trigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Minutescase: execution of ... New_Task_test.myUnitTest() Class 35 1 Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Minutescase: execution of AfterInsert caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com ab...

 

 

 

 

trigger Minutescase on Task (after insert, after update, before delete, after delete) {
     public set<Id> srids = new Set<Id>();
     public list<Sales_Resources__c> ttt = new List<Sales_Resources__c>();
      aggregateResult[] results  = [Select SUM(Minutes_on_tasks__c)summ From Task where status = 'Completed'];
      Decimal decimalRevenue = 0 ;
            if(results.size() > 0)
            try{
                {
                    String str = '' + results[0].get('summ') ;
                    decimalRevenue = Decimal.ValueOf(str) ;
                    System.debug('decimalRevenue ::::: ' + decimalRevenue) ;
                }
                }catch(System.Exception ae){
                system.debug ('you screwed up! ' + ae.getStackTraceString());
                }
          
                
      if(Trigger.isInsert || Trigger.isUnDelete || Trigger.isUpdate){
      for(Task t: Trigger.new){
            if(t.WhatId<>null && t.Minutes_on_tasks__c <> null && t.IsClosed==True)
            {               
                srids.add(t.WhatId);
            }   
            }
        }
       
         if(Trigger.isDelete){
      for(Task t: Trigger.old){
            if(t.WhatId<>null && t.Minutes_on_tasks__c <> null && t.IsClosed==True)
            {               
                srids.add(t.WhatId);
            }   
            }
        }
       
       
        // timeupdate  = ([Select c.Id,c.Total_Time_Spent_Hrs__c,(Select Id, Minutes_on_tasks__c From Tasks where IsClosed = True)From Sales_Resources__c c Where id in: srids]);
                             
                         
      for(Sales_Resources__c sc : [Select Id, Total_Time_Spent_Hrs__c From Sales_Resources__c where id != null and id in: srids])                        
                                 {
                                
                                sc.Total_Time_Spent_Hrs__c = decimalRevenue;
                                ttt.add(sc);
                                 }             
                        
                            update ttt;
                           
 }

Naidu PothiniNaidu Pothini
trigger Minutescase on Task (after insert, after update, before delete, after delete)
{
  public Set<Id> srids = new Set<Id>();
  public List<Sales_Resources__c> ttt = new List<Sales_Resources__c>();

  if(Trigger.isInsert || Trigger.isUnDelete || Trigger.isUpdate)
  {
    for(Task t: Trigger.new)
    {
      if(t.WhatId <> null && t.Minutes_on_tasks__c <> null && t.IsClosed == True)
      {
        srids.add(t.WhatId);
      }
    }
  }

  if(Trigger.isDelete)
  {
    for(Task t: Trigger.old)
    {
      if(t.WhatId <> null && t.Minutes_on_tasks__c <> null && t.IsClosed == True)
      {
        srids.add(t.WhatId);
      }
    }
  }

  Map<Id, AggregateResult> arMap = new Map<Id, AggregateResult>([SELECT WhatId Id, SUM(Minutes_on_tasks__c) summ FROM Task WHERE Status = 'Completed' AND WhatId IN :srIds GROUP BY WhatId]);

  for(Sales_Resources__c sc : [SELECT Id, Total_Time_Spent_Hrs__c FROM Sales_Resources__c WHERE Id != null AND Id IN :srids])
  {
    sc.Total_Time_Spent_Hrs__c = Decimal.valueOf((arMap.get(sc.Id)).get('summ'));
    ttt.add(sc);
  }
  update ttt;
}

 hey try this.. let me know if it doesnt work....

 

This was selected as the best answer
RajnisfRajnisf

Hi Naidu,

thanks a ton..it is working fine...

 

can u have a look at test class also...

 

 

it is covering only 60%

 

 

@isTest(SeeAllData=true)
private class MinutecaseTrigger {
 
    public static testMethod void testTTrigger(){
      
        
        Account acc  = new Account();
        acc.name = 'Test';
        insert acc;

       
        Contact con = new Contact();
        con.LastName = 'Test Contact';
        con.AccountId = acc.Id;
        insert con;

       
       
       // Task t = new Task();
        //t.OwnerId = UserInfo.getUserId();
      //  t.Subject='Donni';
      //  t.Status='Not Started';
      //  t.Priority='Normal';
      //  t.Minutes_on_tasks__c = 2000;
      //  t.status = 'Completed';
      //  insert t;
 
        Task tas = new Task();
        tas.OwnerId = UserInfo.getUserId();
        tas.Subject='Donni';
        tas.Status='Not Started';
        tas.Priority='Normal';
        tas.Minutes_on_tasks__c = 2000;
        tas.status = 'Completed';
        tas.whoid = con.id;

        Sales_Resources__c sr = new Sales_Resources__c ();
        sr.Name = 'Test Sales Resources';
        insert sr;
        
        Task p = [select id from task where Ownerid = '00500000006scDCAAY' limit 1];
         try{
        delete p;
        }
        Catch(Exception e)
        {

          system.debug('Error:'+e);
        }
 
       
        Test.startTest();
       // insert tas;
        try{
        delete tas;
        }
        Catch(Exception e)
        {

          system.debug('Error:'+e);
        }

        Test.stopTest();
       
     }

}

 

 

 

 

 

 

 

Naidu PothiniNaidu Pothini
@isTest
private class MinutecaseTrigger
{
  public static testMethod void testTTrigger()
  {
    test.startTest();

    Sales_Resources__c sr1 = new Sales_Resources__c (Name = 'Test Sales Resources1');
    Sales_Resources__c sr1 = new Sales_Resources__c (Name = 'Test Sales Resources2');
    insert sr1;
    insert st2;

    Task t1 = new Task(OwnerId = UserInfo.getUserId(), Subject='Donni', Priority='Normal',
                        Minutes_on_tasks__c = 2000, Status = 'Completed', WhatId = sr1.Id);

    Task t2 = new Task(OwnerId = UserInfo.getUserId(), Subject='Donni', Priority='Normal',
                        Minutes_on_tasks__c = 1000, Status = 'Completed', WhatId = sr2.Id);

    Task t3 = new Task(OwnerId = UserInfo.getUserId(), Subject='Donni', Priority='Normal',
                        Minutes_on_tasks__c = 500, Status = 'Not Started', WhatId = sr2.Id);

    insert t1;

    insert t2;

    insert t3;

    t3.Status = 'Completed';

    update t3;

    delete t3;

    Test.stopTest();
  }
}

 try this... 

RajnisfRajnisf
now its 100%...thanks Naidu..
But now the issue is while deploying still overall code coverage is 71%...any idea....?
Naidu PothiniNaidu Pothini

I guess you might need to update other test classes to get the overall coverage to 75 and above....

RajnisfRajnisf

Thanks for replying...

 

can u help me to cover the following code...only orange part m not able to cover...

 

trigger..

trigger SetOpportunityOwnerName on Opportunity (before insert, before update){
 

                 Map<Id,String> userMap = new Map<Id, String>();

 

                 for (User u : [Select Id, Name From User]) {
                                     userMap.put(u.Id, u.Name);

                 }

                 for (Opportunity a : Trigger.New)
                  {
                                     
                            if((a.StageName == 'Closed Won'|| a.StageName == 'Closed Won - AR Settlement') && !a.New_Type__c.contains('MYD') )
                            
                                                          
                                {     
                                     
                                     a.Owner_Name__c = userMap.get(a.OwnerId);
                                }     

                 }
}

 

 

test class

 

@isTest private class Test_SetOpportunityOwnerNameTrigger {

                 static testMethod void myUnitTest() {       

                                     Opportunity opp = new Opportunity(Name = 'TestName',StageName = 'FooName',CloseDate = date.today(),True_Product__c = 'Biz Dev');
                                           

                                                          insert opp;              

                                     system.assertequals(opp.Owner.Name, opp.Owner_Name__c);    

                                     }

}