The CreatorCon Call for Content is officially open! Get started here.

Issues calling Script Include from Client Sciprt

AishwaryaRaja
Tera Contributor

Hi All,

 

I am trying to write a Client Script to copy the benefit plan name same as the demand name. for this, using a client script and a script include (to get the demand details).

The Client Script is unable to call the Script include for some reasons which i am unable to understand.

 

The Script Include is created with option 'Client Callable' enabled.

This is assessable from 'All application scope' and there is access.

Can someone spot the issue here?

 

Client Script:

 

function onLoad() {
    
if (!g_form.isNewRecord() || g_form.getValue('benefit_type') != 'non_monetary')
return;
{
    //Call script include
    var gaTask = new GlideAjax('CarbonBenefitNameAjax'); //Scriptinclude
    gaTask.addParam('sysparm_name', 'getName'); //Method
    gaTask.addParam('sysparm_initId', g_form.getValue('benefit_plan.task')); //Parameters
 
alert( g_form.getValue('benefit_plan.task')); // This is returning the benefit plan sys ID
alert(sysparm_initId); // This is not returning the benefit plan sys ID
 
gaTask.getXMLAnswer(function(response) {
                g_form.setValue('name', response);
});
}
}
 
Script Include:
 
var CarbonBenefitNameAjax = Class.create();
CarbonBenefitNameAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getName: function() 
{
        var taskSysId = 'DMND0047713'; /*this.getParameter('sysparm_initId');*/
        var grtask = new GlideRecord('dmn_demand');
        grtask.get(taskSysId);
        //return grtask.short_description;
return 'Hello from SI';
    },
    type: 'CarbonBenefitNameAjax'
});
 
Your help is greatly appreciated. Thanks.
 
- Aish
3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

There's likely not an error calling the Script Include, rather the execution/completion of it.  You can confirm this by adding alerts to the client script and gs.info or gs.addInfoMessage lines to the SI to confirm that it is running, the value that is passed in from the client, and how far the script gets.  In this case, sysparm_initId and taskSysId are likely unknown/null. I don't think g_form.getValue('benefit_plan.task')) is valid.  If the alert is returning the correct sys_id, then OK. In the Script Include, the alternate assignment of taskSysId is invalid as you are supplying the Number field from a Demand, not the sys_id field.  The formatting/syntax of the SI is also off.  You always want to return something to the client.  I would recommend:

var CarbonBenefitNameAjax = Class.create();
CarbonBenefitNameAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getName: function() {
        gs.addInfoMessage('SI running');
        var answer = '';
        var taskSysId = this.getParameter('sysparm_initId');
        gs.addInfoMessage('benefit plan sys_id = ' + taskSysId;
        var grtask = new GlideRecord('dmn_demand');
        if (grtask.get(taskSysId) {
            //answer = grtask.short_description;
            answer = 'Hello from SI';
        }
        return answer;
    },
    type: 'CarbonBenefitNameAjax'
});

   

Thanks a lot for the suggestion. 

There was an issue with the name of the function used. -getName.

Changing it to something else, worked!

That makes sense.  I've seen a few people have trouble with this.  It seems like they should be able to warn in the script editor when a reserved word is used as a function name.