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.

Calling Server script from Client script in Declarative action (Related list)

Lars Egelund
ServiceNow Employee
ServiceNow Employee

I have searched this forum for guidance on how to call a server script from client side. Lot's of good material, but still I can't get it to work.
I have a server side script that works:

 

// create a new record in 'Agenda Topic' with the following data: Document = sys_id, type = sub_item
var gr = new GlideRecord('x_snc_meeting_cr_0_agenda_topic');
gr.initialize();
gr.title = current.document.x_snc_meeting_cr_0_project.getDisplayValue();
gr.type = 'item';
gr.meeting = current.x_snc_meeting_cr_0_meeting;
gr.duration = 60;
var itemId = gr.insert();
// Check the result
if (itemId) {
    gs.info('SUCCESS: New record created with Sys ID: ' + itemId);
} else {
    gs.error('ERROR: Failed to create a record.');
}
 
However, I would like the user to be able to input the duration, so I have created a client script action and a script include. The idea is to do the above but with with user input before creating the record.
 
Action Assignment, implemented as Client Script:
function onClick() {
// 1. Get user input (e.g., using a built-in function or a custom modal)
    var userInput = prompt('Enter duration:');
    if (!userInput) return;

    // 2. Prepare and send the request via GlideAjax
    var ga = new GlideAjax('LECreateMeetingAgendaItem');
    //ga.addParam('sysparm_name', 'processUserInput');
    ga.addParam('sysparm_user_duration', userInput);
    ga.addParam('sysparm_project', g_form.getValue('document.x_snc_meeting_cr_0_project').getDisplayValue());
    ga.addParam('sysparm_meeting', g_form.getValue('x_snc_meeting_cr_0_meeting'));
   
    // 3. Send the request
    ga.getXMLAnswer(ajaxResponse);
   
    function ajaxResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        g_form.addInfoMessage(answer); // Show the server's response
    }
}
 
Scrip Include, Client callable, Sandbox enabled:
var LECreateMeetingAgendaItem = Class.create();
LECreateMeetingAgendaItem.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    processUserInput: function() {
        var inputDuration = this.getParameter('sysparm_user_duration');
        var inputProject = this.getParameter('sysparm_project');
        var inputMeeting = this.getParameter('sysparm_meeting');

        var gr = new GlideRecord('x_snc_meeting_cr_0_agenda_topic');
        gr.initialize();
        gr.title = inputProject;
        gr.type = 'item';
        gr.meeting = inputMeeting;
        gr.duration = inputDuration;
        var itemId = gr.insert();
        // Check the result
        if (itemId) {
            gs.info('SUCCESS: New record created with Sys ID: ' + itemId);
        } else {
            gs.error('ERROR: Failed to create a record.');
        }

        return 'Success.';
    },
    type: 'LECreateMeetingAgendaItem'
});
 
The user is being prompted to enter the duration - then nothing happens.
 
Any ideas on what I'm missing?
3 ACCEPTED SOLUTIONS

G Ponsekar
Tera Guru

Hi @Lars Egelund ,

 

Your issue is that you are not specifying which function in your Script Include to call. When using GlideAjax, you must add a parameter named sysparm_name to tell the server which function to execute. 
You correctly commented out the necessary line: ga.addParam('sysparm_name', 'processUserInput');

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Lars Egelund 

are you in custom scope and UI action and script include both are in same scope?

share screenshots.

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

Thanks for sharing. It seems like the script include must be in Global scope. Once I created it here it works.

Thanks for all your support.

View solution in original post

14 REPLIES 14

@Lars Egelund 

those system logs should come in here

AnkurBawiskar_0-1759326656030.png

 

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

I added some client side alerts - it seems like the function ajaxResponse is not called.

 I get the alert('3. Send the request'); but nothing more...

// 3. Send the request
	alert('3. Send the request');
    ga.getXMLAnswer(ajaxResponse);
	
    function ajaxResponse(response) {
		alert('ajaxResponse called');
        var answer = response.responseXML.documentElement.getAttribute('answer');
		alert(answer);
        g_form.addInfoMessage(answer); // Show the server's response
    }

 

Also tried this simple script - the function is not calles (no alert from here) - but alert 3+4 is shown...

function onClick() { 

	var ga = new GlideAjax('LECreateMeetingAgendaItem');
    ga.addParam('sysparm_name', 'processUserInput');
    
	// 3. Send the request
	alert('3. Send the request');
    ga.getXMLAnswer(ajaxResponse);
	alert('4. Request sent, waiting for response');

	function ajaxResponse(response) {
		alert('ajaxResponse called');
	}
}

@Lars Egelund 

alert 4 won't come as the script will go inside the callback

you need to check whether script include function is getting called or not

client side alerts won't help

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

Thanks for sharing. It seems like the script include must be in Global scope. Once I created it here it works.

Thanks for all your support.