0

Resolved. Object's sfid wasn't created yet. Changed the trigger to after trigger.

I've looked at other posts, but for some reason I cannot find a solution. Basically, when a new object is inserted into my duct-taped database linked list, the child doesn't update it's parent_ID.

Here's the code:

trigger Parent_ID on Object_Pool__c (before insert) {
    for (Object_Pool__c obj : Trigger.new) {
        ID theId = obj.parent_id__c;
        ID objID = obj.Id;
        List<Object_Pool__c> before = [SELECT parent_id__c from Object_Pool__c WHERE parent_id__c = :theId];

        if(before.size() > 0) {
            Object_Pool__c curr = before[0];
            curr.parent_id__c = objID;
            update curr;
        }
    } 
}

curr.parent_id__c is becoming a null value instead of the the obj.ID's value.
I've tried curr.parent_id__C = obj.Id; as well.

Edit: Is it because the object hasn't been inserted yet? Would after keyword instead of before work?

2 Answers 2

1

Well I'm glad to solved that problem, but your trigger will fail because it isn't bulkifed even the slightest.

Please take a look at this https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code

This is important so even though it is working, you will hit your SOQL Limit for sure.

1
  • Hey, thanks for the heads up! I'll go ahead and do that, as well as call update outside of the loop.
    – Sam
    Aug 4, 2014 at 18:54
0

Wow, I deserve to go into Stackoverflow's hall of fame of most embarrassing questions. The object wasn't inserted yet, therefore, it could not have a sfid, thus a null field.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .