How to make Varaible to auto populate user department in record producer

akshayp
Tera Expert

So, i have one record producer, and there is one variable on it 'Department'

  i want this variable to auto populate value of department from user table and it should be read only and hence i was thinking to use Default value to get this done.

  but, if user don't have department then this variable should be editable

 

so anyone can help me how i can do this, do i need to script include for making it editable as default value can't do that i guess

 

Thanks!!

1 ACCEPTED SOLUTION

@akshayp ,

Below is the modified code 

var deptField = 'department'; // replace with the name of the 'Department' field on your form
var userDept = g_user.department;
if (userDept) {
  // If the user has a department value, set the value and make the field read-only
  g_form.setValue(deptField, userDept);
  g_form.setReadOnly(deptField, true);
} else {
  // If the user doesn't have a department value, make the field editable
  g_form.setReadOnly(deptField, false);
}
Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

View solution in original post

5 REPLIES 5

Prince Arora
Tera Sage
Tera Sage

@akshayp ,

 

Please write a onLoad client script and write the script as mentioned below:

 

 

var response = g_form.getReference('YOUR_USER_FIELD',myDept);
function myDept(response){
var dept = response.department; 
if(dept){
g_form.setValue('department',dept);
g_form.setReadOnly('department',true);
}
}


 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Thanks for replying, will try this and let you know ASAP

Sai Shravan
Mega Sage

Hi @akshayp ,

Below is the client script you can give a try

var userField = 'YOUR_USER_FIELD'; // replace with the name of the field on your form that contains the user reference
var deptField = 'department'; // replace with the name of the 'Department' field on your form

g_form.getReference(userField, function(response) {
  var dept = response.department;
  if (dept) {
    // If the user has a department value, set the value and make the field read-only
    g_form.setValue(deptField, dept);
    g_form.setReadOnly(deptField, true);
  } else {
    // If the user doesn't have a department value, make the field editable
    g_form.setReadOnly(deptField, false);
  }
});
Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

akshayp
Tera Expert

@Sai Shravan ,  i do not have any field on the form that contains user reference, only this 'Department' (single line text) variable i am having where i need to auto populate user's department