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
Sunny SohailSunny Sohail 

Trigger to override "Billingstate" on accounts object.

Hello,

I am fairly new to writting triggers and have a question regarding overiding fields via trigger. Basically what I wantr to happen is to force users to input a "billingstate" using the 2 letter abbreviation. For example if someone inputs "UTAH" I want the "Billingstate" to update to "UT". I know that this is somewhat possible using validation rules but I really want to understand how this is done via trigger. The below is a simple code that will only affects accounts in Utah and this is what I have, but this code does not seem to work. Basically the below code should force the state to update "UTAH" to "UT" upon creation and upon update to the record.

trigger ForceAccountstate on Account (before insert,after update) {
    for (Account AccountInLoop : Trigger.new) {
if (Billingstate = UTAH)
Billingstate = 'UT';
    }
}
Best Answer chosen by Sunny Sohail
ManojjenaManojjena
HI Sunny ,

Check below commts in code you will find the solution .
 
trigger ForceAccountstate on Account (before insert,after update) {
    for (Account AccountInLoop : Trigger.new) {
	    //Previous code 
	    if (Billingstate = UTAH)
		 //Modified code addes the account variabel to get the field value and wrapped with quote to string value 
	    if (AccountInLoop.Billingstate = 'UTAH')
		    //Previous code 
		    Billingstate = 'UT';
			//Added account varable to assign value to field 
			AccountInLoop.Billingstate = 'UT';
    }
}

Let me know if it helps 

Thanks 
Manoj

All Answers

ManojjenaManojjena
Hi Sunny,
Try with below code it wil help !!
 
trigger ForceAccountstate on Account (before insert,before update) {
    for (Account acc : Trigger.new) {
		if (acc.Billingstate = 'UTAH')
			acc.Billingstate = 'UT';
		}
	}
}
Let me know if it helps 

Thanks 
Manoj
 
Sunny SohailSunny Sohail
Hi Manoj thank you for your response! Please see the error that i continue to get.
User-added image
ManojjenaManojjena
Hi Sunny ,

Try with below code  it wil work .
trigger ForceAccountstate on Account (before insert,before update) {
    for (Account acc : Trigger.new) {
		if (acc.Billingstate = 'UTAH'){
			acc.Billingstate = 'UT';
		}
	}
}
LEt me know if it helps !!
Thanks 
Manoj
Sunny SohailSunny Sohail
Awesome it worked. So using your code I was still getting a "Boolean" error so what i did was added a double == on Line 3 so im all set. So it looks like you did a few things differently then what i did. On lines 2,3,4 you used "acc" where i did not use ACC why does this make a difference?
ManojjenaManojjena
HI Sunny ,

Check below commts in code you will find the solution .
 
trigger ForceAccountstate on Account (before insert,after update) {
    for (Account AccountInLoop : Trigger.new) {
	    //Previous code 
	    if (Billingstate = UTAH)
		 //Modified code addes the account variabel to get the field value and wrapped with quote to string value 
	    if (AccountInLoop.Billingstate = 'UTAH')
		    //Previous code 
		    Billingstate = 'UT';
			//Added account varable to assign value to field 
			AccountInLoop.Billingstate = 'UT';
    }
}

Let me know if it helps 

Thanks 
Manoj
This was selected as the best answer
Sunny SohailSunny Sohail
awesome thank you so much!!