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
S SaiS Sai 

trigger in Sobject

after update trigger for account object.. condition is if name is changed you need to update the same name in new custom field in same object ....
 
Vivek DeshmaneVivek Deshmane
Hi SS,
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
S SaiS Sai
NOt using Befote trigger use after trigger
 
Vivek DeshmaneVivek Deshmane
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.

Regards,
-Vivek
S SaiS Sai

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
 
Vivek DeshmaneVivek Deshmane
Hi SS,
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
ManojjenaManojjena
HI SS,
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 .
 
trigger updateFieldValueTrigger on Account (after update) {
   List<Account> accListToUpdate=new List<Account>();
   Set<Id> accIdSet=new Set<Id>();
   for (Account acc : Trigger.new) {
       if(Trigger.oldMap.get(acc.id).name !=acc.name){
           accIdSet.add(acc.Id); 
       }
    }
 For(Account acc : [SELECT id,Name,Custom_field__c FROM Account WHERE Id IN :accIdSet ]){
   acc.Custom_field__c=acc.Name;
   accListToUpdate.add(acc);
 }
 try{
   if(!accListToUpdate.isEmpty()){
     update accListToUpdate;
   }
 }Catch(DmlException de ){
    System.debug(de);
 }
}

Let me know if it helps !!

Thanks 
Manoj
S SaiS Sai
NO Need to write any query
ManojjenaManojjena
Hi SS,

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 .
brielea1984brielea1984
I'd recommend checking out the Process Builder as it will guide you through this easily, and offers the option to run whether or not a field "Is Changed" or not. I think this might handle the logic and save you the need to write test code.  http://developerforce.github.io/lightning-process-builder-tutorial/create-apex-controller.html (start on step 3)

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)