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
mustapha L 1mustapha L 1 

FOR loops iteration

Hi All

i'm doing the following:


trigger AutoCreateMaint on QuoteLineItem (before insert) {
    for (QuoteLineItem quoteli : Trigger.new){
        Boolean IsMaintenance = False;
        //check if the product has Ismaintenance box Checked
        IsMaintenance = [OSQL request]
                if (isMaintenance)
                                         {Process}
}
}

I have a Problem with the FOR loop which is supposed to run ONCE but always run twice...of course the second time it runs the OSQL query fails and tigger fails !
i do not undesrtand reason why the LOOP run twice ?

to try to fix it, i have added a "break" but even like this the LOOP runs twice instead to exit

this is what i have each time it runs the second iteration:
"17:46:35:086 FATAL_ERROR System.QueryException: List has no rows for assignment to SObject"

could you assist please?

Many thanks
 
Best Answer chosen by mustapha L 1
Amit Chaudhary 8Amit Chaudhary 8
Its look like your trigger become recusive. Please check below blog to fix this issue
http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html

You can create a class with a static Boolean variable with default value true. In the trigger, before executing your code keep a check that the variable is true or not. Once you check make the variable false.
Apex Class with Static Variable
 
public class TriggerHandler
{
     public static Boolean isFirstTime = true;
}
Then update your trigger like below
trigger AutoCreateMaint on QuoteLineItem (before insert) 
{
	if(TriggerHandler.isFirstTime)
    {
		TriggerHandler.isFirstTime = false;

		for (QuoteLineItem quoteli : Trigger.new)
		{
			Boolean IsMaintenance = False;
			//check if the product has Ismaintenance box Checked
			IsMaintenance = [OSQL request]
					if (isMaintenance)
											 {Process}
		}
	}	
}

Please let us know if this iwll help you,

Thanks,
Amit Chaudhary

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Its look like your trigger become recusive. Please check below blog to fix this issue
http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html

You can create a class with a static Boolean variable with default value true. In the trigger, before executing your code keep a check that the variable is true or not. Once you check make the variable false.
Apex Class with Static Variable
 
public class TriggerHandler
{
     public static Boolean isFirstTime = true;
}
Then update your trigger like below
trigger AutoCreateMaint on QuoteLineItem (before insert) 
{
	if(TriggerHandler.isFirstTime)
    {
		TriggerHandler.isFirstTime = false;

		for (QuoteLineItem quoteli : Trigger.new)
		{
			Boolean IsMaintenance = False;
			//check if the product has Ismaintenance box Checked
			IsMaintenance = [OSQL request]
					if (isMaintenance)
											 {Process}
		}
	}	
}

Please let us know if this iwll help you,

Thanks,
Amit Chaudhary
This was selected as the best answer
mustapha L 1mustapha L 1
MANY thanks Amit, that looks great !!!