0

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;

}

1 Answer 1

2

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
5
  • Thank you very much for your help. Could you please clarify regarding a part below when an instance of a People Record is creating. People__c also has fields Name and Surname__c. So should I create a constructor for it? If yes then how? Thank you very much in advance. Commented Feb 21, 2017 at 23:03
  • I see next error after using code above: Error: Invalid Data. Review all error messages below to correct your data. Apex trigger InsertUpdateMarried caused an unexpected exception, contact your administrator: InsertUpdateMarried: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a000Y000009rYg9QAE; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, InsertUpdateMarried: maximum trigger depth exceeded Commented Feb 21, 2017 at 23:12
  • @AlexNekrasov - I do not understand what you are asking. All I am doing in the code is updating a specific record with the ID of the related person that was being inserted/updated. No need to add any other fields
    – Eric
    Commented Feb 21, 2017 at 23:14
  • @AlexNekrasov - Yea, see the note I added. You will need to handle the recursion since you are operating on the same object. Best bet is to move to a class and handle the recursion check there. Do a search and you will find lots of information on it
    – Eric
    Commented Feb 21, 2017 at 23:17
  • Oh, I got it! Thank you very much for your help, Eric! I really really appreciate it. Commented Feb 21, 2017 at 23:25

You must log in to answer this question.

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