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

Chuck Tomasi
Tera Patron

Hi Kevin,



I'm not sure you can just create variables out of thin air in a script and have them appear on a form. The best practice would be to create the variables on the catalog item and use a script (e.g. business rule) to populate the values of your two variables from the request.requested_for dot-walking values.


The variables exist and are visible on the form.   I simply want them to populate the value from the sys_user record of the individual form whom the order is requested for.


Then you are going about it the right way. I recommend doing this via a business rule, but there are other methods.


This is new territory for me, so forgive questions that may seem quite basic.   I'm struggling at the correct approach to make this work.   I have started a business rule against the Catalog Item Catalog table.   Runs before Insert or Update and the 'Catalog Item is Software Request'.



The script I have setup is as follow, but not working.



(function executeRule(current, previous /*null when async*/) {



          var au = current.requested_for.department.id;


          var company = current.requested_for.cost_center.code;


          current.variables.au = au;


          current.variables.company = company;


 


})(current, previous);