Update a string field on current table based on if value exists in a list field from another table

JO_JO
Tera Contributor

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.

1 ACCEPTED SOLUTION

Ranjane_Omkar
Kilo Sage

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.

View solution in original post

4 REPLIES 4

Ranjane_Omkar
Kilo Sage

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.

Logic makes sense, but when I tried this the value did not populate to table B field.

@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

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?