You need to sign in to do that
Don't have an account?
Hitee 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;
}
}
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;
}
}
Few issues with your trigger.
- You don't need an after insert trigger for validations. Use a before insert trigger for this purpose.
- Do not run SOQL query inside a loop. It will cause your code to ht governor limits.
- 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. 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.