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 

make your script include Accessible from All scope and hopefully it should solve

AnkurBawiskar_0-1759320518872.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

Thanks Ankur, I've done this now. However, it still doesn't work.

@Lars Egelund 

you are not calling the script include function, I could see the line is commented

So I un-commented and try that

Try to see if gs.info() comes i.e. whether script include function is getting called or not

ga.addParam('sysparm_name', 'processUserInput');

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

That was pointed out earlier - I have un-commented it. Still not working.

 

How do I check gs.info()?

 

If this is the correct place then no transactions in the log.