2

I have a question. I have a field titled Familiar Name (text field) in two different objects. the objects are: Contact (standard Master object to Profiling Information a custom object). What I would like to do is enter a familiar name in the profiling information object and this information would feed to the Familiar Name field in the Contact object. is it possible to do this through a workflow rule or would i need to create an apex trigger? I'm trying to reduce the amount of apex triggers right now as I am still learning.

2 Answers 2

2

The simplest option is to use a formula field on the child object. This means you'll only be able to edit the field on the contact, but you'll always be able to see it on your custom object.

You can use workflow to push changes from a child object, e.g. Profiling Information, up to the contact object since it's a master detail relationship, but you'll need a trigger to push that information down to other child records related to the same object.

Personally, unless there is a really good reason, I'd stick with the formula field approach.

0

If you really need to propagate changes from the child object to the master one, you will have to take into account what Ralph points out: you have to push that information to other child records as well. In this case with the trigger approach you can copy the value to the master record and to other child records, but it is not trivial because you have to take care of not overwriting the same records twice as well as not to start an infinite recursion through some context variable or any other technique. I have not tested it but I think something like this would work:

trigger PITrigger on ProfilingInformation__c (after update, after insert)
{
  if(<awesomeGlobalContextVariable>)
    return;

  <setAwesomeGlobalContextVariable>;

  try
  {
    Map<Id, ProfilingInformation__c> recordMap = Trigger.newMap;

    List<Contact> parentRecords = [SELECT id, (SELECT Id FROM ProfilingInformations__r WHERE id NOT IN :recordMap.keySet()) FROM Contact WHERE Id IN (SELECT Contact__c FROM ProfilingInformation__c WHERE Id IN :recordMap.keySet())];
    Map<Id, Contact> parentRecordMap = new Map<Id, Contact>(parentRecords);

    List<ProfilingInformation__c> siblings = new List<ProfilingInformation__c>();
    for(Id piId : recordMap.keySet())
    {
      ProfilingInformation__c pi = recordMap.get(piId);
      Contact parent = parentRecordMap.get(pi.Contact__c);
      parent.FamiliarName__c = pi.FamiliarName__c;

      for(ProfilingInformation__c sibling : parent.ProfilingInformations__r)
        sibling.FamiliarName__c = pi.FamiliarName__c;

      siblings.addAll(parent.ProfilingInformations__r);
    }

    update parentRecords;
    update siblings;
  }
  finally
  {
    <clearAwesomeGlobalContextVariable>;
  }
}
1
  • thank you for the answers. I have decided to coach the team on just updating the child object and referring to this area for the information. it just feels like extra work not needed. again, thank you all for your answers.
    – WickWack
    Jul 10, 2014 at 17:31

You must log in to answer this question.

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