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
Manohar kumarManohar kumar 

status update on account

Hi, 

I have a scenerio where i need to update account status. I have one object named "Application" which is a child object of Account. 
If any of the Application status is "Rejected" then i have to put rejected on the account status. Which  i am doing with the after insert, after update and after delete trigger on "Application". But my requiremnt is if user changes status manually on accunt, then don't update status ever.

How can i achieve this. Any help will be appriciated.  

Thanks,
Manohar

 

Best Answer chosen by Manohar kumar
Abhishek BansalAbhishek Bansal
Hi Manohar,

Please try with the below code:
trigger updateField on Account (before update){
	for(Account acc : trigger.new){
		if(acc.Automatic_Update__c == true && acc.Status__c != trigger.OldMap.get(acc.Id).Status__c){
			acc.Status__c = trigger.OldMap.get(acc.Id).Status__c;
			acc.Automatic_Update__c = false;
		}
		else if(trigger.OldMap.get(acc.Id).Automatic_Update__c == true && Automatic_Update__c == false){
			acc.Automatic_Update__c = true;
		}
		else if(acc.Automatic_Update__c == false){
			acc.Status__c = trigger.OldMap.get(acc.Id).Status__c;
		}
	}
}

Thanks,
Abhishek Bansal.​

All Answers

Abhishek BansalAbhishek Bansal

Hi Manohar,

You can simply acheive this by writing a trigger on Account object. The tigger will fire on before update and always update the Status using the trigger.oldMap. Please let me know if you need any further help on this.

Thanks,
Abhishek Bansal.

Manohar kumarManohar kumar

Hi Abhishek,

Thanks for your reply. can you pls tell me more about the conditions. I have one boolean field on account called "Automatic update" . Its true by default. From account before update, i can make this false. But my trigger on application also calls this account trigger. 

can you pls let me know in details about this. 

Thanks,
Manohar

Abhishek BansalAbhishek Bansal
Hi Manohar,

You can do one thing here. Set the value of the checkbox as False from the Application trigger and then use the below code on your account trigger:
trigger updateField on Account (before update){
	for(Account acc : trigger.new){
		if(acc.Automatic_Update__c){
			if(acc.Status__c != trigger.OldMap.get(acc.Id).Status__c){
				//The above condition means that your status have been changed manually because the Automatic Update is true so we will revert it back to old value
				acc.Status__c = trigger.OldMap.get(acc.Id).Status__c;
			}
		}
		else{
			//Set it true again in order to handle the future changes
			acc.Automatic_Update__c = true;
		}
	}
}
//Whenever your status is changed form the Appication trigger the Automatic_Update__c will be false and your account trigger will only set it to true.
//Whenver your status is changed manually the Account Trigger will revert back the old status


Please let me know if you need any information related to this.

Thanks,
Abhishek Bansal. 

Manohar kumarManohar kumar

Hi Abhishek, 

One quick question, if account is manually chaged and again any Application is created. Then it will update account, but i dont want that. If its manually updated once then it shouldn't update ever. 

Thnaks, 
Manohar

Abhishek BansalAbhishek Bansal
Hi Manohar,

Can you please list down you complete requirement here. Please include all the cases so that it would be easy to figure out a solution for you.

Thanks,
Abhishek Bansal.
Manohar kumarManohar kumar

Hi Abhishek, 

Status on the account should update based of the application status values. if user manually changes status on the account then dont change status of that account ever. I had mention this in my question.

please let me know if anything else you need, I tried adding that condition in you above code. But i am not sure where to put that.

Thanks,
Manohar

 

Abhishek BansalAbhishek Bansal
Hi Manohar,

Please try with the below code:
trigger updateField on Account (before update){
	for(Account acc : trigger.new){
		if(acc.Automatic_Update__c == true && acc.Status__c != trigger.OldMap.get(acc.Id).Status__c){
			acc.Status__c = trigger.OldMap.get(acc.Id).Status__c;
			acc.Automatic_Update__c = false;
		}
		else if(trigger.OldMap.get(acc.Id).Automatic_Update__c == true && Automatic_Update__c == false){
			acc.Automatic_Update__c = true;
		}
		else if(acc.Automatic_Update__c == false){
			acc.Status__c = trigger.OldMap.get(acc.Id).Status__c;
		}
	}
}

Thanks,
Abhishek Bansal.​
This was selected as the best answer
Manohar kumarManohar kumar
Hi Abhishek, Thnak you so much for helping.