0

I got 2 objects Contacts(Parent) and Survey custom Object(Child). All I'm trying to do is update a field of contact object from child.

Survey_Response__c AvailSurveys =  [select name,Contact__c,Contact__r.id,Contact__r.Name,Contact__r.All_Survey_Responses__c  ,Response__c from Survey_Response__c];

List<Survey_Response__c> SurvyList = new List<Survey_Response__c>();

for(Survey_Response__c survy : AvailSurveys ) {
  if(survy.Contact__c != null) {
        if(survy.Contact__r.All_Survey_Responses__c != null) {
            survy.Contact__r.All_Survey_Responses__c = survy.Contact__r.All_Survey_Responses__c +','+ survy.Response__c ;
        }
        else survy.Contact__r.All_Survey_Responses__c = survy.Response__c ;
   }
   SurvyList.add(survy);           
}         

update SurvyList;

Parent field is not updating in this scenario.Any help?

  • from what I see you are not querying the Survey_Response__c.id in your select statement. – Marc Zaharescu Apr 1 '16 at 8:16
  • is it master-detail or lookup relationship? – Micz Domz Apr 1 '16 at 8:38
1

In your code you're not actually updating the Contact records but instead updating the Survey_Response__c records. To update the contacts you'll need to store a list of the Contact records and update that rather than the current SurvyList as you are currently doing it. I have quickly put together a version which stores and updates the contacts below with comments to explain any changes made:

Survey_Response__c AvailSurveys =  [select name,Contact__c,Contact__r.id,Contact__r.Name,Contact__r.All_Survey_Responses__c  ,Response__c from Survey_Response__c];

Map<Id,Contact> contactsToUpdate = new Map<Id,Contact>();
for(Survey_Response__c survy : AvailSurveys )
{
    if(survy.Contact__c != null)
    {
    /*
    *   Because we may have multiple surveys referencing the same contact put the contacts into a map keyed by id
    *   this will help when updating to avoid duplicate contacts in the update list and will help stop unwanted overwriting of All_Survey_Responses__c data.
    */
        if(!contactsToUpdate.containsKey(survy.Contact__c))
        {
            contactsToUpdate.put(survy.Contact__c, survy.Contact__r);
        }
        Contact currentContact = contactsToUpdate.get(survy.Contact__c);

        /*
        *   Now we have the current contact we will be working on that record from here out in the loop.
        */
        if(currentContact.All_Survey_Responses__c != null)
        {
            currentContact.All_Survey_Responses__c = currentContact.All_Survey_Responses__c +','+ survy.Response__c ;
        }
        else currentContact.All_Survey_Responses__c = survy.Response__c ;
        system.debug('survy -->'+survy );
    }           
}     
/*
* if we have contacts added to the map update these to store the new   All_Survey_Responses__c values.
*/
if(contactsToUpdate.size() > 0)
{    
    /*
    *   as its a map we're calling update against the list of values in the map.
    */
    update contactsToUpdate.values();
}
| improve this answer | |
0

You can't update Contact__r this way. All changes made to Contact__r will we be erased on survy update. What you should do is to collect the Contact__r objects and update them separately.

| improve this answer | |
0

If the relationship happens to be master-detail, select the field and check Child records can be reparented to other parent records after they are created to allow updating a specific record.

| improve this answer | |

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.