0

I want to do something similar with what this forum is asking, but with more of a list of objects and not a shared field.

I have an object called Call_Report__c and upon save, I want to get its field value called Apps_to_Relate__c which has a string of recordId's of the object Application__c separated by a comma.

I want to update a field of Application__c objects in that string field Apps_to_Relate__c to be the recordId of Apps_to_Relate__c.

How can I go about this? This is the code I have so far:

trigger CallReportTrigger on Call_Report__c (after insert, after update) {
    Map<String, Call_Report__c> objMap = new Map<String, Call_Report__c>();

    for (Call_Report__c callReport : Trigger.new) {
        if (callReport.Custom_Related__c != NULL) {
            objMap.put(callReport.Custom_Related__c, callReport);
        }
    }

    List<Call_Report__c> customObj = [SELECT Custom_Related__c FROM Call_Report__c];
    List<String> finalcustomObj = new List<String>();
    for (Integer i=0; i<customObj.size(); i++) {
         finalcustomObj.add(customObj.get(i).split(',')); // error: split is not a method
    // Custom_Related__c is a long text field, with a String of recordIds separated by commas
    
}

1 Answer 1

1

If you want to update the Application Object, using the comma separated Id's stored in another object, you should split the comma separate field and set it to a List, which can be iterated to set individual record values.

So the code should be something like this,

List<Application__c> applicationListToUpdate = new List<Application__c>();
Set<String> applicationIdSet = new Set<String>();
for (Call_Report__c callReport : Trigger.new) {
    if (String.isNotBlank(callReport.Custom_Related__c)) {
       for(String applicationId: callReport.Custom_Related__c.split(',')) {
            if(!applicationIdSet.contains(applicationId)) {
                Application__c appObj = new Application__c();
                appObj.Field_To_Update__c = callReport.newValue //Update Application Field Here
                appObj.Id = applicationId //Set Application Id Field Here to be used in Update
                applicationListToUpdate.add(appObj);
                applicationIdSet.add(applicationId);
            }
       }
    }
}
update applicationListToUpdate;

Note: I haven't tested this code. But this should help you to get through the logic

1
  • This works perfectly, thank you
    – Lia
    Jul 7, 2021 at 13:27

You must log in to answer this question.

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