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
cloud lendingcloud lending 

calling trigger only one time, if the trigger is new

in my Scenario, i have picklist field called status, in that picklist field i have multiple values. when i select the picklist field status='approved' and click on the save button then my trigger will run.
here is my trigger:
 for (genesis__Applications__c app1: Trigger.new)
         {     
           
               
              if(app1.genesis__Status__c == 'Approved')
              {
                  
                    application.getDetails(app1.Id);
              }
        }

my requirement is like: this trigger i need to call one time when the trigger is new, suppose if select the status=approved two times and clicking save button, i am getting the result two times with same values.for second time calling the trigger i want to check the trigger whether it is old or new. if it is new , then my class method i need to cal.if it is old then that method no need to cal. can any help me out from this scenario
Vijay NagarathinamVijay Nagarathinam
Add one if condiotion in your trigger like this
 
if(Trigger.isBefore && Trigger.isInsert){
	for (genesis__Applications__c app1: Trigger.new){     
         if(app1.genesis__Status__c == 'Approved'){
              application.getDetails(app1.Id);
          }
     }
}

This trigger will fired only in before insert event .

Let me know if you need any help regarding this.

Thanks,
Vijay

 
cloud lendingcloud lending
the trigger is after update: i have written already that if condition
 if(trigger.isUpdate && trigger.isAfter)
    {
    for (genesis__Applications__c app1: Trigger.new)
         {     
           
               
              if(app1.genesis__Status__c == 'Approved')
              {
                  
                    application.getDetails(app1.Id);
              }
        }
}
but my requirement is: for second time clicking save button with status=approved. i dont want to call this method.
for first time saving, i am getting some result. for second time also i am getting same result.
Vijay NagarathinamVijay Nagarathinam
Hi,

Try the below code it will work one time only,
 
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}


Trigger code :

trigger updateTrigger on anyObject(after update) {
	if(trigger.isUpdate && trigger.isAfter){
		if(checkRecursive.runOnce()){
			for (genesis__Applications__c app1: Trigger.new){     
				if(app1.genesis__Status__c == 'Approved'){
					application.getDetails(app1.Id);
				}
			}
		}
	}
}

Thanks,
Vijay
Vijay NagarathinamVijay Nagarathinam
Use the trigger code like this,
 
trigger updateTrigger on anyObject(after update) {
	if(trigger.isUpdate && trigger.isAfter){
		for (genesis__Applications__c app1: Trigger.new){  
			if(checkRecursive.runOnce()){
				if(app1.genesis__Status__c == 'Approved'){
					application.getDetails(app1.Id);
				}
			}
		}
	}
}

 
cloud lendingcloud lending
recent code also calling the trigger two times
 
cloud lendingcloud lending
I have created a class with ur below code

public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}
Vijay NagarathinamVijay Nagarathinam
Hi,

Use the trigger and apex class both.

 
cloud lendingcloud lending
Yes . i used  both, but i am getting same issue
Abhishek Vishwakarma 5Abhishek Vishwakarma 5

Hi,

you can have a Checkbox , whenever you are inserting make this checkbox value = true.

if(app1.genesis__Status__c == 'Approved' && checkbox ! = true)
check this condition it will work.
cloud lendingcloud lending
means, i have to create one boolean variable in trigger
Abhishek Vishwakarma 5Abhishek Vishwakarma 5

yes..
Abhishek Vishwakarma 5Abhishek Vishwakarma 5

Hi,

Please select the best answer so it should be helpful to others.

Thanks,
Abhishek