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
Hitee Bhasin 23Hitee Bhasin 23 

record id

Hello,
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 suggestions?? 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;
    }
}
NagendraNagendra (Salesforce Developers) 
Hi Bhasin,

Few issues with your trigger.
  1. You don't need an 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.
}
Hope this helps.

Mark it as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering similar issue.

Thanks,
Nagendra.