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 

How to get record ID of parent object (Account) and child object (Relationship [custom object])?

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!
Balayesu ChilakalapudiBalayesu Chilakalapudi
You can get child record id with the parent ID in a single query like this,
SELECT Id,(SELECT Id FROM custobj__r) FROM Account
And iterate your query list to find if it has a relationship or not like this,
Set<Id> custidset=new Set<Id>();
for(CustObj__c c:Trigger.new)
    custidset.add(c.Id);
List<Account> acclist=[SELECT Id,(SELECT Id FROM custobj__r) FROM Account];
for(Account acc:acclist){
   for(CustObj__c c:acc.custobj__r){
     if(custidset.contains(c.Id){
         System.debug('relationship already exists');
         break;
     }  
   }
}
where custobj is the name of your child relationship.
Let me know if it helps you.
Prithviraj_ChavanPrithviraj_Chavan
Hi,
 create a master detail relationship and than on master create a roll up summary field of child with count. then you write validation on rule rollup summary field to check for >1 .. so it will give you error if it has more than one record for same master detail relation values..
Or
suppose Account is master and contact is child then you can query like
Select Id,Name,Account.Id From Contact Where Account.Id != NULL

 
Hitee Bhasin 23Hitee Bhasin 23
Thank you both these answers helped me a lot!!
Hitee Bhasin 23Hitee Bhasin 23
@Bala - I realize I didn't ask the question in the correct way. What I need is to actually retrieve the record ID of the Account and the Relationship so I can put them together in a string and insert that value back into the database. Here is a summary of what I am trying to accomplish: 

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.

So how would I get the record ID of each individual object's record so I can use them throughout my trigger AND insert it back?
 
Balayesu ChilakalapudiBalayesu Chilakalapudi
option1) Use newMap, it has A map of IDs to the new versions of the sObject records.
select id FROM Account WHERE parentid in : Trigger.newmap.Keyset()

option2) You can also use substring()  to get each individual id from the long id like this
String longid='001G000001zPkCCAAA001G000001zPkBsAAB';
String id1=longId.substring(0,17);
String id2=longid.substring(17,35);
Hitee Bhasin 23Hitee Bhasin 23
Hi Bala, 

Thank you for your reply! I'm so new to coding, I'm hoping you can look over my code and tell me if this looks right? 

Based on what you've said, I'm a little confused on how to replace ParentID__c and ChildID__c (which are formula fields right now) with the actual record ID fields.

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;
    }
}
Balayesu ChilakalapudiBalayesu Chilakalapudi
Given code will work perfectly.