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.

What is the best way to copy fields between GlideRecords

ccorbett
Mega Contributor

Good Morning All,

I am working on a scenario where we are attempting to identify duplicate CIs and merge one with the other.

The approach I'm taking is to have each CI in its own GlideRecord and a list of fields I would like to not copy like so:

var ci1 = 'some sys_id';

var ci2 = 'another sys_id';

var excludeList = 'field1, field2, field3';

function copyRecords (ci1, ci2, excludeList) {

var copyFrom = new GlideRecord('cmdb_ci');

      copyFrom.get(manualCI);

      var copyFromFields = copyFrom.getFields();

var copyTo = new GlideRecord('cmdb_ci');

copyTo.get(manualCI);

var copyToFields = copyFrom.getFields();

for (var i = 0; i < copyFromFields.size(); i++) {

        var copyFromElement = copyFromFields.get(i);

     

        if (copyFromElement.hasValue() && exclFldsLst.indexOf(String(copyFromElement.getName())) < 0) {

                  Copying of fields from CopyFrom to CopyTo would occur here    

}

}

}

This correctly identifies the fields that I want to copy, but I am unsure how to go about accessing the CopyTo GlideRecord.

Would anyone have insight on how to do this?

Thanks for your time.

1 REPLY 1

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Not sure I 100% follow what you are after, but here is a mergeRecord() function that might do what you are requesting.   Basically you pass it a source ci ID, a duplicate CI ID, and a list of fields you don't want to copy.   It will then get the records, field list and copy the data from the duplicate to the source.   I haven't tested this but it should work.



function mergeRecords (sourceID, duplicateID, excludeList) {


      //Get source CI


      var sourceRec = new GlideRecord("cmdb_ci");


      sourceRec.get(sourceID);


      var sourceClassName = sourceRec.sys_class_name;


   


      // Now that we know which class of CI it is, get the record again with all the fields


      sourceRec = new GlideRecord(sourceClassName);


      sourceRec.get(sourceID);


   


      //Get duplicate CI


      var duplicateRec = new GlideRecord("cmdb_ci");


      duplicateRec.get(duplicateID);


      var dupClassName = duplicateRec.sys_class_name;


   


      // Now that we know which class of CI it is, get the record again with all the fields


      duplicateRec = new GlideRecord(dupClassName);


      duplicateRec.get(duplicateID);


   


      // Since we are merging duplicate into source, get a list of fields of the duplicate


      var recordUtil = new global.GlideRecordUtil();


      var dupFieldList = recordUtil.getFields(duplicateRec);


   


      if (excludeList) {


              // Loop through the excludeList and remove all those fields from the dupFieldList array


              excludeList = excludeList.toString().split(",");


              for (var e = 0; e < excludeList.length; e++) {


                      var fieldIndex = dupFieldList.indexOf(excludeList[e]);


                      if (fieldIndex > -1) {


                              dupFieldList.splice(fieldIndex, 1);


                      }


              }


      }


   


      // Now loop through fields and copy them to source


      for (var f = 0; e < dupFieldList.length; f++) {


              var fieldName = dupFieldList[f];


              if (sourceRec.isValidField(fieldName)) {


                      sourceRec[fieldName] = duplicateRec[fieldName];


              }


      }


}