+ Start a Discussion
Ashish Sharma 207Ashish Sharma 207 

trying to sync two fields

Hello,

we have a scheduled apex to ensure that the project role on the contact = the role added to the opportunity.
But that the gap we have today, is that the values on the contact role under opportunity and the values in the project role field does not map. If they are same, this sync would be.

Anyone know how I would sync them. An apex trigger, workflow or formula?

Thank you.
Best Answer chosen by Ashish Sharma 207
rajat Maheshwari 6rajat Maheshwari 6

Hi Ashish,

Please use  : - global class RoleSchedule implements Schedulable instead global class RoleSchedule implements Schedulabe

Here Schedulabe is spelt wrong.

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

 

All Answers

rajat Maheshwari 6rajat Maheshwari 6

Hi Ashish,

please have a look ove rthe below code snippet, Hope it will helps :)

global class RoleSchedule implements Schedulabe
  {
     
      global void execute(ScheculableContext sc)
         {
            List<Contact> con_List = new List<contact>();
        
            Set<Id> set_Id = new Set<id>();
             Map<Id,OpportunityContactRole> mp_Contact = new Map<Id,OpportunityContactRole>();

             for(OpportunityContactRole OCR : [Select Id,ContactId,Contact.ProjectRole, OpportunityId,Role from OpportunityContactRole where Role!=null])
                  {
                     mp_Contact.put(OCR.ContactId,OCR);
                    set_Id.add(OCR.ContactId); 
                   }

             for(Contact con : [Select Id,ProjectRole from Contact where Id IN : set_Id])
                {
                    if(mp_Contact!=null && mp_Contact.containsKey(con.id))
                       {     
                           con.ProjectRole = mp_Contact.get(con.id).Role;
                           con_List.add(con); 
                        }
                  }

if(con_List!=null && con_List.size()>0)
      update con_List;
}

}
 

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

 

Ashish Sharma 207Ashish Sharma 207
Hi Rajat,

I am gettting this error when creating the apex class

Error: Compile Error: line 3:0 no viable alternative at character ' ' at line 3 column 0.

One more question, I am not very familar with deveploment so would i need to create anything else besides the apex class to implement my rule?

Thank you very much for you help.
rajat Maheshwari 6rajat Maheshwari 6

Hi Ashish,

Please use  : - global class RoleSchedule implements Schedulable instead global class RoleSchedule implements Schedulabe

Here Schedulabe is spelt wrong.

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

 

This was selected as the best answer
Ashish Sharma 207Ashish Sharma 207
Hi Rajat,

Can you help me understand how this code actually works and how I can test it?
rajat Maheshwari 6rajat Maheshwari 6

Hi Ashish,

I will guide you the way to testing the code and teach you each and every aspects. Please you can contact me at email Id.

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com