You need to sign in to do that
Don't have an account?
MLamb2005
My Lookup update trigger won't update a Master Detail field...
Hi all,
I started trying to update a Lookup with Apex, and I've successfully done so (thread here: http://community.salesforce.com/sforce/board/message?board.id=apex&thread.id=10227). The resulting code is as follows:
Now, I've changed the Lookup to a Master-Detail field and the code no longer works. It appears that the error checking Salesforce performs on the M-D field happens before the trigger, despite the "before insert" clause. Additionally, I did have the "before update" clause, but that caused issues for previously created records, so I removed it.
Any guidance would be very helpful...
Thanks!
Matt
I started trying to update a Lookup with Apex, and I've successfully done so (thread here: http://community.salesforce.com/sforce/board/message?board.id=apex&thread.id=10227). The resulting code is as follows:
Code:
trigger updateEmployeeOnProjectHour on Lab_Project_Hours__c (before insert) { for(Lab_Project_Hours__c lb : Trigger.new) { String currentUserId = UserInfo.getUserId(); User thisUser = [select thisUser.Employee_Page__c from User thisUser where (thisUser.Id =: currentUserId)]; lb.Employee_Name__c = thisUser.Employee_Page__c; } }
Any guidance would be very helpful...
Thanks!
Matt
is Employee_Name__c your master detail field? and what sobject is it looking up?
The three objects in play are:
- Employee - (Tracks employee data, one record per employee)
- Lab Project - (Tracks project data, one record per project
- Lab Project Hour - (Tracks hours spent on projects, associated with an Employee and a Lab Project)
Yes, Employee_Name__c is the Master-Detail field that exists on the Lab Project Hour record, looking up to an Employee record.(As I mentioned before in more general terms, Lab Project Hours are always created from a Lab Project page, so the Master-Detail field for Lab Project Name is taken care of by auto-population.)
Thanks,
Matt
Message Edited by mikef on 12-04-2008 04:02 PM
Message Edited by mikef on 12-04-2008 04:02 PM
I do have two record types on the Lab Project Hour record, would that make any difference?
Thanks,
Matt
Bump for the next week...
Anyone have any suggestions, to the above code? The issue is still that the "before insert" isn't actually working that way; the Salesforce required field check kicks in before the Apex code actually runs. I know the data retrieve via query works, because if I roll the field back to a Lookup, everything works just fine.
Thanks,
Matt
Hi guys,
I ran into this post while struggling with the same problem. It can be resolved now thanks to the 'Reparentable Master Detail' option on the Master-detail relationship. That one allows for an empty master field, which in turn allows the trigger code to run.
Hope this is still useful for someone :-)
Guy
I'm wondering why this doesn't work for me.
My object has two masters to its detail. Both are reparentable.
According to both the system console and the debug log, the record's creation is never even attempted--suggesting Salesforce is rejecting the attempted create even before the trigger fires.
Of course, the field shows-up with a red bar.
If you do this via the standard create/edit screen, it will still not work indeed, because Salesforce fires all validations and also required field checks upon screen submission (so indeed even before the actual record creation kicks in).
The actual solution depends on what you are trying to accomplish.
If you have an automated script (apex code) determining the master(s), the solution might be to use a VisualForce page instead of the standard page layout. That way you can skip the initial checks, add the required Master fields and only then insert the record (and the checks and validations will be run).
Yet another reason validation and edit-checks should be the responsibility of the back-end and not the front.
Maybe we'll try to figure out how to re-implement the "new row" button to load the fields ahead of time.
The validations are loaded with the page (for standard pages) in order to increase the responsibility to the end-user (no call to the server needed for screen feedback).
In some cases that might be not what you want. Therefore there's VisualForce (or possibly even Javascript) which can override the standard 'New' button with anything you want.