- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-18-2024 02:27 PM
New to ServiceNow platform so not sure if there is a methodology set in place for this. I have a list field in a form of table A, that can have one or more records selected from table B. Now on the form of table B, I want to add a string field that updates when the form loads with the name/number of the record in table A IF the current record in table B included in the list field. Else the default would be blank. Sorry if this is confusing, let me know if I need to elaborate.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-19-2024 12:33 AM - edited ‎11-19-2024 12:38 AM
Hello @JO_JO ,
There are 3 ways. Do this in table B. Query the table A (in below case problem).
1) Calculated type field (Field will be read-only)
Check the Calculated checkbox in string field.
var arr = [];
var gr = new GlideRecord("problem");
gr.addQuery(<list type field name>, "IN", current.sys_id); // Use "CONTAINS" if "IN" doesn't work
gr.query();
while (gr.next()) {
arr.push(gr.getValue('number').toString())
}
if (arr.length != 0)
return arr.toString();
else
return "";
2) Display BR
var arr = [];
var gr = new GlideRecord("problem");
gr.addQuery(<list type field name>, "IN", current.sys_id); // Use "CONTAINS" if "IN" doesn't work
gr.query();
while (gr.next()) {
arr.push(gr.getValue('number').toString())
}
if(arr.length != 0)
current.setValue(<table B field name>, arr.toString());
else
current.setValue(<table B field name>,"");
3) OnLoad CS with GlideAjax
Use same code written in (2) but with Ajax Call.
Regards,
----
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-19-2024 12:33 AM - edited ‎11-19-2024 12:38 AM
Hello @JO_JO ,
There are 3 ways. Do this in table B. Query the table A (in below case problem).
1) Calculated type field (Field will be read-only)
Check the Calculated checkbox in string field.
var arr = [];
var gr = new GlideRecord("problem");
gr.addQuery(<list type field name>, "IN", current.sys_id); // Use "CONTAINS" if "IN" doesn't work
gr.query();
while (gr.next()) {
arr.push(gr.getValue('number').toString())
}
if (arr.length != 0)
return arr.toString();
else
return "";
2) Display BR
var arr = [];
var gr = new GlideRecord("problem");
gr.addQuery(<list type field name>, "IN", current.sys_id); // Use "CONTAINS" if "IN" doesn't work
gr.query();
while (gr.next()) {
arr.push(gr.getValue('number').toString())
}
if(arr.length != 0)
current.setValue(<table B field name>, arr.toString());
else
current.setValue(<table B field name>,"");
3) OnLoad CS with GlideAjax
Use same code written in (2) but with Ajax Call.
Regards,
----
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-19-2024 10:11 AM
Logic makes sense, but when I tried this the value did not populate to table B field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-20-2024 07:49 AM - edited ‎12-10-2024 09:35 AM
@Ranjane_Omkar Please ignore the previous replies, your solution works. I modified the solution to fit my reference field. One question I do have is how to update table B. Since script runs on display, when I do a search in table view on Table B, the field is not updated. Only when the form opens. Is there a way to save the change so that the table view of Table B will be updated with the new value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2024 10:18 AM
Modified the string field to be a reference field. This works, but slight issue. Since this business rule on Table B runs on Display, the data is never updated. So now when I am on table view on Table B, the field is still empty until I open the record. Is there a way to have the BR update this table so the changes are saved?