- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2024 11:53 AM
I am having trouble with something that seems like it should be easy. On a user record for sys_user, I created a new reference field for SEAT ASSIGNED. When the user record is opened, I want it to fill in the SEAT ASSIGNED with the value of SPACE assigned to that user in the SPACES table. The related list tab is doing this for us already, but I want it in a field so the preview/popup view will show this field-location when that user is referenced.
I had thought that a simple ONLOAD client script would do it, but my searching is showing that I might need a script include and glideajax?
What is the best way to do this?
Below is code I have revamped a hundred times, so I am positive its not working... I cant even get the alert to pop.
function onLoad() {
uservalue = g_form.getValue("user_name");
user = g_form.getReference("user_name");
var seat = new GlideRecord("sn_wsd_core_space");
seat.addQuery('u_assigned_to', user);
seat.query();
alert('popup: ' +user);
while (seat.next()){
var seatspace = seat.sys_id;
g_form.setValue("u_space", seatspace);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 11:48 PM
Hi @jlaps ,
First of all, you should make that field a referenced field and it should reference the 'sn_wsd_core-space' table. Because if you are using that field as a string field, you will not be able to preview that record and the reference icon will not be visible for string field . See below examples
String Field With no reference icon
Reference Field with reference icon
Now lets move towards your script . I have made some changes to it please refer below script
(function calculatedFieldValue(current) {
var spc = new GlideRecord('sn_wsd_core_space');
spc.addQuery('u_assigned_to', current.sys_id);
spc.setLimit(1);
spc.query();
if spc.next()) {
return spc.sys_id.toString();
} else {
return '';
}
})(current);
One point to remember here is that using a calculated value makes the field read-only. By default, you cannot preview a reference field if it's read-only. To address this, we need to set one property to true: 'glide.ui.reference.readonly.clickthrough'.
Here's a scenario I tried in my PDI:
Use Case:The sys_user table has a related list named "Incident Task." To reflect this, I created a field named "Incident Task" in the sys_user table that references the incident_task table. When a task is assigned to any user, it is displayed in both the "Incident Task" related list and the "Incident Task" field (as shown in the screenshot below)
Script for calculate value Which I used -
(function calculatedFieldValue(current) {
var incTask = new GlideRecord('incident_task');
incTask.addQuery('assigned_to', current.sys_id);
incTask.setLimit(1);
incTask.query();
if (incTask.next()) {
return incTask.sys_id.toString();
} else {
return '';
}
})(current);
Outcome of it -
I'm not able to preview record because i have not set property (glide.ui.reference.readonly.clickthrough ) to true .now after setting property to true -
The same can be Achieve with the help of display business rule and onload Client Script
Display Business rule -
(function executeRule(current, previous /*null when async*/ ) {
var incTask = new GlideRecord('incident_task');
incTask.addQuery('assigned_to', current.sys_id);
incTask.setLimit(1);
incTask.query();
if (incTask.next()) {
g_scratchpad.incidentTaskSysId = incTask.sys_id.toString();
} else {
g_scratchpad.incidentTaskSysId = ' ';
}
})(current, previous);
Onload Client Script -
function onLoad() {
g_form.setValue('u_incident_task', g_scratchpad.incidentTaskSysId);
g_form.setDisabled('u_incident_task', true);
}
I spent a lot of time figuring it out. If you could mark it as helpful and accept it as a solution, I'd really appreciate it
Thanks,
Astik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2024 07:40 PM
Hi @jlaps ,
You can achieve same requirement with the help of calculated Value where you can directly do dot walking
For more info follow below link -
https://www.servicenow.com/community/developer-blog/calculate-value-field-use-case/ba-p/2386808
If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.
By doing so you help other community members find resolved questions which may relate to an issue they're having
Thanks,
Astik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 05:57 AM
Thank you, I think this is what I am looking for. I tried modifying the script there to match my situation, but am not getting the field filled in as expected. This is a string field on a sys_user record... any tips on what I am making a mistake with?
(function calculatedFieldValue(current) {
// Add your code here
var spc = new GlideRecord('sn_wsd_core_space');
spc.addEncodedQuery('u_assigned_to=current.sys_id');
spc.setLimit(1);
spc.query();
if (spc.next()) {
return spc.value.toString();
}
})(current);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 11:48 PM
Hi @jlaps ,
First of all, you should make that field a referenced field and it should reference the 'sn_wsd_core-space' table. Because if you are using that field as a string field, you will not be able to preview that record and the reference icon will not be visible for string field . See below examples
String Field With no reference icon
Reference Field with reference icon
Now lets move towards your script . I have made some changes to it please refer below script
(function calculatedFieldValue(current) {
var spc = new GlideRecord('sn_wsd_core_space');
spc.addQuery('u_assigned_to', current.sys_id);
spc.setLimit(1);
spc.query();
if spc.next()) {
return spc.sys_id.toString();
} else {
return '';
}
})(current);
One point to remember here is that using a calculated value makes the field read-only. By default, you cannot preview a reference field if it's read-only. To address this, we need to set one property to true: 'glide.ui.reference.readonly.clickthrough'.
Here's a scenario I tried in my PDI:
Use Case:The sys_user table has a related list named "Incident Task." To reflect this, I created a field named "Incident Task" in the sys_user table that references the incident_task table. When a task is assigned to any user, it is displayed in both the "Incident Task" related list and the "Incident Task" field (as shown in the screenshot below)
Script for calculate value Which I used -
(function calculatedFieldValue(current) {
var incTask = new GlideRecord('incident_task');
incTask.addQuery('assigned_to', current.sys_id);
incTask.setLimit(1);
incTask.query();
if (incTask.next()) {
return incTask.sys_id.toString();
} else {
return '';
}
})(current);
Outcome of it -
I'm not able to preview record because i have not set property (glide.ui.reference.readonly.clickthrough ) to true .now after setting property to true -
The same can be Achieve with the help of display business rule and onload Client Script
Display Business rule -
(function executeRule(current, previous /*null when async*/ ) {
var incTask = new GlideRecord('incident_task');
incTask.addQuery('assigned_to', current.sys_id);
incTask.setLimit(1);
incTask.query();
if (incTask.next()) {
g_scratchpad.incidentTaskSysId = incTask.sys_id.toString();
} else {
g_scratchpad.incidentTaskSysId = ' ';
}
})(current, previous);
Onload Client Script -
function onLoad() {
g_form.setValue('u_incident_task', g_scratchpad.incidentTaskSysId);
g_form.setDisabled('u_incident_task', true);
}
I spent a lot of time figuring it out. If you could mark it as helpful and accept it as a solution, I'd really appreciate it
Thanks,
Astik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2024 06:38 AM
Thank you for all of this, Trying to implement...