3

I have a two Parent objects. There is a junction between these two objects (not via master detailed but lookup).

I need to implement a custom rollup of these child records on the parent object.

In the trigger logic i do the following:

trigger on Child_Object ( before insert ) {
    helper(trigger.new);
}

Below is the helper logic:

helper(list<childObject> childlist) {

    list<Id> parentID = new list<ID>();
    // DO SOME PROCESSING HERE.
    for ( childObject cvar : childlist ) {
        parentId.add(cvar.parent_lookup__c);
    }
    // I HAVE DONE SOME PROCESSING ON THE PARENT RECORD ROLLUP AND UPDATING PARENT RECORDS.

    IF (parentList.size() > 0)
        update(parentList);
}

I am having a validation on the Parent that is restricting the update, which should not allow insertion of the child and fail the child records.

Unfortunately, I am unable to capture the error in the helper class using try and catch and show exception message on the child record on creation.

Could you please help me form a solution for the same.

Even tried using "database.SaveResult[]", but this fails since I am unable to capture the child reference in the saveResult as the action I update the parent and parent records fail.

How do i find the corresponding child that is causing the error.

TIA.

1

Your trigger is shown as a before insert trigger. I think that's a large part of your problem. If you stop and think about it, you can't really update a "roll-up" on records before the new record exists can you? If you can, I don't really see how. At a minimum, this needs to be an after insert trigger.

Only after the record has been inserted can you actually update the roll-up. If you want to use some custom visualforce, you could show "what it would be" on a visualforce page before inserting the new record, but you can't really update the field until the new records that support it exist.

  • I agree with crmprogdev here. I'd also suggest using @future with the after trigger if this is a heavily customized object (lots of trigger work). – drakored Jun 10 '13 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.