You need to sign in to do that
Don't have an account?
Selva
before update trigger on account
Hi,
I am new to Apex trigger and Apex class.
I would like to know how to replace the old values with new values of Account object,if some criteria is met(for example, if the user who is updating the account is System administrator, update the old values(account name,account owner) with new values). I have to do it in the before update trigger. Do i need to create Apex class for updating the values?
my sample trigger code:
trigger tocAccountTrigger on Account (before update) {
if (Trigger.isBefore) {
if (Trigger.isUpdate) {
for (Account a : Trigger.new) {
// update the old values with the new values if the criteria met
if (Trigger.isBefore) {
if (Trigger.isUpdate) {
for (Account a : Trigger.new) {
// update the old values with the new values if the criteria met
} } } }
To update fields, just set the field properties of your accounts:
Message Edited by Selva on 05-27-2008 04:45 AM
Message Edited by Selva on 05-27-2008 04:47 AM
Can't we update the read-only value ?
My requirment is below :
Name
Phone
Email
Fax
ShippingStreet
ShippingCity
ShippingState
ShippingPostal
ShippingCountry
Account Owner
Segment
Current User performing the update is not “System Administrator”.
Current Account has the Company Id populated and the company name is “IBM”.
I tried the following code, but it failed since the values which i tried to replace are read-only.
Isn't it possible to modify my sample code and meets the above requirement?
for (Account a : Trigger.new) {
// you can use the trigger.old for the comparison logic - but when updating values, leverage the trigger.new collection:
a.description = 'you can set this value';
}
Hi Sir,
setting the other way around also doesn't work!! :-(
newAccount.Name = oldAccount.Name;
Could you please modify the below code to meet my requirement?
The logic is that to replace the existing values for the mentioned fields with the new values that just entered by user(system admin) for those corresponding fields, if the condition met. thats it..
The above assignment doesn't update the old value with new value. Tried with opposite way, it does throw the exception saying Read-Only field can't be updated.
Awaiting for the modified code which meets the requirement.
Yes Simon, you are right!
If the criteria(selected source system as IBM ,but IBM id is not entered by the user) is not met, then it should the error "IBM Id should be present". thats behaving as expected.
If the above condition is met and the user updating the page is not system admin, then trigger shouldn't allow them to update the values with the error message saying that only system admin can update the values or something like that. Is it possible?
I expecting my code to be modified with someone and it should be perfectly meet the requirement. As i am new to this apex, i don't think my code is 100% right.
kindly check and give the update , if possible