Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Display if change has attachment(s) in CAB workbench

Michael Laban
Tera Expert

We have a request to indicator if attachments exist on a change request that is being displayed at CAB utilizing CAB Workbench.  Currently, CAB Workbench has no indicator if attachments exist.  After searching for like solutions and opening a case with ServiceNow, it appears CAB Workbench is a portal will not use code like the Platform/Legacy UI.  

 

For example, I have tried:

var chg_id = g_form.getUniqueValue();

var chg_attach_cnt = new GlideRecord("sys_attachment");

chg_attach_cnt.addQuery("table_name", "change_request");
chg_attach_cnt.addQuery("table_sys_id", chg_id);
chg_attach_cnt.query();
var attachment_count = 0;
while (chg_attach_cnt.next()){
    attachment_count++;
}
I have validated that g_form.getUniqueValue(); does return the sys_id of the change request being displayed.  Note:  
g_form.getUniqueValue() and g_form.getSysID() do NOT work in this instance.  
 
If I use the code above in legacy view the number of attachments is counted and displays just fine.  However in CAB Workbench no value is returned.  
 
I have also tried creating a custom field on the change_request table with a calculated value.  That also does not work and returns no value in CAB Workbench ... once again, it works in the platform.  
 
Through searching forums and the Google searches I have not come across any other solution.  
 
Has anyone been able to achieve an indicator that a change request has an attachment within CAB Workbench?  If so, how?  
 
Thank you in advance.
1 ACCEPTED SOLUTION

Michael Laban
Tera Expert

ServiceNow provided this solution-

function onLoad() {
    var chg_id = g_form.getUniqueValue();                       
    var gr = new GlideRecord("sys_attachment");
    gr.addQuery("table_name", "change_request");
    gr.addQuery("table_sys_id", chg_id);
    gr.query(function(response){
        if (response.next()){
            g_form.showFieldMsg('number', "Has attachment(s)", "info", false);
        }
    });
}

With the additional detail of:
1 - We discourage GlideRecord on the client in favour of REST and or the scratchpad on the server side.
2 - The GlideRecord code only work in UI16 because it's forgiving, as we can see the warning in console:
***WARNING *** GlideAjax.getXMLWait - synchronous function - processor: AJAXGlideRecord
*** WARNING *** GlideRecord synchronous query for table: sys_attachment
3 - Service Portal does not support synchronous ajax calls, so the call was never made.

We've refactored you code to support both UI16 and SP, but we highly suggest using a better method and not clientside GlideRecord.

View solution in original post

1 REPLY 1

Michael Laban
Tera Expert

ServiceNow provided this solution-

function onLoad() {
    var chg_id = g_form.getUniqueValue();                       
    var gr = new GlideRecord("sys_attachment");
    gr.addQuery("table_name", "change_request");
    gr.addQuery("table_sys_id", chg_id);
    gr.query(function(response){
        if (response.next()){
            g_form.showFieldMsg('number', "Has attachment(s)", "info", false);
        }
    });
}

With the additional detail of:
1 - We discourage GlideRecord on the client in favour of REST and or the scratchpad on the server side.
2 - The GlideRecord code only work in UI16 because it's forgiving, as we can see the warning in console:
***WARNING *** GlideAjax.getXMLWait - synchronous function - processor: AJAXGlideRecord
*** WARNING *** GlideRecord synchronous query for table: sys_attachment
3 - Service Portal does not support synchronous ajax calls, so the call was never made.

We've refactored you code to support both UI16 and SP, but we highly suggest using a better method and not clientside GlideRecord.