function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MLamb2005MLamb2005 

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:

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;
    }

}

 
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

MLamb2005MLamb2005
Forgot to mention, if it matters, the target object is a Junction Object, with two Master Detail relationships.  One of them is auto-populated when the user clicks "New", since the user was on a record of that Master object when they clicked New.  My trigger is trying to update the second M-D field.
mikefmikef
a few questions

is Employee_Name__c your master detail field? and what sobject is it looking up?
MLamb2005MLamb2005
Hi Mike,

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
mikefmikef
try this

Code:
trigger updateEmployeeOnProjectHour on Lab_Project_Hours__c (before insert) {
    
   Id employeePageId = [select Employee_Page__c 
from User
where Id = : UserInfo.getUserId() Limit 1].Employee_Page__c;
for(Lab_Project_Hours__c lb : Trigger.new) { lb.Employee_Name__c = employeePageId; } }

 



Message Edited by mikef on 12-04-2008 04:02 PM

Message Edited by mikef on 12-04-2008 04:02 PM
MLamb2005MLamb2005
Thanks Mike.  Unfortunately that didn't work, I'm still not able to insert new records before the "you didn't enter a value for the Employee Name" check kicks in.

I do have two record types on the Lab Project Hour record, would that make any difference?

Thanks,
Matt
MLamb2005MLamb2005

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

GuyClairboisGuyClairbois

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

tggagnetggagne

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.

GuyClairboisGuyClairbois

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).

 

tggagnetggagne

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.

GuyClairboisGuyClairbois

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.