You need to sign in to do that
Don't have an account?
Donna Whitig 34
Update Contact fields with Account field data
I need to update contacts that are chosen on lookup fields on an account to have the same values on custom fields as associated account.
Background:
*********** Here's my code so far ******************
public with sharing class Assign_Access {
public static void SelectContactsAndAccess() {
//Find the Account that have Primary Contacts Associated with them
//Create a list to hold all primary contacts and access information
List<Account> AllContacts = new List<Account>();
AllContacts.clear();
//Fetch first primary contact and add to PrimaryContact1 list where Primary_Contact1__c holds the Contact ID
List<Account> PrimaryContact1 = [SELECT Id, Name, Primary_Contact1__c,
Main_Page_Access__c, Sub_Page_Access__c
FROM Account
WHERE Primary_Contact__c != NULL ];
System.debug('PrimaryContact1 ' + PrimaryContact1);
//Add Primary Contact 1 to AllContacts list
AllContacts.addall(PrimaryContact1);
//Fetch second primary contact and add to PrimaryContact2 list where Primary_Contact2__c holds the Contact ID
List<Account> PrimaryContact2 = [SELECT Id, Name, Primary_Contact2__c,
Main_Page_Access__c, Sub_Page_Access__c
FROM Account
WHERE Primary_Contact2__c != NULL ];
System.debug('PrimaryContact2 ' + PrimaryContact2);
//Add Primary Contact 2 to AllContacts list
AllContacts.addall(PrimaryContact2);
system.debug('AllContacts '+ AllContacts);
}
}
Background:
- I have two fields on the Account object that lookup directly to the contact object to house Primary Contact 1 and Primary Contact 2.
- The Account object has two custom fields Main_Page_Access__c and Sub_Page Access__c. The Contact object has these same custom fields.
- I want to run a nightly routine that will update the Main_Page_Access__c, Sub_Page Access__c fields on the Contact object with the values from the Account object. I need help with the Apex class to do the updates.
*********** Here's my code so far ******************
public with sharing class Assign_Access {
public static void SelectContactsAndAccess() {
//Find the Account that have Primary Contacts Associated with them
//Create a list to hold all primary contacts and access information
List<Account> AllContacts = new List<Account>();
AllContacts.clear();
//Fetch first primary contact and add to PrimaryContact1 list where Primary_Contact1__c holds the Contact ID
List<Account> PrimaryContact1 = [SELECT Id, Name, Primary_Contact1__c,
Main_Page_Access__c, Sub_Page_Access__c
FROM Account
WHERE Primary_Contact__c != NULL ];
System.debug('PrimaryContact1 ' + PrimaryContact1);
//Add Primary Contact 1 to AllContacts list
AllContacts.addall(PrimaryContact1);
//Fetch second primary contact and add to PrimaryContact2 list where Primary_Contact2__c holds the Contact ID
List<Account> PrimaryContact2 = [SELECT Id, Name, Primary_Contact2__c,
Main_Page_Access__c, Sub_Page_Access__c
FROM Account
WHERE Primary_Contact2__c != NULL ];
System.debug('PrimaryContact2 ' + PrimaryContact2);
//Add Primary Contact 2 to AllContacts list
AllContacts.addall(PrimaryContact2);
system.debug('AllContacts '+ AllContacts);
}
}
Please give a try now. Sorry I made contactToUpdate List of Account instead of Contact
All Answers
Please give a try now. Sorry I made contactToUpdate List of Account instead of Contact
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
In the Developer Console => Open Execute Anonymous Window, execute the following commands to start and stop the job You should be able to see the jobs navigating as shown below