Not able to call a script include function from a UI page using GlideAjax

Rahul Chand
Tera Contributor

I am using GlideAjax to call a script include function 'acceptPayment' from a UI page but it is not able to run the function , both UI page and script include are in same scope and when I try to run the script include from the same GlideAjax code from a widget it works perfectly fine. 

This is my client script code :

 

function myFunction() {
var number = document.getElementById('number').value;
var paymentId = document.getElementById('paymentId').value;
var PayerID = document.getElementById('PayerID').value;
 
var ga = new GlideAjax('global.PaypalAPI');
 
ga.addParam('sysparm_name','acceptPayment');
ga.addParam('sysparm_payment_id',paymentId);
ga.addParam('sysparm_payer_id',PayerID);
ga.addParam('sysparm_record_number',number);
 
ga.getXML(myCallBack);
}
 
 
 
and below is the script include function 'accept payment':

acceptPayment: function() {
gs.info('abcd acceptPayment');
var token = this.getToken();
var payer_id = this.getParameter('sysparm_payer_id');
        var payment_id = this.getParameter('sysparm_payment_id');
var record_number = this.getParameter('sysparm_record_number');
        if (token[0] == 200){
try {
var r = new sn_ws.RESTMessageV2('paypal', 'Accept Payment');
r.setStringParameterNoEscape('payer_id', payer_id);
r.setStringParameterNoEscape('token', token[1]);
r.setStringParameterNoEscape('payment_id', payment_id);
 
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
if(httpStatus == 200 || httpStatus == '200'){
}
} catch (ex) {
var message = ex.message;
}
}
    }
 
 
myFunction();
 
function myCallBack(response){
var redirectUrl = response.responseXML.documentElement.getAttribute('answer');
top.window.location.href=redirectUrl;
}
 
 
8 REPLIES 8

Peter Bodelier
Giga Sage

Hi @Rahul Chand,

 

Have you verified that your UI page Client script is executing correctly?

Place some debugging in the to see if it at least is running up until the getXML.

 

Since everything is in global scope, you don't need to place global. in front of the script include name. (Althought I doubt that is the issue).


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Yes ,UI page is working perfectly fine. But code is not running after the GlideAjax call and yes global. does not make any difference I have tried both ways.

Hi Rahul,

 

You say the code is not running after GlideAjax call. Does that mean, that the script include is called, but the callback is not triggering?


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Amit Gujarathi
Giga Sage
Giga Sage

HI @Rahul Chand ,
I trust you are doing great.
Please find the updated client script for the same.

function myFunction() {
    var number = document.getElementById('number').value;
    var paymentId = document.getElementById('paymentId').value;
    var PayerID = document.getElementById('PayerID').value;
    
    var ga = new GlideAjax('global.PaypalAPI'); // Ensure 'global.PaypalAPI' matches the API name of your Script Include
    
    ga.addParam('sysparm_name', 'acceptPayment');
    ga.addParam('sysparm_payment_id', paymentId);
    ga.addParam('sysparm_payer_id', PayerID);
    ga.addParam('sysparm_record_number', number);
    
    ga.getXML(myCallBack);
}

function myCallBack(response) {
    var redirectUrl = response.responseXML.documentElement.getAttribute('answer');
    if (redirectUrl) {
        top.window.location.href = redirectUrl;
    } else {
        console.error("No redirect URL received from the server.");
    }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi