• There are 2 custom objects involved.
  • Project__c is the parent, Project_Contact__c is the child.
  • Both objects contain a field called Survey_Sent__c.
  • Whenever 'Survey Sent' is updated on the child, I'd like my trigger to fire and update 'Survey Sent' on the parent.

============ Code so Far =========

trigger UpdateProjectSurveySent on Project_Contact__c (before update) {
    if(trigger.isUpdate){ // if trigger fired due to an update

        List<Id> parentIds = new List<id>(); // create a list variable to that holds Ids
        List<Id> childIds = new List<Id>(); // list to hold child Ids

        for(Project_Contact__c Pc : trigger.new){ // cycle through child records, grab Ids, store child Ids in trigger.new
            parentIds.add(Pc.Project__c.Id); // then add parent Ids to our parent Id list. Lists are ordered. This is kind of a map stucture. 

        List<Project__c> pj = [SELECT Id, Survey_Sent__c FROM Project__c WHERE Id in: parentIds]; // fill list3 with projects that have an Id stored in pjIdList

        for(Project__c p : pj){ // cycle list3 projects, and make Survey Sent fields match.
            p.survey_sent__c = pj.survey_sent__c;
  • Is it not going to be conflicting when you will have children for the same parent with mixed Survey_Sent__c values? – o-lexi Dec 8 '16 at 19:57
  • Not sure. I can easily change the name of one, but my code still wouldn't be correct. – Rock Tober Dec 8 '16 at 20:01
  • We are on different pages - is it always 1 child for each parent? Otherwise you could have Child1.Survey_Sent__c = true, Child1.Survey_Sent__c = false, Parent1 set as Project__c for both, so your Parent1.Survey_Sent__c can be either one, depending how you have your code structured. Code wise there are multiple issues, but lets clarify the case from above. – o-lexi Dec 8 '16 at 21:05

You should try learn how to use Map, it's very useful I used it all the time. So first you need to check if the value of the project contact survey change (don't need to update the project if the survey didn't change). Add the project to a map, then retrieve the project via SOQL, then loop to project contact again and see if we need to update related project found. It should look something like :

trigger UpdateProjectSurveySent on Project_Contact__c (before update) {
    if(trigger.isUpdate){ // if trigger fired due to an update

        Map<Id, Project__c> projects = new Map<Id, Project__c>();

        for(Project_Contact__c Pc : trigger.new){
            if (pc.Survey_Sent__c != Trigger.oldMap.get(pc.Id).Survey_Sent__c){  //do we need to update Project ? 
              projects.put(Pc.Project__c, null);
        //get projects
        if (projects.isEmpty()){
          projects = new Map<Id, Project__c>([SELECT Id, Survey_Sent__c FROM Project__c WHERE Id in: projects.keySet()]); 

         for(Project_Contact__c Pc : trigger.new){ \
            Project__c p = projects.get(pc.Project__c);
            if (p != null){
              p.survey_sent__c = Pc.survey_sent__c;

        update projects.values();
  • Thanks bro. Can you explain how this line reads in plain terms? if(pc.Survey_Sent__c != Trigger.oldMap.get(pc.Id)). Seems like I'm comparing a boolean to an id here... – Rock Tober Dec 8 '16 at 22:37
  • my bad I forgot something – Cloud Ninja Dec 8 '16 at 22:40
  • updated my answer. But basically the Trigger class contains oldMap which is a map of the record you are updating in their old state. So you can compare the new value with the old value to see if it has been modified – Cloud Ninja Dec 8 '16 at 22:42
  • Thanks for the help. I'm getting an error, but I'm not sure it has to do with your code. I received the same error on my trigger after I rewrote it. The error reads "duplicate value found: <unknown> duplicates value on record with id: <unknown>". I think this is a MavensMate problem. I'll keep researching it. I believe your code is correct. Thank you. – Rock Tober Dec 8 '16 at 22:46
  • UPDATE: Cloud Ninja, your code works. The aforementioned error was basically stating that I had to rename my trigger. For some reason (even though my trigger didn't appear in the list) Salesforce was treating the trigger like it was there; and therefore I had to name my trigger something different. – Rock Tober Dec 9 '16 at 15:05

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.