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
SurpriseSurprise 

Case trigger

Hi Friends,

 

I have trigger on case object and I am only trying to count  the number of cases where assisted has been set to yes.

I am only querying using count function which counts the number of records and then I am assigning that number to field on the case object  and finally I insert.However,When get the following error.

 

 

Apex trigger calculate caused an unexpected exception, contact your administrator: calculate: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, calculate: maximum trigger depth exceeded Case trigger event AfterInsert for [500c0000001ZohW] Case trigger event AfterInsert for [500c0000001ZohX] Case trigger event AfterInsert for [500c0000001ZohY] Case trigger event AfterInsert for [500c0000001ZohZ] Case trigger event AfterInsert for [500c0000001Zoha] Case trigger event AfterInsert for [500c0000001Zohb] Case trigger event AfterInsert for [500c0000001Zohc] Case trigger event AfterInsert for [500c0000001Zohd] Case trigger event AfterInsert for [500c0000001Zohe] Case trigger event AfterInsert for [500c0000001Zohf] Case trigger event AfterInsert for [500c0000001Zohg] Case trigger event AfterInsert for [500c0000001Zohh] Case trigger event AfterInsert for [500c0000001Zohi] Case trigger event AfterInsert for [500c0000001Zohj] Case trigger event AfterInsert for [500c0000001Zohk] Case trigger event AfterInsert for [500c0000001Zohl]: []: Trigger.calculate: line 10, column 1

 

 

 

 

 

 

Can somebody help by correcting this trigger.?

 

