pre-populate a relationship field

Hi There,
I have a couple of modules…hr_employees and hr_employees_expenses (which has a many-2-one relationship with hr_employees)

All users of the crm are in the employees table. so what i am needing it to do is pre populate the employee field in custom_employee_expenses based on the user that is currently logged in.

I tried creating some jquery to do this for me but i get an error when i tried to save the record. If it was just a normal text field then this would have worked a treat. the code hides the assigned to box and the buttons and table cell value and populates the other field.


  $(document).ready(function(){
    var txt = $('input:text[name=hr_employee_expenses_hr_employees_name]').val();
    if(jQuery.trim(txt).length == 0)
{
  $("#assigned_user_name_label").hide();
  $('button:button[name=btn_clr_assigned_user_name]').hide();
  $('button:button[name=btn_assigned_user_name]').hide();
  $('input:text[name=assigned_user_name]').hide();
 var user_name = $('input:text[name=assigned_user_name]').val();  
  $('input:text[name=hr_employee_expenses_hr_employees_name]').val(user_name);
}
});

How can i get it to pre populate this field properly based on the assigned to?

thanks
Jamie

As well as setting the text field there is a hidden field with the id that you will need to set as well.

Thanks for that Ewan, I found the hidden field.
Can i use jquery to lookup the hr_employees table and return the id for me? if so how?

thanks
Jamie

If its just the assigned user then there should already be a hidden id in the form you can access.

For anything else you’d probably have to make an AJAX request and add a new action to the modules controller to return the info.