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
Vignesh RamshettyVignesh Ramshetty 

geeting error Variable does not exist: Phone at line 16 column 59

Public class accountrecord {

Public static void making(List<Account> varacc){

           Map<Id,Account> varmap = new  Map<Id,Account>();

          List <Contact> listcontact = new List <Contact>(); 
        
           
 
          for(Account vara :varacc ){
          
          
          

        if (vara.Phone !=  trigger.oldMap.get ( vara.id ).Phone){
              varmap.put(vara.id,vara); 
                        
        }


   }

if(varmap.size() > 0){



         listcontact = [SELECT AccountId, phone FROM Contact Where AccountId in: varmap.keyset() ];


if(listcontact.size() > 0){


        for(Contact con : listcontact ){

               con.Phone = varmap.get(con.AccountId).phone;
                   
           
}
 update listcontact ;  

}

   

 }

}
}
Best Answer chosen by Vignesh Ramshetty
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vignesh,

I have updated the code can you try this.

Trigger:
trigger Accountobject on Account (after update) {

if(trigger.isafter  && trigger.isupdate ){

accountrecord.making(trigger.new,Trigger.oldmap);

}
}

Apex Class:
 
Public class accountrecord {

Public static void making(List<Account> varacc,Map<id,Account> oldmap){

           Map<Id,Account> varmap = new  Map<Id,Account>();

          List <Contact> listcontact = new List <Contact>(); 
        
           
 
          for(Account vara :varacc ){
          
          
          

        if (vara.Phone !=  oldMap.get ( vara.id ).Phone){
              varmap.put(vara.id,vara); 
                        
        }


   }

if(varmap.size() > 0){



         listcontact = [SELECT AccountId, phone FROM Contact Where AccountId in: varmap.keyset() ];


if(listcontact.size() > 0){


        for(Contact con : listcontact ){

               con.Phone = varmap.get(con.AccountId).phone;
                   
           
}
 update listcontact ;  

}

   

 }

}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,​​​​​​​

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vignesh,

Do you have phone field in Account. If so can you share the API name of that field.

Thanks,
 
Vignesh RamshettyVignesh Ramshetty
Yes I have phone field in the Account object  and the API name of that field is Phone
Image of API name
Vignesh RamshettyVignesh Ramshetty
Yes I have phone field in the Account object and the API name of that field is Phone [image: Image of API name]
Sai PraveenSai Praveen (Salesforce Developers) 
HI Vignesh,

Your code is in Handler and the handler does not know trigger.oldMap. Can you share the trigger so can help you on both .

Thanks,
 
Vignesh RamshettyVignesh Ramshetty
trigger Accountobject on Account (after update) {

if(trigger.isafter == true && trigger.isupdate == true ){

accountrecord.making(trigger.new);

}


Apex Class :

Public class accountrecord {

Public static void making(List<Account> varacc){

           Map<Id,Account> varmap = new  Map<Id,Account>();

          List <Contact> listcontact = new List <Contact>(); 
        
           
 
          for(Account vara :varacc ){
          
          
          

        if (vara.Phone !=  trigger.oldMap.get ( vara.id ).Phone){
              varmap.put(vara.id,vara); 
                        
        }


   }

if(varmap.size() > 0){



         listcontact = [SELECT AccountId, phone FROM Contact Where AccountId in: varmap.keyset() ];


if(listcontact.size() > 0){


        for(Contact con : listcontact ){

               con.Phone = varmap.get(con.AccountId).phone;
                   
           
}
 update listcontact ;  

}

   

 }

}
}
Vignesh RamshettyVignesh Ramshetty
trigger Accountobject on Account (after update) { if(trigger.isafter == true && trigger.isupdate == true ){ accountrecord.making(trigger.new); } Apex Class : Public class accountrecord { Public static void making(List varacc){ Map varmap = new Map(); List listcontact = new List (); for(Account vara :varacc ){ if (vara.Phone != trigger.oldMap.get ( vara.id ).Phone){ varmap.put(vara.id,vara); } } if(varmap.size() > 0){ listcontact = [SELECT AccountId, phone FROM Contact Where AccountId in: varmap.keyset() ]; if(listcontact.size() > 0){ for(Contact con : listcontact ){ con.Phone = varmap.get(con.AccountId).phone; } update listcontact ; } } } }
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vignesh,

I have updated the code can you try this.

Trigger:
trigger Accountobject on Account (after update) {

if(trigger.isafter  && trigger.isupdate ){

accountrecord.making(trigger.new,Trigger.oldmap);

}
}

Apex Class:
 
Public class accountrecord {

Public static void making(List<Account> varacc,Map<id,Account> oldmap){

           Map<Id,Account> varmap = new  Map<Id,Account>();

          List <Contact> listcontact = new List <Contact>(); 
        
           
 
          for(Account vara :varacc ){
          
          
          

        if (vara.Phone !=  oldMap.get ( vara.id ).Phone){
              varmap.put(vara.id,vara); 
                        
        }


   }

if(varmap.size() > 0){



         listcontact = [SELECT AccountId, phone FROM Contact Where AccountId in: varmap.keyset() ];


if(listcontact.size() > 0){


        for(Contact con : listcontact ){

               con.Phone = varmap.get(con.AccountId).phone;
                   
           
}
 update listcontact ;  

}

   

 }

}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,​​​​​​​
This was selected as the best answer