Multiple function script include not working

Aimee Smieth
Giga Contributor

Hi all,

I'm hoping someone can point me in the right direction with this. Bearing in mind I'm new(ish) to scripting, I'm having trouble making a script include work as soon as I put more than one function in it. E.g below:

 

var esgUtils2 = Class.create();
esgUtils2.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

   getCozInfo: function () {

       
        var model = this.getParameter('sysparm_model'); 
       var eMod = new GlideRecord('x_ka2_esg1_esg_core');
 eMod.addQuery('model', model);
 eMod.query();
 if (eMod.next()){
 
    var newmodel = this.getParameter('sysparm_newmodel'); 
    var nMod = new GlideRecord('x_ka2_esg1_esg_core');
 nMod.addQuery('model', newmodel);
 nMod.query();
 if (nMod.next()){
  
               var results = {
  "CurrEmPerAsset": eMod.getValue("asset_emissions"),       
  "CurrCozYears": eMod.getValue("asset_life_in_years"),
  "NewEmPerAsset": nMod.getValue("asset_emissions"),
  "NewCozYears": nMod.getValue("asset_life_in_years"),
  "RecEm": eMod.getValue("asset_recycling_emissions"),
  "CombEm": eMod.getValue("asset_emission_total"),
  "AssetKg": eMod.getValue("asset_emission_kg"),
  "RecKg": eMod.getValue("asset_recycling_emission_kg"),
  "CombKg": eMod.getValue("asset_emission_total_kg"),
  "AveWt": eMod.getValue("kg_average_weight"),
                };
   
         return JSON.stringify(results);        
    }  
     }
        } 
    getModelAm: function () {
  var count = 0;
        var model = this.getParameter('sysparm_model'); 
       var cMod = new GlideRecord('alm_hardware');
 cMod.addQuery('model', model);
 cMod.query();
 while (cMod.next()){
 count += 1;
 }
                var results = {
                    "amount": count,    
                };
                return JSON.stringify(results);
    }   

 // type: 'esgUtils2'
 });

 

If I put them in separate script includes they work fine. I'm hoping I'm missing something really simple!! Any pointers please?

 

Thank you!

5 REPLIES 5

Aman Kumar S
Kilo Patron

After ending the function you need to add a comma at the end separating two functions:

 
         return JSON.stringify(results);        
    }  
     }
        },


    getModelAm: function () {

///your statements

   return JSON.stringify(results);
    },   

  type: 'esgUtils2'// Remove the comments

Best Regards
Aman Kumar

@Aimee Smieth 

Is your issue resolved?

If yes, feel free to mark correct so it ends up in resolved queue.

Best Regards
Aman Kumar

@Aimee Smieth 

Is your issue resolved?


Feel free to mark correct, if your issue has been resolved, so it ends up in solved queue.

Will be helpful for others looking for the similar query.

Best Regards
Aman Kumar

-O-
Kilo Patron
Kilo Patron

In SN when you are defining Script Includes, you are creating an object using literal notation and assigning that object to the prototype of the Script Include "class". Thus the definition is equivalent to:

function getCozInfo () {
	// ...
}

function getModelAm () {
	// ...
}

var esgUtils2Prototype = {
	'getCozInfo': getCozInfo,
	'getModelAm': getModelAm,
}

var esgUtils2 = Class.create();

esgUtils2.prototype = Object.extendsObject(global.AbstractAjaxProcessor, esgUtils2Prototype);

As you can see members/properties (getCozInfo, getModelAm) of the prototype (esgUtils2Prototype) are separated with a comma. In your script those are not. In fact the script editor of SN should have told you that. Cause what you wrote is just the in-lining of all of the above. So your Script Include should have been:

var esgUtils2 = Class.create();

esgUtils2.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

	'getCozInfo': function () { // Note that you are NOT defining a function here, you are defining a member that points to - in this case - anonymous inline function
		// ...
	}, // Member separator added

	'getModelAm': function () {
		// ...
	}, // Member separator added

	 'type': 'esgUtils2', // Member separator can be added to the last member too.

});