+ Start a Discussion
Luke Higgins 22Luke Higgins 22 

Update a field from unrelated object

I am trying to run a batch class once a week to update the Filled_By__c field on the Placement__c object from the User__c field on the PlacementTeamMember__c object. I'm usure how to do this since they are unrelate objects.

This is the code I have so far-
 

global class ptmToOldCommissions implements Database.Batchable<sObject>, Database.Stateful {
	
    List<jstcl__PlacementTeamMember__c> plcTeamMemIds = [SELECT Id, jstcl__User__c FROM jstcl__PlacementTeamMember__c WHERE jstcl__CommissionPlan__r.Name LIKE '%Recruiter%' LIMIT 1];
    
	global Database.QueryLocator start(Database.BatchableContext BC){
        String plcIds = 'SELECT Id, ts2__Filled_By__c FROM ts2__Placement__c WHERE ts2__Status__c = "Active"';
        return Database.getQueryLocator(plcIds);
    }

    global void execute(Database.BatchableContext BC, List<ts2__Placement__c> scope){

        for ( ts2__Placement__c a : scope){
            for ( jstcl__PlacementTeamMember__c b : plcTeamMemIds){
                
           a.ts2__Filled_By__c = b.jstcl__User__c;
        }
            update scope;
        }
    }  
    global void finish(Database.BatchableContext BC){
    }
}
MagulanDuraipandianMagulanDuraipandian
Try the below code

global class ptmToOldCommissions implements Database.Batchable<sObject>, Database.Stateful {
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        String plcIds = 'SELECT Id, jstcl__User__c, ts2__Placement__c FROM jstcl__PlacementTeamMember__c WHERE ts2__Placement__r.ts2__Status__c = "Active"';
        return Database.getQueryLocator(plcIds);
    }

    global void execute(Database.BatchableContext BC, List<jstcl__PlacementTeamMember__c> scope){

        List<ts2__Placement__c> listPlacments = new List<ts2__Placement__c>();
        for ( jstcl__PlacementTeamMember__c a : scope) {
            listPlacments.add( new ts2__Placement__c( ts2__Filled_By__c = a.jstcl__User__c, Id = a.ts2__Placement__c ) ) ;
        }
        update listPlacments;
    }  
    global void finish(Database.BatchableContext BC){
    }
    
}

--
Magulan Duraipandian
www.infallibletechie.com
Luke Higgins 22Luke Higgins 22
Hi Magulan,

Unfortunately this has not ended up working. There were no errors and the logs says it ran successful so I am unsure what has gone wrong.