[SalesForce] Update related object in trigger

I have custom object People__c that has lookup field Married__c to the same object (People__c) and I want to create a trigger for auto update after insert and update. For example, if we insert new People__c (husband) or modify it and add in lookup field another People__c (wife) then wife must have in her lookup field automatically added husband and conversely.

I've written the next code but it doesn't work. What could I try instead?

trigger InsertUpdateMarried on People__c (after insert, after update) {

    Set<Id> peopleMarriage = new Set<Id>();
    for(People__c people : trigger.New) {
        peopleMarriage.add(people.Married__c);
    }

    Map<Id,People__c> matchingPeopleMap = new Map <Id,People__c>();

    for (People__c people : [Select Id, Married__c From People__c Where Id IN :peopleMarriage]) {
        matchingPeopleMap.put(people.Id, people);
    }

    List<People__c> peoplesToUpdate = new List<People__c>();

    for(People__c people : trigger.New) {

        if (matchingPeopleMap.containsKey(people.Id))

        {

            people.Married__c = matchingPeopleMap.get(people.Id).Id;

            peoplesToUpdate.add(people);

        }

    }

    update peoplesToUpdate;

}

Best Answer

This can all be reduced to the following. No queries needed as you did not ask to check if the field on the related person was already populated or not

Note The block of updating records that are in the trigger. You will need to figure out how to handle People in the trigger that are related to people in the trigger

trigger InsertUpdateMarried on People__c (after insert, after update) {

    Map<Id,Id> peopleMarriage = new Map<Id,Id>();

    for(People__c people : trigger.New) {
        //Populate related Id to This Person Id
        if(people.Married__c == null || trigger.newMap.containsKey(people.Married__c)) continue; //Prevent from updating itself will need to fingure out how you want to handle it
        peopleMarriage.(people.Married__c, people.Id); //Related person Id mapped to THIS person
    }

    Map<Id,People__c> peoplesToUpdate = new Map<Id,People__c>();

    for(Id peoplePartner : peopleMarriage.keyset()){
        //update the Related Person lookup to the value of This person
        peoplesToUpdate.put(
            peoplePartner,
            New People__c(
                Id = peoplePartner,
                Married__c = peopleMarriage.get(peoplePartner)
            )
        );
    }


    update peoplesToUpdate.values();

}

You will need to handle recursion since you are updating records that are part of the same object. Best to move all logic to a class but the above will give you an idea of what you need to do

Basically what it does is

  1. For each record in the trigger that has a Married__c value
  2. Create an instance of a People Record
  3. Set the Id of that #2 record to the related Trigger record Married__c
  4. Set the Married__c of that #2 record to the Id of the matching trigger record
  5. Updates the related person records
Related Topic