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

Hi Kevin,



If that's not working, try converting the values to a string. I've seen some issues (although not specifically with variable copying values...) The key to remember is every field (value) is actually an object.



Try this and let me know.



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



          current.variables.au = current.requested_for.department.id.toString();


          current.variables.company = current.requested_for.cost_center.code.toString();


 


})(current, previous);


That update didn't work.   Based on my admittedly limited experience with SN, I suspect the scripting portion will not be as simple as I have it.  


If this business rule is running on the Requested item (sc_req_item) table, department and cost_center are non-null values, this should work. I validated that id is on the cmn_department table, and code is a valid field on the cmn_cost_center table.



Technically, I'm not seeing anything wrong. You could potentially inject some debug statements or if you're on Istanbul or later, use the script debugger to step through and find out what's happening.



Example debug statement.


gs.info('requested_for=' + current.requested_for);


gs.info('department=' + current.requested_for.department);


gs.info('ID=' + current.requested_for.department.id);



If something says "undefined", we're going to have problems.


Yes, if the Business Rule (BR) is running on the RITM, the variables do populate after submitting/ordering from the Catalog Item.   What we are looking to do is populate those variables upon loading the form.  



The image below shows the Catalog Item loaded (in ITIL view).   At this point, there is no RITM record to reference and I'm failing to understand if the BR is on the RITM table, how will it pull any data to populate the variables.   This is the reason why I initially had the BR pointing to the   Catalog Items Catalog [sc_cat_item_catalog] table.



find_real_file.png


OK, if you're trying to do this on form load, then I recommend taking a look at Michael Ritchie's getReferenceAdvanced() utility that makes it easy to get information related to a record and populate it on the form using g_form.setValue() - in a client script.



getReferenceAdvanced, g_form.getReference and GlideAjax Alternatives