- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
I have something really weird going on. I am building a Catalog Item that uses the Delegated Request Experience (so they can select multiple people under Requested For). The issue is that they want to show the Manager and Cost Center for each person selected. On the Catalog Item view, since these fields would only appear once, and you could have multiple people requested, that would not work too well to see it there.
So what I did was add those two references fields to the Catalog, but not have those value looked up/populated until after the request has been submitted and multiple RITMs have been created (because ServiceNow will create a separate RITM for each Requested For person).
I did this by creating an "onLoad" Catalog Client Script that only runs against Requested Items and Catalog Tasks (and not Catalog Item View). So it gets populated when the RITM or Task is first opened (which is fine, as this information is only for viewing purposes anyway). Here is what that code looks like:
function onLoad() {
var req_for = g_form.getReference('requested_for', setUserFields);
function setUserFields(req_for){
g_form.setValue('manager',req_for.manager);
g_form.setValue('cost_center',req_for.cost_center);
}
}The code works fine, and does exactly what it is supposed to. However, there is one slight issue. That code runs EVERY time the Requested Item is opened. So, if someone were to open an old request, and the Requested For person's Manager had changed since the record was first submitted, the Catalog Client Script will update the Manager field to show the current Manager (not the one at the time of original request). I don't want that to happen. Once initially populated, I do not want these fields to change.
So I tried amending my Catalog Client Script to first check to see if the Manager field is already populated. If not, then populate the fields. If it is already populated, do not do anything (no updates). However, my changes were not working, it was still updating records that already had values in my testing.
So I went back to my original code, and wanted to see what it was showing the value of the Manager field. For some reason, the "g_form.GetValue" command does not seem to be working properly. When I try to apply it in my code, it never returns any value. I even tried it at the end of my code, after I set the value, and it still return nothing.
Here is what my code test looks like:
function onLoad() {
//see if manager is populated
var mgr = g_form.getValue('manager');
alert('Manager: ' + mgr);
//call function to populate manager and cost center
var req_for = g_form.getReference('requested_for', setUserFields);
function setUserFields(req_for){
g_form.setValue('manager',req_for.manager);
g_form.setValue('cost_center',req_for.cost_center);
}
//verify manager after setting
var mgr2= g_form.getValue('manager');
alert('Manager2: ' + mgr2);
}
Both the alerts do not return any value.
Why does "g_form.setValue" seemd to work, but "g_form.getValue" NOT seem work?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi,
You should use a business rules for that.
When to run: before insert
Condition: item is [your catalog item]
script:
If it's useful, please mark it as correct.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi,
You should use a business rules for that.
When to run: before insert
Condition: item is [your catalog item]
script:
If it's useful, please mark it as correct.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Ricardo26,
Thank you! That works perfectly!
Just wondering though, do you have any idea why my original method would not work? Just trying to understand.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi,
The g_form.setValue() runs on the client side (browser), and no changes are made on the server side (database). That means that every time your form/view was loaded, you were running a onLoad script that would get the manager and set it on the right field, but you were not updating the record on the database, only in the browser.
The business rules is a better approach in this case because it's already on the server side, so you can set the values on the record before it's even sent to the database, and the record will already be created whit the corrects values. Also, it will run only one time, as oppose to the client script that runs to every load.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Thank you for the excellent explanation!
