Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get all field values and store it in a string field

Pravallika9
Tera Expert

Hi,

As part of some requirement I want to store the complete form details first in a different table field as a backup.

eg. for suppose we have a incident ticket and it is in work in progress

Once it reached that state I want to have a backup of all the field values stored in some other field.

var grINC = new GlideRecord('incident');
grINC.query();
grINC.next();
var fields = grINC.getFields();
for (var i = 0; i < fields.size(); i++) {
  var glideElement = fields.get(i);
  if (glideElement.hasValue()) {
var obj = {};
    // here I am confused how exactly i can bring all the values in the array.
  }
}
gs.print('');
1 ACCEPTED SOLUTION

Voona Rohila
Mega Patron
Mega Patron

Hi Pravalika

Try this:

var grINC = new GlideRecord('incident');
grINC.addQuery('sys_id','incident_sysid_here'); //map record sysid here.
grINC.query();
if(grINC.next())
{
    var arr={};
var elements = grINC.getElements();
       for (var i=0; i<elements.size(); i++) {


               var element = elements.get(i);

                arr[element.getLabel()] = grINC.getValue(element.getName());
               //gs.info("Element label is {0}, name is {1}, value is {2}", element.getLabel(), element.getName(), grINC.getValue(element.getName()));


       }
       gs.print(JSON.stringify(arr));

}

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

View solution in original post

6 REPLIES 6

Hi,

Thanks for the reply it worked,

Additional requirement can we remap all the data which we got into the form after particular state.

@Voona Rohila 

Hi ,

 When i am using the script which is provided above i am getting all the fields in the table . What i required is when i open the incident form and i have 10 fields and  i need only that fields. not  all the fileds.