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
rahul soni 20rahul soni 20 

This trigger is not updating the moblile filed?.

trigger mobileUpdate on APEX_TEST__c (before insert, after insert, after update, before update) {
    List<APEX_TEST__c> mobile = new List<APEX_TEST__c>();
    
    for(APEX_TEST__c m : mobile){
        m.mobile__c = '9413836278';
    }
    insert mobile;
}

I want to update mobile filed for all record. any help??
Best Answer chosen by rahul soni 20
RahulRahul
Use this :- For Before insert you dont have to add Insert seperately

trigger mobileUpdate on APEX_TEST__c (before insert, after update) {

    List<APEX_TEST__c> listtoupdate = new List<APEX_TEST__c>();
    
    If(Trigger.isInsert && Trigger.isBefore){
    
     for(APEX_TEST__c m : Trigger.new){
        m.mobile__c = '9413836278';
    }
    }
    
     If(Trigger.isUpdate && Trigger.isAfter){
    
     for(APEX_TEST__c m : Trigger.new){
        m.mobile__c = '9413836278';
        listtoupdate.add(listtoupdate);
    }
    if(listtoupdate.size() > 0){
    
    update listtoupdate;
    
    }    
}

Please mark it as best answer if this helps you

All Answers

RahulRahul
Use this :- For Before insert you dont have to add Insert seperately

trigger mobileUpdate on APEX_TEST__c (before insert, after update) {

    List<APEX_TEST__c> listtoupdate = new List<APEX_TEST__c>();
    
    If(Trigger.isInsert && Trigger.isBefore){
    
     for(APEX_TEST__c m : Trigger.new){
        m.mobile__c = '9413836278';
    }
    }
    
     If(Trigger.isUpdate && Trigger.isAfter){
    
     for(APEX_TEST__c m : Trigger.new){
        m.mobile__c = '9413836278';
        listtoupdate.add(listtoupdate);
    }
    if(listtoupdate.size() > 0){
    
    update listtoupdate;
    
    }    
}

Please mark it as best answer if this helps you
This was selected as the best answer
rahul soni 20rahul soni 20
It is working fine after a correction your code for insert. but it is not wokring for update. 
Error: 
mobileUpdate: execution of AfterUpdate caused by: System.FinalException: Record is read-only Trigger.mobileUpdate: line 24, column 1
whiich is m.mobile__c = '9413836278';
RahulRahul
Please try this :-

trigger mobileUpdate on Account (before insert, before update) {

    List<Account> listtoupdate = new List<Account>();
    
    If(Trigger.isInsert && Trigger.isBefore){
    
     for(Account m : Trigger.new){
        m.Phone = '9413836278';
    }
    }
    
     If(Trigger.isUpdate && Trigger.isbefore){
    
     for(Account m : Trigger.new){
        m.Phone = '9413836278';
    }
   
}
}
RahulRahul
Replace account with APEX_TEST__c and phone with mobile__c  in the code and try it will work i have tested it