Hi SS, I after update trigger records are readoly and We can't change field value in after update trigger of Sobject hence go with before trigger. Given code will definately solve your problem. let me konow if it not resolving your problem.
My Requirment is in account object create one custom field like test__c when i am edit the Sobject field Name after saving the field update the same name in custom field tyest__C ...
After context your trigger.new and Trigger.old always read only as it is saved to database .So you can not do DML on Trigger.new /old .That is the reason you need to query to do any modification on the particular record .
Please use below code and let me know if it helps you.
trigger updateFieldValueTrigger on Account (before update) {
for (Account accRecord : Trigger.new) {
if(Trigger.oldMap.get(accRecord.id).name !=Trigger.newMap.get(accRecord.id).name)
{
accRecord.Custome_Field__C=accRecord.name;
}
}
}
Please mark the best answer.
Best Regards,
-Vivek
I after update trigger records are readoly and We can't change field value in after update trigger of Sobject hence go with before trigger. Given code will definately solve your problem. let me konow if it not resolving your problem.
Regards,
-Vivek
Hi Vivek
Thanks For Replying
My Requirment is in account object create one custom field like test__c when i am edit the Sobject field Name after saving the field update the same name in custom field tyest__C ...
Use after Update only
Still you want go with After update then use following code.
trigger updateFieldValueTrigger on Account (after update) {
for (Account accRecord : Trigger.new) {
if(Trigger.oldMap.get(accRecord.id).name !=Trigger.newMap.get(accRecord.id).name)
{
MyFutureClass.updateAccountFields(accRecord);
}
}
}
global class MyFutureClass {
@future
static void updateAccountFields(String accId) {
if(String.isNotEmpty(accId))
{
Acount acRecord=[Select name,Custom_field__c From Account where Id =:accId][0];
acRecord.Custom_field__c=acRecord.name;
update acRecord;
}
}
}
Please let me know if it resolve your problem and mark the best answer
Try with below code and replace your custom field AI with Custom_field__c ,Also your field type should Text else it wil throw you error .
Let me know if it helps !!
Thanks
Manoj
After context your trigger.new and Trigger.old always read only as it is saved to database .So you can not do DML on Trigger.new /old .That is the reason you need to query to do any modification on the particular record .
Let me know if it helps .
If you're only needing to update the same record, not a set of records, then you could try using a workflow action/field update with the condition using formula ISCHANGED().
https://help.salesforce.com/apex/HTViewHelpDoc?id=customize_functions_i_z.htm&language=en_US#ISCHANGED (https://help.salesforce.com/apex/HTViewHelpDoc?id=customize_functions_i_z.htm&language=en_US#ISCHANGED)