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?
4 REPLIES 4

G Ponsekar
Giga 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

Thanks, makes sense. However, it didn't make any difference...

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

Yes, all is in a custom scope.