Does anyone know if there is a way to validate before a related record is detached? Or if having the related look-up field required should stop the ability to detach all related records? From what I'm finding, only an after update trigger will fire when attaching/detaching but I was hoping there was something I'd missed.
Thanks.
Hi Steven,
There is a way to compare the current and previous value of a lookup field. You can create a field validation on your lookup field, and use {!relationshipToken} to get the current value in the form and {!relationshipToken#before} to get the previous value. The tokens will return the IDs of your lookup.
Use JavaScript to determine which record is detached from the lookup since you have the original and current value. Then, use return "error message" in the formula body of your validation to prevent the record from being saved.
Regards,
Godfrey
Hi and thanks for the response.
I actually tried using field validation but since the record isn't in edit mode (we have the related record grid on the view page only) it didn't fire. At least it didn't show up in the trigger debug info window. Using your suggestion, I did try to reset the field back to the before value on an after update trigger. I figured at the very least, I could just get the original value back in but that didn't work either. The before and after values never changed after the detach. Not sure why.
Thank you for the additional information. I was able to reproduce the issue by testing the validation using "detach" and "attach" of related list views. The #before will not work because the trigger is not even running at all. I'm now checking with Development if this is an expected behavior.
I also tested using {!relationshipToken} and {!relationshipToken#before} on "After Create" triggers but I was able to get the correct result. Perhaps you can share your code?
Regards,
Godfrey
I was using a 'Update Field Value' trigger with an 'After Update' timing if that makes a difference. But according to the trigger debugger, it didn't even run when the record was detached, only when attached.
Here is the code i was hoping would execute
if (({!R106819974#id} <= 0) && (parseInt("{!R106819974#before}") >= 0))
{
return parseInt("{!R106819974#before}");
}
Thanks,
Steve
It seems the main cause of the issue is the "attach" and "dettach". Have you considered using other methods to detach the records?
Using workflows, I might be able to provide you a workaround solution for this if the relationship cardinality between the parent and child object is one-to-many, respectively.
Thanks for the offer, really appreciate it, but I wouldn't worry about it. We like the quick intuitive attach/detach functionality so I doubt we would want to take that away. Plus, we discussed and it isn't the worst thing in the world for these records to be orphaned. They just aren't that important and we don't know if our users will run into that often. I just see it as a bug and was hoping for a quick solution that someone else may have ran across as I am pretty new to Rollbase.
Thanks again Godfrey,
Steve