You need to sign in to do that
Don't have an account?
Hitee 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!
Let me know if it helps you.
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
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?
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);
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;
}
}