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.

Workspace script for UI action not working

Shubham Gagneja
Tera Guru

I am working on a workspace where I have created a custom table with one field. Field is a lookup number. What I wanted to achieve is, if a user enters a number and clicks the Lookup search button then my script would search for that number in contact table and opens the record in a new tab in the workspace. This is my ui action workspace script. 

function runClientSideCode() {
    g_form.addInfoMessage('Search button clicked!');
    var tableName = "x_ccn_contact";
    var contactNumber = g_form.getValue("contact_number");
    if (!contactNumber ) {
        g_form.addErrorMessage("Please enter a contact number.");
        return;

    }
    var ContactCall = new GlideAjax("ContactSearchAjax");
    ContactCall .addParam("sysparm_name", "findContact");
    ContactCall.addParam("ContactNum", contactNumber );
    ContactCall.getXMLAnswer(function(response) {
        gs.info("GlideAjax Response " + response);

        if (response) {
            var ContactSysId = response;
            gs.info("Contact sysid " + ContactSysId );
            if (ContactSysId) {
                gs.info("GlideAjax Response " + response);
                   g_aw.openRecord(tableName, ContactSysId); // Open contact profile in workspace
            } else {
                g_form.addErrorMessage('Contact not found.');
            }

        } else {
            g_form.addErrorMessage('Error in GlideAjax call.');
        }

    });

    //gsftSubmit(null, g_form.getFormElement(), "clientcode");
}


And this is my server script where i am getting the sys_id of the record
var ContactSearchAjax= Class.create();
ContactSearchAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    findContact: function(ContactNum) {
       
        var gr = new GlideRecord("x_ccn_contact");
        gr .addQuery("contact_number", ContactNum);
        gr .query();
        if (gr .next()) {
            return gr .getValue("sys_id");
        }
        return "";
    }
});
I have checked all the values in UI action, script include is also client calable but my button doesn't execute anything. 

ShubhamGagneja_0-1742508369606.png

 

3 REPLIES 3

Nilesh Pol
Kilo Sage
Kilo Sage

Hi @Shubham Gagneja following are some finding in your script:

  • gs.info Doesn't Work in Client-Side Scripts and ensure that g_aw is properly loaded.
  • Ensure sysparm_name in GlideAjax Matches the Server Function Your GlideAjax call uses: ContactCall.addParam("sysparm_name", "findContact"); and Script Include function is: findContact: function(ContactNum).

Use below updated UI Action script for your reference:

function runClientSideCode() {
g_form.addInfoMessage("Search button clicked!");

var tableName = "x_ccn_contact";
var contactNumber = g_form.getValue("contact_number");

if (!contactNumber) {
g_form.addErrorMessage("Please enter a contact number.");
return;
}

var ContactCall = new GlideAjax("ContactSearchAjax");
ContactCall.addParam("sysparm_name", "findContact");
ContactCall.addParam("sysparm_ContactNum", contactNumber); // Fix parameter name

ContactCall.getXMLAnswer(function(response) {
console.log("GlideAjax Response: ", response);

if (response) {
var ContactSysId = response;
console.log("Contact sys_id: ", ContactSysId);

if (ContactSysId) {
g_navigation.openRecord(tableName, ContactSysId); // Open contact profile in workspace
} else {
g_form.addErrorMessage("Contact not found.");
}
} else {
g_form.addErrorMessage("Error in GlideAjax call.");
}
});
}

 

Script Include

var ContactSearchAjax = Class.create();
ContactSearchAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
findContact: function() {
var ContactNum = this.getParameter("sysparm_ContactNum"); // Fix parameter name

var gr = new GlideRecord("x_ccn_contact");
gr.addQuery("contact_number", ContactNum);
gr.query();

if (gr.next()) {
return gr.getValue("sys_id");
}
return "";
}
});

Hey Nilesh, Thanks for getting back. I believe g_navigation is for ui action client script but now for WOrkspace script. I tried it didn;t work!

avensisd
Tera Contributor

Server Code Verification: You need to verify that the GlideRecord is returning data correctly and that the findContact function on the server is working with the request.