1

I have a child object to contact called"ObjectA".I have a discipline field(text) in the child object and I have the same field in Contact too.Now If I insert 4 child records with different contact ids, the records discipline values should be updated correspondingly with a space ' ' separator.

For Eg,

  • Child Rec 1: Contact = C1, Disc Name = Biology
  • Child Rec 2: Contact = C2, Disc Name = English
  • Child Rec 3: Contact = C1, Disc Name = Physics
  • Child Rec 4: Contact = C2, Disc Name = History
  • Child Rec 5: Contact = C2, Disc Name = English

Then C1 should have Discipline value of ‘Biology Physics’, and C2 should have Discipline value of ‘English History’. (Duplicates should be ignored). Here is my code. But I'm not able to assign the discipline value from Child Records to Contact Discipline field. Instead, the value is displayed as "common.apex.runtime.impl.ScalarList@a1324f1f" in the contact records. I'm calling this class in after insert and after update method from trigger.

public class TriggerHandler {

public static void disciplineDescription(List<ObjectA__c> lstOfRecs, Map<Id,ObjectA__c> oldRecs) { 
Set<Id> contactIds = new Set<Id>();
Set<String> disciplineSet = new Set<String>();
Map<Id,List<String>> contactDisciplineMap = new Map<Id,List<String>>();
List<Contact> updatedContacts = new List<Contact>();
for(ObjectA__c objA: lstOfRecs) {
  contactIds.add(objA.Contact__c);
}

List<ObjectA__c> contactList = new List<WileyPlus__c>([SELECT Id,Discipline_Name__c,Contact__c,Contact__r.Discipline_Description__c FROM ObjectA__c WHERE Contact__c IN : contactIds]);
for (ObjectA__c objA: contactList) {    
  disciplineSet.add(objA.Discipline_Name__c);
  contactDisciplineMap.put(objA.Contact__c,new List<String>(disciplineSet));
}

List<Contact> updContact = new List<Contact>([SELECT Id,Discipline_Description__c FROM Contact WHERE Id IN : contactDisciplineMap.keyset()]);
  if(updContact.size() > 0 ) {
    for(Contact cont : updContact) {
      cont.Discipline_Description__c = String.join(contactDisciplineMap.values(), ' ');
      updatedContacts.add(cont); 
    }
  } 
update updatedContacts;
  }
 }
4
  cont.Discipline_Description__c = String.join(contactDisciplineMap.values(), ' ');

You're trying to join the List<List<String>>, which calls the default toString method, which isn't what you want. Instead, you need to call get:

  cont.Discipline_Description__c = String.join(contactDisciplineMap.get(cont.Id), ' ');

The typical way to do this is to build a map, query the records, then update the records, which I call the Aggregate-Query-Update pattern.

In your case, the code would look something like this:

Map<Id, Contact> contacts = new Map<Id, Contact>();
Map<Id, Set<String>> disciplines = new Map<Id, Set<String>>();
// Step 1: Aggregate data, initialize maps and sets.
for(ObjectA__c record: lstOfRecs) {
  contacts.put(record.Contact__c, new Contact(Discipline_Description__c=null, Id=record.Contact__c));
  disciplines.put(record.Contact__c, new Set<String>());
}
contacts.remove(null);
// Step 2: Query for existing records to use
for(ObjectA__c record: [SELECT Contact__c, Discipline_Name__c
                        FROM ObjectA__c
                        WHERE Contact__c = :contacts.keySet()]) {
  disciplines.get(record.Contact__c).add(record.Discipline_Name__c);
}
// Step 3: Perform updates on records
for(Contact record: contacts.values()) {
  record.Discpline_Description__c = String.join(
    new List<String>(discplines.get(record.Id)), ' ');
}
update contacts.values();

Error handling not included, but this should illustrate how the Set/Map combination should have been used.

  • I'm getting duplicate values with my code. Can you pls tell whether my approach for my requirement ins correct or not – ram135868 Mar 5 '18 at 12:02
  • @ram135868 Yes, you're not using your map correctly. Your code has a number of issues. I only addressed the immediate issue in your question. You're basically adding all of the values for any contact into the map each time, when you really just want to add it for the appropriate contact. You may have other issues, too, but I don't think duplicates should be one of them. – sfdcfox Mar 5 '18 at 12:05
  • i tried many times ,but couldn't output the results as expected.Can you pls modify my code and post the correct answer in a way it has to be. @sfdcfox – ram135868 Mar 5 '18 at 12:12
  • @ram135868 Edited. – sfdcfox Mar 5 '18 at 12:23
  • Thank you so much. And also a special thanks for the illustration and guidance which will be very helpful for me in future – ram135868 Mar 5 '18 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.