function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lisa.sturycz1.3890322391892622E12lisa.sturycz1.3890322391892622E12 

Trigger populate parent record field based on loop through related list values

I am having trouble with how to go about writing a trigger to complete the following:

I have two related objects Contacts and Contact Specialties (related list on Contacts). Any time a Contact Specialty record is added, edited, or deleted from a Contact, I need a trigger to fire that loops through all Contact Specialty records on the Contact and looks to see if the Specialty field on any of the related Contact Specialty records is one of three values: "Family Medicine", "General Medicine", or "Family Practice". If so, I need to check the PCP__c checkbox on the Contact record, if not I need to uncheck the PCP__c checkbox for that contact.

Could anyone be of assistance with this?
bob_buzzardbob_buzzard
It should be something close to the following (bulkfied, but caveat emptor - I haven't compiled this so there might be the odd typo.  It also won't scale if there are thousands of specialities associated with each contact):

trigger setupPCP on Contact_Speciality__c (after insert, after update, after delete) {
  // get the contacts
  Set<Id> contactids=new Set<Id>();
  Map<Id, Contact_Speciality__c> specMap;

  // figure out which map is populated
  if (Trigger.isInsert || Trigger.isUpdate)
  {
     specRecs=Trigger.newMap();
  }  
  else
  {
    specRecs=Trigger.oldMap();
  }

  // get the contact ids from the speciaity records
  for (Contact_Speciality__c spec : specMap.values)
  {
    contactIds.add(spec.Contact__c);
  }

  // now retrieve the contacts and all of their associated specialities, so that we can figure out the correct value for the contact
  List<Contact> toUpdate=new List<Contact>();
  for (Contact cont : [select id, PCP__c, (select id, Speciality__c from Contact_Specialities__r) from Contact where id in :contactIds])
  {
    // iterate the specialities looking for speciality values that require the PCP flag to be set on the contact
    boolean checkPCP=false;
    for (Contact_Speciality__c cSpec : cont.Contact_Specialities__r)
    {
       if (cSpec.Speciality__c=='Family Medicine' || cSpec.Speciality__c=='General Medicine' | cspec.Speciality__c=='Family_Practice__c')
       {
           checkPCP=true;
           break;       // no need to continue with the inner loop as we know we need to check the PCP box on the contact
       }
    }

    // is the result different to the contact record?
    if (checkPCP!=cont.PCP__c)
    {
       cont.PCP__c=checkPCP;
       toUpdate.add(cont);
    }
    
  }

  if (!toUpdate.isEmpty())
  {
    update toUpdate;
  }
}
lisa.sturycz1.3890322391892622E12lisa.sturycz1.3890322391892622E12
Thank you so much...your help is very much appreciated! I will give this a try.
sp08830sp08830
I have a similar requirement. however I cannot write a trigger on child object. Is there a way to achieve the same with a trigger on parent?