• Hitee Bhasin 23
  • NEWBIE
  • 5 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 5
    Replies
Account is the parent of a custom object in our org (Relationship). I would like to get the parent record's ID and the child record's ID any time there is a new Relationship created. I am writing a trigger to perform some checks..like seeing if the relationship already exists. How would I get the record ID of the Account record and the Relationship record in an apex trigger? Thanks for your help in advance!
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;
    }
}

I'm getting an error that doesn't seem like an error...Maybe there's something I'm not seeing. Relationship is a custom object, and it's API name is Relationship__c. Can anyone help? I'm fairly new to coding. Thank you!

trigger RelationshipReverseRecord on Relationship__c (before insert, before update, before delete)
{
    Set<string> rIDs = new Set<string>();

    if(Trigger.isInsert)
    {
        for(Relationship r : Trigger.New)
        {
            String uID = r.parentID + r.childID;
            String rID = r.childID + r.parentID;
            r.UniqueID__c = uID;
            r.ReverseID__c = rID;
            rIDs.add(rID);
        }
    }
    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>();
    for(Relationship__c newR : Trigger.New)
    {
        if(newR.ReverseID__c == existingR.UniqueID__c)
        {
            found = true;
            break;
        }
    }
    if(found==false && newR.ReverseRecords)
    {
        Relationship__c newReverse = new Relationship__c(ReverseRecord__c = true, Parent__c = newR.Child__c);
        newReverse.Child__c = newR.Parent__c;
        newReverseRecords.add (newReverse);
    }

    insert newReverseRecords;
}

Account is the parent of a custom object in our org (Relationship). I would like to get the parent record's ID and the child record's ID any time there is a new Relationship created. I am writing a trigger to perform some checks..like seeing if the relationship already exists. How would I get the record ID of the Account record and the Relationship record in an apex trigger? Thanks for your help in advance!

I'm getting an error that doesn't seem like an error...Maybe there's something I'm not seeing. Relationship is a custom object, and it's API name is Relationship__c. Can anyone help? I'm fairly new to coding. Thank you!

trigger RelationshipReverseRecord on Relationship__c (before insert, before update, before delete)
{
    Set<string> rIDs = new Set<string>();

    if(Trigger.isInsert)
    {
        for(Relationship r : Trigger.New)
        {
            String uID = r.parentID + r.childID;
            String rID = r.childID + r.parentID;
            r.UniqueID__c = uID;
            r.ReverseID__c = rID;
            rIDs.add(rID);
        }
    }
    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>();
    for(Relationship__c newR : Trigger.New)
    {
        if(newR.ReverseID__c == existingR.UniqueID__c)
        {
            found = true;
            break;
        }
    }
    if(found==false && newR.ReverseRecords)
    {
        Relationship__c newReverse = new Relationship__c(ReverseRecord__c = true, Parent__c = newR.Child__c);
        newReverse.Child__c = newR.Parent__c;
        newReverseRecords.add (newReverse);
    }

    insert newReverseRecords;
}