Workspace 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 ContactSearchAjaxClass.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-1742512843350.png

 

2 ACCEPTED SOLUTIONS

@Shubham Gagneja  I was able to achieve this in my pdi with below code, you might need to do some changes to your code as follows.

 

UI Action:

JohnnySnow_0-1742528265929.png

 

JohnnySnow_1-1742528279804.png

UI Action script

function onClick(g_form) {
    g_form.addInfoMessage('Search button clicked!');
    var tableName = "sys_user";
    var contactNumber = g_form.getValue("close_notes");
    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);
    ContactCall.getXMLAnswer(_handleResponse);

    function _handleResponse(answer) {

        if (answer) {
            var ContactSysId = answer;
            if (ContactSysId) 
                g_aw.openRecord(tableName, ContactSysId); // Open contact profile in workspace
             
		}
    }
}

 

your script include seems to be incorrect

JohnnySnow_2-1742528339404.png

Script include code

var ContactSearchAjax = Class.create();
ContactSearchAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    findContact:function() {
		gs.info("*** Script log entered");
		var mobNo = this.getParameter('sysparm_ContactNum');
		gs.info("*** Script log " + mobNo);
		var gr = new GlideRecord("sys_user");
		gr.addQuery("mobile_phone", mobNo);
		gr.query();
		if (gr.next()) {
			gs.info("*** Script log " + gr.getUniqueValue());
			return gr.getUniqueValue();
		}
		

    },
    type: 'ContactSearchAjax'
});

 

Please change the parameters and table name as per your instance, I've used the ootb user table for this

 

Please mark it as helpful or correct if it helped you resolve the issue or worked for you.

 

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Shubham Gagneja 

the way how you are getting the value from Ajax, it's wrong

Also field contact_number on current form is string type

update as this

var ContactSearchAjax= Class.create();
ContactSearchAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    findContact: function() {
       var ContactNum = this.getParameter('ContactNum');
        var gr = new GlideRecord("x_ccn_contact");
        gr.addQuery("contact_number", ContactNum);
        gr.query();
        if (gr.next()) {
            return gr.getUniqueValue();
        }
        return "";
    }
});

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

12 REPLIES 12

JohnnySnow
Kilo Sage

@Shubham Gagneja 

1. What are you trying to achieve here? If it is just opening a record in new tab, then why are you not using the OOTB global search option?

2. Which version are you in?  If Xanadu, refer this link https://www.servicenow.com/docs/bundle/xanadu-it-service-management/page/product/incident-management...

JohnnySnow_0-1742514167113.png

Please mark it as helpful or correct if it helped you resolve the issue or worked for you.

 

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.

Like i mentioned, if I enter a user_name in my field and click on the ui action, I am trying to open a record based on the user_name that I entered. I need to use this ux and the field for that lookup search. 

@Shubham Gagneja can you share your script include as well here and the screenshots of the UI action that you have created.

I'm not sure if you have noticed, there are lot of extra spaces like here:

ContactCall .addParam("sysparm_name""findContact");

 

  gr .addQuery("contact_number", ContactNum);
        gr .query();
        if (gr .next()) {
            return gr .getValue("sys_id");
 
make sure that is not there in the actual script

 

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.

@Shubham Gagneja  I was able to achieve this in my pdi with below code, you might need to do some changes to your code as follows.

 

UI Action:

JohnnySnow_0-1742528265929.png

 

JohnnySnow_1-1742528279804.png

UI Action script

function onClick(g_form) {
    g_form.addInfoMessage('Search button clicked!');
    var tableName = "sys_user";
    var contactNumber = g_form.getValue("close_notes");
    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);
    ContactCall.getXMLAnswer(_handleResponse);

    function _handleResponse(answer) {

        if (answer) {
            var ContactSysId = answer;
            if (ContactSysId) 
                g_aw.openRecord(tableName, ContactSysId); // Open contact profile in workspace
             
		}
    }
}

 

your script include seems to be incorrect

JohnnySnow_2-1742528339404.png

Script include code

var ContactSearchAjax = Class.create();
ContactSearchAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    findContact:function() {
		gs.info("*** Script log entered");
		var mobNo = this.getParameter('sysparm_ContactNum');
		gs.info("*** Script log " + mobNo);
		var gr = new GlideRecord("sys_user");
		gr.addQuery("mobile_phone", mobNo);
		gr.query();
		if (gr.next()) {
			gs.info("*** Script log " + gr.getUniqueValue());
			return gr.getUniqueValue();
		}
		

    },
    type: 'ContactSearchAjax'
});

 

Please change the parameters and table name as per your instance, I've used the ootb user table for this

 

Please mark it as helpful or correct if it helped you resolve the issue or worked for you.

 

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.