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
srikanth11srikanth11 

very very urgent issue

i have written this trigger on appointments and when i am saving bulk records i am getting too many soql errors plz help me

 

 

}

 

trigger Dev_Appt_Max_slot_Error on Appointment__c (before insert,before update) 
{
    Set<Id> doctorId = new Set<Id>();
    Set<Id> branchId = new Set<Id>();
    Set<Date>  appDate = new Set<Date>();
    set<string> appampm = new set<string>();
    set<string> apphour = new set<string>();
    set<string> appmin =new set<string>();
        for(appointment__c a:trigger.new)
    {
        doctorId.add(a.doctor__c);
        branchId.add(a.branch__c);
        appDate.add(a.Appt_Date__c);
        appampm.add(a.Appt_AM_PM__c);
        apphour.add(a.Appt_Hour__c);
        appmin.add(a.Appt_Minute__c);
    }
     branch__c b=[select id,Dev_Check_Branch_Has_Max_Appt_Book_Slot__c   from branch__c where id in: branchId];
    List<appointment__c> a1 =[select  id,Dev_Maximum_Book_Slot__c from appointment__c where doctor__c in: doctorId and branch__c in: branchId and Appt_Date__c in: appDate and Cancelled__c=false and Appt_AM_PM__c in:appampm and Appt_Hour__c in:apphour and Appt_Minute__c in:appmin];
    for(appointment__c a:trigger.new)
    {
        
       if(a1.size()>a.Dev_Max_Appt_Book_Slot_for_Blocking__c && a.Dev_Check_Appt_is_Blocked_for_Max_Slot__c =='TRUE' &&b.Dev_Check_Branch_Has_Max_Appt_Book_Slot__c=='TRUE')
        {
            a.addError('The maximum booking slot limit is exceeded');

        }



    }

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Following was my trigger which was recursively called, Problem here was Lead record been updated inside a Lead trigger, so same trigger was called recursively:

 

trigger UpdateLead on Lead (after insert, after update)
{
	{
		list<Lead> lstLead = new list<Lead>();
		for(Lead objLead: lstLead)
		{ 
			if(objLead.Closed_date__c != null)
			{
				objLead.Closed_date__c = date.today();
			}
		}
		update lstLead;
	}
}

 Solution for this is to use a Static boolean and in trigger execute the code only if the this variable is false, once the trigger is executed make that varable as true.

No what will happen is whenever trigger is called again, its execution would be stopped.
Static method with test case:

public class StaticClassUpdateLead 
{
	public static boolean isActive = false;
	public static boolean hasAlreadyDone()
	{ 
		return isActive;
	}
	public static void setAlreadyDone()
	{
		isActive = true;
	}
	public static testMethod void testStaticClassUpdateLead()
	{
		StaticClassUpdateLead objStaticClassUpdateLead = new StaticClassUpdateLead();
		StaticClassUpdateLead.hasAlreadyDone();
		StaticClassUpdateLead.setAlreadyDone();
	}
}

 

 Trigger using above class:

trigger UpdateLead on Lead (after insert, after update)
{
	if(!StaticClassUpdateLead.hasAlreadyDone())
	{
		list<Lead> lstLead = new list<Lead>();
		for(Lead objLead: lstLead)
		{ 
			if(objLead.Closed_date__c != null)
			{
				objLead.Closed_date__c = date.today();
			}
		}
		StaticClassUpdateLead.setAlreadyDone();
		update lstLead;
	}
}

 check the above code how static variable is used.

Understand the situation and method before using it.

 

 

All Answers

SrikanthKuruvaSrikanthKuruva

i hope you are testing the trigger using dataloader right? if so go to settings menu and set the batch size as 100.

then test it and see.

Rahul SharmaRahul Sharma

I think your trigger is been called more than once, thats why number then number of queries may be getting added after every call.

Check the above, if its the case then,

Try changing event from before to after and if it doesn't help, use static method to avoid the error.

srikanth11srikanth11

hi i am not using data loader  i have another functionality on appointments called from to which creartes bulk appoiyments at a time so this trigger is getting called when they are inserted so can u tell me a way how to not call this trigger

Rahul SharmaRahul Sharma

Firstly when you get error, check in system log / debug log that if your code is called more than once.

also do check how many soql statements are retrived every call.

 

I too had faced this kind of issue, when there were series of triggers being called one from other.

The most common problem is due to trigger getting called multiple times.

srikanth11srikanth11

sorry i marked it as solution by mistake so what u said is write there are many triggers on appoitments and they are not throwing errors when the from to functionality is executed but when i active this trigger only its throwing error i have checked the debug log also my trigger is only throwing error plz help how to resolve this issue

Rahul SharmaRahul Sharma

I had unmarked it,
FYI : You can unmark it as solution by a option "Not a solution".

Make your trigger as active and then check how many times your trigger is been called.

Also check if they are called multiple times from same place or from different places.

srikanth11srikanth11

i tried changing it after update but it was not resolved u mentioned some static method can u plz tell me how to do that

Rahul SharmaRahul Sharma

Did you checked the other things which i mentioned in previous post?

srikanth11srikanth11

yes i checked the debug log i changed before update to after update but it dint work still its throwing error

Rahul SharmaRahul Sharma

I asked you to check which trigger is invoked multiple times.

And also check if this trigger occurs recursively.

If yes then you can use a static class to avoid it.

Check below links to control recursion:

1. Recursion helper

2. Recursion helper

srikanth11srikanth11

yes rahul my trigger is only calling recursevely i checked it and please write a sample code of the static method in my trigger plz

 

i mean how to create a class how to call it in a trigger plz help me in this

Rahul SharmaRahul Sharma

Following was my trigger which was recursively called, Problem here was Lead record been updated inside a Lead trigger, so same trigger was called recursively:

 

trigger UpdateLead on Lead (after insert, after update)
{
	{
		list<Lead> lstLead = new list<Lead>();
		for(Lead objLead: lstLead)
		{ 
			if(objLead.Closed_date__c != null)
			{
				objLead.Closed_date__c = date.today();
			}
		}
		update lstLead;
	}
}

 Solution for this is to use a Static boolean and in trigger execute the code only if the this variable is false, once the trigger is executed make that varable as true.

No what will happen is whenever trigger is called again, its execution would be stopped.
Static method with test case:

public class StaticClassUpdateLead 
{
	public static boolean isActive = false;
	public static boolean hasAlreadyDone()
	{ 
		return isActive;
	}
	public static void setAlreadyDone()
	{
		isActive = true;
	}
	public static testMethod void testStaticClassUpdateLead()
	{
		StaticClassUpdateLead objStaticClassUpdateLead = new StaticClassUpdateLead();
		StaticClassUpdateLead.hasAlreadyDone();
		StaticClassUpdateLead.setAlreadyDone();
	}
}

 

 Trigger using above class:

trigger UpdateLead on Lead (after insert, after update)
{
	if(!StaticClassUpdateLead.hasAlreadyDone())
	{
		list<Lead> lstLead = new list<Lead>();
		for(Lead objLead: lstLead)
		{ 
			if(objLead.Closed_date__c != null)
			{
				objLead.Closed_date__c = date.today();
			}
		}
		StaticClassUpdateLead.setAlreadyDone();
		update lstLead;
	}
}

 check the above code how static variable is used.

Understand the situation and method before using it.

 

 

This was selected as the best answer
srikanth11srikanth11

is there a test case required for the class we have writtewn for the static method

srikanth11srikanth11

plz sir if there is a test case for that plz give it because the method u sead me is working

Rahul SharmaRahul Sharma

Yes

srikanth11srikanth11

sir can u give me the test case code

Rahul SharmaRahul Sharma

I have edited the post and included the test case which would cover the class 100%.

Please refer the post

srikanth11srikanth11

sir which post u edited it  i cannot find can u pls give it again sorry sir for causing u inconvinience as i have urgent client call

Rahul SharmaRahul Sharma

Static method with test case:

public class StaticClassUpdateLead 
{
	public static boolean isActive = false;
	public static boolean hasAlreadyDone()
	{ 
		return isActive;
	}
	public static void setAlreadyDone()
	{
		isActive = true;
	}
	public static testMethod void testStaticClassUpdateLead()
	{
		StaticClassUpdateLead objStaticClassUpdateLead = new StaticClassUpdateLead();
		StaticClassUpdateLead.hasAlreadyDone();
		StaticClassUpdateLead.setAlreadyDone();
	} 
}

 

srikanth11srikanth11

rahul u have been a great helpful to me thank u very much for resolving the issue

Rahul SharmaRahul Sharma

You are welcome, glad that its been solved! :)