Trigger calculate on Case(after Insert)
{
integer number1=0;
if(trigger.IsInsert)
{

case c=new case();
number1=[select count() from case where assisted__c='yes'];
c.Count_of_Inquires_Answered__c=number1;
insert c;

}

Eugene NeimanEugene Neiman

Ouch.  This is a case trigger after insert.  Ok, you have a non-null set of case records passed in.  But rather than doing something with the set you passed in you call 

 

case c=new case();

...and...

insert c;

 

 

 

But insert c call this very trigger, which in turn adds a new case, which in turn calls this trigger, ad infinitum.

 

 

 

 

Naidu PothiniNaidu Pothini
Trigger calculate on Case(after Insert)
{
    if(trigger.IsInsert)
    {
   	Boolean hasnewRecord = false;

	for(case c : Trigger.new)
	{
   	    if(c.Assisted__c)
	    {
		hasNewRecord = true;
	    }
	}

	if(hasNewRecord)
	{
	    case c=new case();
	    c.Count_of_Inquires_Answered__c = [select Id from case where assisted__c='yes'].size();
c.Assisted__c = 'No'; insert c; } } }

 You might try something like this...  but the above code creates new case record with count only if there is atleast a record with Assisted__c = Yes is inserted.

 

 

 

 

SurpriseSurprise

Thanks Eugene for explaning why it is happening.
Thanks Naidu for your reply.

 

  if(c.Assisted__c)  is a picklist with yes and no in it ,therefore it gives me an error .I changed it to if(c.Assisted__c='yes')

 

 

Why in the end your are setting assisted__c to no.However,I have tried your code but it is not showing me the count of records in the Count_of_Inquires_Answered__c field.

 

Any more idea.Why the field  Count_of_Inquires_Answered__c field is not populated with the number if ID's.

 

Trigger calculate on Case(after Insert)
{
    if(trigger.IsInsert)
    {
       Boolean hasnewRecord = false;

    for(case c : Trigger.new)
    {
           if(c.Assisted__c)
        {


        hasNewRecord = true;
        }
    }

    if(hasNewRecord)
    {
        case c=new case();
        c.Count_of_Inquires_Answered__c = [select Id from case where assisted__c='yes'].size();
        c.Assisted__c = 'No';
        insert c;    
    }

 

 

 

Thanks

Surprise

Naidu PothiniNaidu Pothini

You should try creating a new record with Assisted__c = 'Yes'...

 

I am creating a new record with count value and setting its field Assisted__c to No so that this insert will not create a new record.

 

Can you please explain why you are creating a new case record with just the count of cases?

SurpriseSurprise

since we will be pulling case related data from salesforce on a daily basis and move it to the new system using automation.We will be using this for analysis.So I was thinking when somebody creates a new case and assisted has been selected to yes then we have count of the number of cases which have been assisted and show that count to the user.This count will keep on changing as new inquiries are inserted and data pulled out of salesforce will reflect change in total count.


Another thing,I have record types on the object ,could that be the reason that salesforce is no showing the count of records in  the field.

Can u please suggests?

Naidu PothiniNaidu Pothini

So if they have 100 inserts with Assisted__c = 'Yes', then you would be creating 100 cases with count value.

 

how come the user know which case to look at when there are like 100 records with different count values on them?

 

Is there anything like updating the same case everytime a new case is inserted ( with assisted = 'Yes')??

SurpriseSurprise

For example, if there are 100 record with assisted__c='yes' condaition then I want  to ensure that any user who logs in should see 100  count in the count_of_inquires field.


Now if it becomes 120 then all users shoul see 120 in the field.

 

It is like count functionality which you get when we use rollup summary.

 

I do not want different users to see different value in the count_of_inquires fied at all.

 

No there is no update functionality on this.

 

Please let me know if I am not clear enough

 

 

 

 

 

 

Naidu PothiniNaidu Pothini

Do you need to show the count when ever the user creates a new case record with Assisted= yes? If so, do you want the user to see the count on the same record which he created?

 

or if you create a new Case record, how would the user know that you created a new case record and should look into that record to know the client?

SurpriseSurprise

Thanks,

 

For example, we have three users who are basically salesforce rep in the company.Now ,when they log in the salesforce and click on the case tab then count field on the case object should show the totlal number of cases where asssted__c is yes,count of existing cases in the system and if they add one more case which meets condition assisted__c='yes' then the total in the count fiels should increase by one and all the users should see that number in the count field.

 

So all the users when they login will see that count in the count field.And this count field should get updated as users create new cases with assisted="yes"

 

So that when we pull out data from case ,we get latest data and use elsewhere outside salesforce system.

 

We are not worrying about  contacts or accounts.

We need total number of  cases with assisted="yes"

 

We are doing not for the salesforce users,infact to get some data out of salesforce.

 

 

 

 

 

 

 

 

 

 

 

 

SurpriseSurprise

Hi,

 

Your code seems to be running fine as I can see in debug logs that it is counting the number of records in which assisted__c='yes' .As ofd now there are 3 records with asisted__c='yes';

 

However,It does not assign that value of 3 to the field count on the case object.How can we achieve that?

 

 

 

Naidu PothiniNaidu Pothini
For example, we have three users who are basically salesforce rep in the company.Now ,when they log in the salesforce and click on the case tab then count field on the case object should show the totlal number of cases where asssted__c is yes,count of existing cases in the system and if they add one more case which meets condition assisted__c='yes' then the total in the count fiels should increase by one and all the users should see that number in the count field.

If you go to the case tab, you can see the recent case records, but i could not understand what you mean by count field on Case object in here... You will have the Count field on case records and you can update the cases.  am i missing anything here...??

 

were you able to see a count field on the Case Tab (not case object)

SurpriseSurprise

I am sorry for the confusion.

 

When you click on the case tab ,you see a list of records and new button,you click on the new button and new case opens in which you type your infromation.Here on this case object I have count field along with the rest of the other case fields.

 

This count field should show the sum of all the cases where assisted__c='yes';

 

.As of now I am only inserting not updating

 

 

 

 

 

 

Naidu PothiniNaidu Pothini
Trigger calculate on Case(before Insert)
{
    if(trigger.IsInsert)
    {
    	Integer count = 0;

	   	Boolean hasnewRecord = false;

		for(case c : Trigger.new)
		{
	   	    if(c.Assisted__c == 'Yes')
		    {
	    		hasNewRecord = true;
				count++;
		    }
		}

		if(hasNewRecord)
		{
			Integer count2 = [SELECT Id FROM CASE WHERE Assisted__c = 'YES'].size() + Count;

		    for(Case c : Trigger.new)
		    {
	    		if(c.Assisted__c == 'Yes')
			    {
		    		c.Count_of_Inquires_Answered__c = count2;
			    }
		    }	
		}
    }
}

 try this.

SurpriseSurprise

 Thanks a lot for your help

I am new to salesforce so sorry for the trouble.This works just fine.If I upload like 50 records from dataloader,will it count all the records and show in the count field

 

Before insert will take care of the new records.However,If somebody saves a record and later on updates it as case will take time to resolve.So I will have to incorporate update functionality as well.

 

Can I incorporate update functionality in the same trigger.

 

This is what I am thinking of putitng in the trigger for update functionality.

 

If case is updated

 

Previosly the value is not assisted and now the case has been edited and values changed from not assisted to assisted.

If this condition meets then add 1 to the count field which increases its total.

 

 

 

 

 

.

 

SurpriseSurprise

Hi,

 

We have calculated the total number of cases solved.We did that based on some condition.Is there a way to count the total

number of cases in the system.?

 

I am not sure what condition we can put to calculate total number of cases in the system.We have record types as well on the case object .

 

 

 

 

 

Naidu PothiniNaidu Pothini
Trigger calculate on Case(before Insert, before update)
{
	Integer count = 0;

    if(trigger.IsInsert || trigger.isUpdate)
    {
    	Integer count = 0;

	   	Boolean hasnewRecord = false;

		if(trigger.oldMap <> null)
		{
			for(case c : trigger.new)
			{
				if(c.Assisted__c == 'Yes' && trigger.oldMap.get(c.Id).Assisted__c  <> 'Yes')
			    {
		    		hasNewRecord = true;
					count++;
			    }
			}
		}
		else
		{
			for(case c : Trigger.new)
			{
		   	    if(c.Assisted__c == 'Yes')
			    {
		    		hasNewRecord = true;
					count++;
			    }
			}
		}

		if(hasNewRecord)
		{
			Integer count2 = [SELECT Id FROM CASE WHERE Assisted__c = 'YES'].size() + Count;

		    for(Case c : Trigger.new)
		    {
	    		if(c.Assisted__c == 'Yes')
			    {
		    		c.Count_of_Inquires_Answered__c = count2;
			    }
		    }	
		}
    }
}

 can you try this. this would work as well for update.

 

 

query the total number of records in case object with out any conditons, you would get the total number of cases.

Naidu PothiniNaidu Pothini
Trigger calculate on Case(before Insert, before update)
{
	Integer count = 0;

	List<Case> newCases = new List<Case>();

    if(trigger.IsInsert || trigger.isUpdate)
    {
		if(trigger.oldMap <> null)
		{
			for(case c : trigger.new)
			{
				if(c.Assisted__c == 'Yes' && trigger.oldMap.get(c.Id).Assisted__c  <> 'Yes')
			    {
		    		newCases.add(c);
			    }
			}
		}
		else
		{
			for(case c : Trigger.new)
			{
		   	    if(c.Assisted__c == 'Yes')
			    {
					newCases.add(c);
			    }
			}
		}

		if(newCases.size() > 0)
		{
			Integer count2 = [SELECT Id FROM CASE WHERE Assisted__c = 'YES'].size() + newCases.size();

		    for(Case c : newCases)
		    {
	    		if(c.Assisted__c == 'Yes')
			    {
		    		c.Count_of_Inquires_Answered__c = count2;
			    }
		    }	
		}
    }
}

 try this.. 

SurpriseSurprise

Thanks Naidu,

 

It is awesome ,I am so thankful to you,It looks like you are simply too good in Apex .Can u guide me to the right approach in learning apex language?.

 

I have one more question.

 

 Is there a way to calculate the total number of cases by owner?.

 

For example if user1 has 10 Total cases then we show in a field the number of cases which he has.

 

and if user2 has 5 cases then we show total number of cases which he owns.

 

 

 

Will it be possible to do that  through trigger.?

 

I know I am asking too many questions but if u can guide me with the right approach?

 

 

 

 

 

 

 

Naidu PothiniNaidu Pothini
Trigger calculate on Case(before Insert, before update)
{
	Integer count = 0;

	List<Case> newCases = new List<Case>();

	Map<Id, Integer> ownerCount = new Map<Id, Integer>();

    if(trigger.IsInsert || trigger.isUpdate)
    {

		if(trigger.oldMap <> null)
		{
			for(case c : trigger.new)
			{
				if(c.Assisted__c == 'Yes' && trigger.oldMap.get(c.Id).Assisted__c  <> 'Yes')
			    {
		    		newCases.add(c);

		    		if(ownerCount.get(c.OwnerId) <> null)
		    		{
	    				Integer count = ownerCount.get(c.OwnerId)+1;
	    				ownerCount.put(ownerId, count);
		    		}
		    		else
		    		{
		    			Integer count = 1;
		    			ownerCount.put(ownerId, count);
		    		}
			    }
			}
		}
		else
		{
			for(case c : Trigger.new)
			{
		   	    if(c.Assisted__c == 'Yes')
			    {
					newCases.add(c);

					if(ownerCount.get(c.OwnerId) <> null)
		    		{
	    				Integer count = ownerCount.get(c.OwnerId)+1;
	    				ownerCount.put(ownerId, count);
		    		}
		    		else
		    		{
		    			Integer count = 1;
		    			ownerCount.put(ownerId, count);
		    		}
			    }
			}
		}

		if(newCases.size() > 0)
		{
			Integer count2 = [SELECT Id FROM CASE WHERE Assisted__c = 'YES'].size() + newCases.size();
			Map<Id, AggregateResult> arMap = new Map<Id, AggregateResult>([SELECT OwnerId Id, Count(Id) cnt FROM Case WHERE Assisted__C = 'Yes' GROUP BY OwnerId]);

		    for(Case c : newCases)
		    {
	    		if(c.Assisted__c == 'Yes')
			    {
		    		c.Count_of_Inquires_Answered__c = count2;
		    		c.Count_of_Inquires_Owned__c = Integer.valueOf((arMap.get(c.OwnerId)).get('cnt')) + ownerCount.get(c.OwnerId); // new field which shows count of cases owned by owner
			    }
		    }	
		}
    }
}

 try this.

SurpriseSurprise

Hi Naidu ,

 

I tried your trigger to calculate the the cases owned by owner's.However,I am gettting below given error.

 

Apex trigger calculation caused an unexpected exception, contact your administrator: calculation: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.calculation: line 63, column 1.

 

 

Can u also please explain last bit of code starting from ?

 

if(newCases.size() > 0)
		{
			Integer count2 = [SELECT Id FROM CASE WHERE Assisted__c = 'YES'].size() + newCases.size();
			Map<Id, AggregateResult> arMap = new Map<Id, AggregateResult>([SELECT OwnerId Id, Count(Id) cnt FROM Case WHERE Assisted__C = 'Yes' GROUP BY OwnerId]);

		    for(Case c : newCases)
		    {
	    		if(c.Assisted__c == 'Yes')
			    {
		    		c.Count_of_Inquires_Answered__c = count2;
		    		c.Count_of_Inquires_Owned__c = Integer.valueOf((arMap.get(c.OwnerId)).get('cnt')) + ownerCount.get(c.OwnerId); // new field which shows count of cases owned by owner
			    }
		    }	
		}
    }

 

Thanks,

Surprise

Naidu PothiniNaidu Pothini
Count_of_Inquires_Owned__c 

 did you create this field?

Naidu PothiniNaidu Pothini
        if(arMap.get(c.OwnerId) <> null)
	{
		c.Count_of_Inquires_Owned__c = Integer.valueOf((arMap.get(c.OwnerId)).get('cnt')) + ownerCount.get(c.OwnerId);
	}
	else
	{
		c.Count_of_Inquires_Owned__c = ownerCount.get(c.OwnerId);
	}

 replace the line with these...