Auto-populate variable with value from sys_user

kevinthury
Tera Guru

On Istanbul for 4 months.

I have created a Catalog Item with variables for entering the Department ID and Accounting Unit values.   I am being asked to auto-populate these two variables based on the person submitting the request.

I am able to do so with a Run Script activity on the associated workflow that dot-walks to the desired field.

current.variables.au = current.request.requested_for.department.id;

current.variables.company = current.request.requested_for.cost_center.code;

find_real_file.png

Our goal is to have the 'au' and 'company' variables visible on the Catalog Item view (Service Portal) and auto-populate.   The purpose being is for ordering items and there may come a time when an order is placed that will need to be charged to a different au/company than the person ordering.

Will a similar script work and where do I place this?   Any assistance is appreciated.

1 ACCEPTED SOLUTION

Hi Kevin,



Reference fields contain two bits of information. A sys_id (which is the unique ID of the field you are referencing) and a display value - one of the fields from the referenced record to be human friendly. Because not too many humans know which user, client script, or ACL this is: 24b9bd5d9363220028517a75e57ffb1d



So to do that, there's a third argument that you cited "displayValue". Someone has defined a field on the company table and department table as the display value. You did a great job in retrieving the sys_id, the record to reference, but the system says "What am I supposed to show the users as a friendly display value?" so it runs back to the server to get that. You have the opportunity to get it with getReferenceAdvanced(). While I haven't used getReferenceAdvanced() enough to remember, based on your example, you could get it like this:



var reqFor = getReferenceAdvancedPortal(g_form, "opened_by", "department.sys_id;department.name;cost_center.sys_id;cost_center.name");
g_form.setValue("company", reqFor.cost_center_sys_id, reqFor.cost_center_name);
g_form.setValue("au", reqFor.department_sys_id, reqFor.department_name);



Visually, it will look the same, but improve performance by only doing a single server call.



Take a look at the Quick tip in Episode 39 of TechNow for more information on display values...


TechNow Episode List


View solution in original post

23 REPLIES 23

I'm not fully understanding your suggestion, so I did a bit of research.   My non-scripting brain is having trouble making sense of a difference between value and dislay.   Using my line of code an as example, can you guide me in the right direction?



var reqFor = getReferenceAdvancedPortal(g_form, "opened_by", "department.sys_id;cost_center.sys_id");
g_form.setValue("company", reqFor.cost_center_sys_id);
g_form.setValue("au", reqFor.department_sys_id);





setValue(fieldName, value, displayValue)


Sets the value and the display value of a field. Will display value if there is no displayValue. To improve performance by preventing a round trip, include a display value in addition to the value.




Hi Kevin,



Reference fields contain two bits of information. A sys_id (which is the unique ID of the field you are referencing) and a display value - one of the fields from the referenced record to be human friendly. Because not too many humans know which user, client script, or ACL this is: 24b9bd5d9363220028517a75e57ffb1d



So to do that, there's a third argument that you cited "displayValue". Someone has defined a field on the company table and department table as the display value. You did a great job in retrieving the sys_id, the record to reference, but the system says "What am I supposed to show the users as a friendly display value?" so it runs back to the server to get that. You have the opportunity to get it with getReferenceAdvanced(). While I haven't used getReferenceAdvanced() enough to remember, based on your example, you could get it like this:



var reqFor = getReferenceAdvancedPortal(g_form, "opened_by", "department.sys_id;department.name;cost_center.sys_id;cost_center.name");
g_form.setValue("company", reqFor.cost_center_sys_id, reqFor.cost_center_name);
g_form.setValue("au", reqFor.department_sys_id, reqFor.department_name);



Visually, it will look the same, but improve performance by only doing a single server call.



Take a look at the Quick tip in Episode 39 of TechNow for more information on display values...


TechNow Episode List


Brilliant, Chuck!!!   Brilliant!   Thanks for the guidance and taking the time to explain the way you did.   The modifications work and this will be a good lesson I can carry with me going forward.


You are welcome. Glad you got it working (efficiently.)