Via an uploadsheet Trying to update the only 2 filelds other fields should not be updated

jhansi28
Tera Contributor

I am trying to upload an excel sheet with upload action it should allow to update only 2 fields location id and location name when discovery is filled in.  and all other fields should not get updated.Below is the onbefore transform script used .how can we achieve to update only 2 location fields in below script?

 

 

 

function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

if (action == 'update') {
target.u_most_recent_discovery = new GlideDate();
var grRecord = new GlideRecord('cmn_location');
if (grRecord.get(target.getUniqueValue())) {
var discoverySource = grRecord.getValue("u_discovery_source");
// In case Discovery source is not matching , then update is not allowed
if (discoverySource && discoverySource != source.u_discovery_source) {
this.errorOccurred = true;
source.u_custom_errors += '\nUpdate Error - Discovery Source is not matching';
}

Below is the script. where we need to update location id and location name should not allowed to update other fields
Direct fields mapings are already available in transform maps(source to target)
how can we achieve to update only 2 location fields in below script?

/ For update via Auto load (not via schedule import), checking & restricting updating location records created by scheduled jobs (admin can override)
if (discoverySource && this.initiatorInfo.isRITM && !(this.initiatorInfo.isAdminAccount)) {
var createdBy = grRecord.getValue("sys_created_by").toString();
var sourceIDs = this.initiatorInfo.sourceIDs;
if (sourceIDs && sourceIDs != '') {
for (var i in sourceIDs) {
if (createdBy.contains(sourceIDs[i])) {
this.errorOccurred = true;
source.u_custom_errors += '\nUpdate Error - location can be updated only by Record creator';
break;
}
}
}
}
}

 

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@jhansi28 

you can use IF condition and set target field using this syntax

target.locationId = source.<fieldName>;

target.locationName = source.<fieldName>;

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

I tried adding if condition it is allowing to update other fields like zipcode,state,city,country etc.it should not happen.

    if (discoverySource && this.initiatorInfo.isRITM && !(this.initiatorInfo.isAdminAccount)) {
                var createdBy = grRecord.getValue("sys_created_by").toString();
                var zipcode = target.zip;
                var sourceIDs = this.initiatorInfo.sourceIDs;
                if (sourceIDs && sourceIDs != '') {
                    for (var i in sourceIDs) {
//Checking  location id and location is not emapty in the uploadsheett(source)
                        if (source.u_location_id != '' || source.u_location_name != '') {
                            target.u_location_id = source.u_location_id;
                            target.u_location_name = source.u_location_name;
                            break;
                            
                        } else {
                            if (createdBy.contains(sourceIDs[i])) {
                                this.errorOccurred = true;
                                source.u_custom_errors += '\nUpdate Error - location can be updated only by Record creator';
                                break;

                            }
                        }


                    }
                }

            }

@jhansi28 

try this

                        if (source.u_location_id != '' && source.u_location_name != '') {
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ademir Amaral1
Kilo Sage

Hi @jhansi28 

 

Try this:

function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

  if (action == 'update') {
    target.u_most_recent_discovery = new GlideDate();
    var grRecord = new GlideRecord('cmn_location');
    if (grRecord.get(target.getUniqueValue())) {
      var discoverySource = grRecord.getValue("u_discovery_source");
      // In case Discovery source is not matching , then update is not allowed
      if (discoverySource && discoverySource != source.u_discovery_source) {
        this.errorOccurred = true;
        source.u_custom_errors += '\nUpdate Error - Discovery Source is not matching';
      } else {
        // Only update location id and location name fields
        if (source.u_location_id) {
          target.u_location_id = source.u_location_id;
        }
        if (source.u_location_name) {
          target.u_location_name = source.u_location_name;
        }
      }
    }
  }
}

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.