gliderecord functions within a scoped application

jamesmcwhinney
Giga Guru

I am building a forms application wherein there is one application for the base forms table, and then additional applications to handle each individual type of form.

All forms extend the base table.

I am trying to create a UI action which will allow users to create a new instance of a form using the current instance, and I need it to work without knowing the actual field names in the given table.

I have created the below Script in the base application, but am running into a strange error which I can't seem to get around.

Function getFields is not allowed in scope x_eqp_forms

My code is below.

Could anyone tell me why I am getting this error and how to get around it??

Thnks!

UI Action

//query the rows we want to copy

var currentForm = new GlideRecord(current.sys_class_name);
if(currentForm.get(current.sys_id)){
     
var newForm = new GlideRecord(current.sys_class_name);
newForm.initialize();



//get all the fields for this record
      var currentFields = currentForm.getFields();
      for (var i = 0; i < currentFields.size(); i++) {
 
              var currentField = currentFields.get(i);
  var currentFieldName = currentField.getName();
  gs.log("FieldName:" + currentFieldName);
              //make sure we don't copy the sys_id
              if(currentFieldName != 'sys_id')
              {
    newForm.setValue(currentFieldName, currentForm.getValue(currentFieldName));
              }
      }



      var newSysId = newForm.insert();
gs.addInfoMessage(newForm.sys_class_name.getDisplayValue() + ' ' + newForm.number + ' has been created using ' + currentForm.number + '. Please review the new form for accuracy before submitting for Approval.');
action.setRedirectURL(newForm);
}

1 ACCEPTED SOLUTION

drjohnchun
Tera Guru

Hi James - below is a possible workaround:



var newForm = new GlideRecord(current.sys_class_name);


var currentFields = [];


newForm.initialize();


for (var f in newForm) currentFields.push(f);



currentFields will then have all field names from newForm.



PS: going one step further, I think you should be able to replace your for loop with the for...in from above.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

View solution in original post

4 REPLIES 4

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

All the Scoped and Global API's are documented on the developer portal.   Here are the available GlideRecord API's for scoped applications:


https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&type=server&scoped=true&to=class__scope...



You will notice that getFields() is not available in scoped applications.   I cannot tell you why unfortunately but that is the reason for your error message.   Unfortunately I don't know of a work around either.   I would suggest you follow up with support to see if they know of a work around and to possibly open an enhancement to get that API call exposed if possible.


drjohnchun
Tera Guru

Hi James - below is a possible workaround:



var newForm = new GlideRecord(current.sys_class_name);


var currentFields = [];


newForm.initialize();


for (var f in newForm) currentFields.push(f);



currentFields will then have all field names from newForm.



PS: going one step further, I think you should be able to replace your for loop with the for...in from above.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

Thank You !!!!!!!!!!!!!



That was exactly what I needed!!




Here is the revised script:



//Get Current Form
var currentForm = new GlideRecord(current.sys_class_name);
if(currentForm.get(current.sys_id)){
var newForm = new GlideRecord(current.sys_class_name);
newForm.initialize();


//Get Field Names for Current Form
var currentFieldNames = [];
for (var f in currentForm) currentFieldNames.push(f);

//Copy current form's values into a new form
      for (var i = 0; i < currentFieldNames.length; i++) {
  //make sure we don't copy the sys_id etc
              if((currentFieldNames[i] != 'sys_id')&&(currentFieldNames[i] != 'number')&&(currentFieldNames[i] != 'sys_mod_count')&&(currentFieldNames[i] != 'sys_tags')&&(currentFieldNames[i] != 'approval'))
              {
    newForm.setValue(currentFieldNames[i], currentForm.getValue(currentFieldNames[i]));
              }
      }

//Insert the new form
var newSysId = newForm.insert();
gs.addInfoMessage(newForm.sys_class_name.getDisplayValue() + ' ' + newForm.number + ' has been created using ' + currentForm.number + '. Please review the new eForm for accuracy before submitting for Approval.');
action.setRedirectURL(newForm);
}


and if you exclude 'sys_class_type' you can copy all content of one record from another table. The following works on an existing record:




copyAllParentVariables: function(child)


{


var scopeName=gs.getCurrentScopeName();



var parent= new GlideRecord(scopeName+'_request');   // this is the parent table. your child record needs to be precreated but can be in any table.


parent.addQuery('serial_number', child.serial_number.trim());


parent.query();


parent.next();



if(parent.get(parent.sys_id)){



var currentFieldNames = [];


for (var f in parent) currentFieldNames.push(f);



//Copy current form's values into a new form


for (var i = 0; i < currentFieldNames.length; i++) {



if(   (currentFieldNames[i] != 'sys_class_name') && (currentFieldNames[i] != 'task_type') && (currentFieldNames[i] != 'sys_id')&&(currentFieldNames[i] != 'serial_number') && (currentFieldNames[i] != 'number')&&(currentFieldNames[i] != 'sys_mod_count')&&(currentFieldNames[i] != 'sys_tags')&&(currentFieldNames[i] != 'approval'))


{


child.setValue(currentFieldNames[i], parent.getValue(currentFieldNames[i]));


}


}




child.update();


}


},