0

I am trying to accomplish the following however I'm lost on how reference IDs in my trigger.

In our org, each Relationship record (custom object) ties two Accounts together. Basically, every Account can have multiple Relationship records and through this trigger, whenever a new Relationship is created, I need to pull all the relationship records for the Account ID it's associated to so I can create reverse records and check for uniqueness, etc. An example of a unique ID would be 001G000001zPkCCAAA001G000001zPkBsAAB which is a combination of two account records. The reverse ID would be 001G000001zPkBsAAB001G000001zPkCCAAA.

Right now I am using ParentID__c and ChildID__c as custom formula fields to grab the ID of each of the records but I cannot keep using them because formula fields are not writeable. Any help would be appreciated. My code is below:

trigger RelationshipReverseRecord on Relationship__c (after insert)
{
    Set<string> rIDs = new Set<string>();

    for(Relationship__c r : Trigger.New){
        String uID = r.parentID__c + r.childID__c;
        String rID = r.childID__c + r.parentID__c;
        r.UniqueID__c = uID;
        r.ReverseID__c = rID;
        rIDs.add(r.ReverseID__c);
        rIDs.add(r.UniqueID__c);
        System.debug(r.ReverseID__c);

        List<Relationship__c> existingReverseRecords = [SELECT UniqueID__c, Reverse_Record__c FROM Relationship__c WHERE ID IN : rIDs];
        List<Relationship__c> newReverseRecords = new List<Relationship__c>();

        r.found__c = false;
        for(Relationship__c existingR : existingReverseRecords) {
            if(r.ReverseID__c == existingR.UniqueID__c){
                r.found__c = true;
                //Throw an error
                System.debug('Record Found');
                break;   
            }
        }

        if(r.found__c==false ){
            Relationship__c newReverse = new Relationship__c(parentID__c = r.ChildID__c, childID__c = r.parentID__c, Reverse_Record__c = true);
            newReverseRecords.add (newReverse);
        }


        insert newReverseRecords;
    }
}

1 Answer 1

1

Few issues with your trigger.

  1. You don't need a after insert trigger for validations. Use a before insert trigger for this purpose.

  2. Do not run SOQL query inside a loop. It will cause your code to ht governor limits.

  3. Why are you inserting the Relationship__c records explicitly in the end? The trigger is running BECAUSE an insert operation is going on.

So your trigger should look something like this. I am outlining the trigger.

    trigger RelationshipReverseRecord on Relationship__c (before insert)
    {
        //Loop through all your Relationship__c records which are getting inserted and catch all the possible combinations of Parent and child Ids.
        Set<String> uniqueIds = new Set<String>();
        Set<String> reverseIds = new Set<String>();
        for(Relationship__c r : Trigger.New)
        {
            String uID = r.parentID__c + r.childID__c;
            String rID = r.childID__c + r.parentID__c;
            r.UniqueID__c = uID;
            r.ReverseID__c = rID;
            uniqueIds.add(uID);
            reverseIds.add(rID);
        }
        //Create a map of UniqueId or reverseId to Relationship__c record.
        Map<String, Relationship__c> uniqueIdOrRevIdToRelationshipMap = new Map<String, Relationship__c>();
        for(Relationship__c obj : [SELECT UniqueID__c, ReverseID__c FROM Relationship__c WHERE UniqueID__c IN : uniqueIds OR ReverseID__c IN :reverseIds])
{
    uniqueIdOrRevIdToRelationshipMap.put(obj.UniqueID__c, obj);
    uniqueIdOrRevIdToRelationshipMap.put(obj.ReverseID__c, obj);
}

for(Relationship__c r : Trigger.New)
{
    //check if any of the records UniqueID__c or ReverseID__c Id matches with the map you created. If matches it means it duplicate record.
}

So this should server the purpose for you. Let me know if you need further clarification.

3
  • 1
    Thank you for your help. I'm very new to coding so I appreciate all the input. This is the flow I need to recreate..which is why I thought inserting the records at the end would be important: Relationship record created --> Set Unique ID (combo of IDs for both accounts. set the reverse ID that is also unique) --> check if reverse ID already exists --> If not, create reverse record. If it does, error message. We'll also have to mark the record as a "reverse" record to avoid recursion.
    – user42632
    Mar 23, 2017 at 14:30
  • I still have a question for you, if that's ok. I need to use Parent (Account record ID) and Child (Relationship record ID) instead of the ParentID__c and ChildID__c custom formula fields (these are generating the record ID through a formula, that's all) because formula fields are not writeable. But if I'm not "inserting" the records, that's no longer necessary right?
    – user42632
    Mar 23, 2017 at 14:31
  • Okay, there is a catch. Suppose you have 2 accounts with record Ids A & B. When you are inserting Relationship record with unique value AB, the trigger is called. It will insert the record with AB. But to have a corresponding record with BA, you will have to manually insert it. After insert was the right way then. Mar 23, 2017 at 18:17

You must log in to answer this question.

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