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
thekid12345thekid12345 

Batch Class on Account Contact Roles

global class AcrUpdateContact implements Database.Batchable<SObject>, Schedulable{
     public Database.QueryLocator start(Database.BatchableContext BC)
    {
        //String query = 'SELECT ContactId FROM AccountContactRole WHERE Role := Primary Contact';
        String query = 'Select Id, (SELECT ContactId,Role FROM AccountContactRoles WHERE Role =: Primary Contact) from Account WHERE Id IN(Select AccountID from AccountContactRole)';
      
        return Database.getQueryLocator(query);
    }
    public void execute(Database.BatchableContext BC, List<Contact> cont)
    {
        for(Contact c : cont)
        {
            c.Status__c = 'Open';
            system.debug(c);
        }
        update cont;
    }
    global void execute(SchedulableContext sc){
        //Database.executeBatch(this);
    }
    global void finish(Database.BatchableContext BC) {
        
    }
}
The goal is when an account contact role is set to Primary Contact, update the associated Contact's status field to open.  I know that account contact roles is very limited in that we can not trigger anything off of that object.  Was wondering if the code above is correct.
 
Raj VakatiRaj Vakati
global class AcrUpdateContact implements Database.Batchable<SObject>, Schedulable{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        return Database.getQueryLocator([Select Id,AccountId,ContactId,IsPrimary from AccountContactRole where role ='Primary Contact']);
    }
    global void execute(Database.BatchableContext BC, List<AccountContactRole> cont)
    {
        List<Id> idsOfcon = new List<Id>();
        for(AccountContactRole c : cont)
        {
            idsOfcon.add(c.ContactId);
        }
        
        List<Contact> con =[Select Id , Status__c from Contact where id in :idsOfcon]; 
        for(Contact cn :con){
            cn.Status__c='Open';
        }
        
        update cont;
    }
   
    global void finish(Database.BatchableContext BC) {
        
    }
}