1

I'm trying to add a value to a multi-select picklist with a batch class. The debug statement shows that its being added but it's not saving for some reason. Any ideas?

public class projectPatriotConTypeBatch implements Database.Batchable<sObject>, Database.Stateful {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT Id, Current_Placement__r.Contractor_Type__c FROM JSO2__File__c WHERE Current_Placement__r.Contractor_Type__c excludes (\'Project Patriot\') AND Current_Placement__r.Contractor_Type__c != null');
    }
    public void execute(Database.BatchableContext BC, List<JSO2__File__c> efList){
        for(JSO2__File__c ef : efList){
            ef.Current_Placement__r.Contractor_Type__c += ';Project Patriot';
            System.debug(ef.Current_Placement__r.Contractor_Type__c);
        }
        update efList;
    }
    public void finish(Database.BatchableContext BC){
    }
}

1 Answer 1

4

In a nutshell, DML on one object does not cause changes made to related data to be saved.

You need to be performing DML on a Placement__c (I'm assuming that's what the SObject name is) record, not the JSO2__File__c. Futher, it's not really clear why your query is based on JSO2__File__c in the first place (you're not changing any of the information on the records of that SObject type).

If you change your batch query to be on Placement__c instead, your life will be easier.

If for some reason that isn't possible, then you'll want to adjust the execute() method so that you have a Map<Id, Placement__c> (to avoid duplicate records in an update) and use that for your DML update instead of efList.

e.g.

Map<Id, Placement__c> placementsToUpdate = new Map<Id, Placement__c>();

for(JSO2__File__c file :efList){
    Placement__c placement = file.Current_Placement__r;
    placement.Contractor_Type__c += ';something';

    placementsToUpdate.put(placement.Id, placement);
}

update placementsToUpdate.values();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .