Want to update the records in alm_asset table not to restrict when a record producer is submitted.

Community Alums
Not applicable

Hi All, 

 

we are having a record producer to insert the records in alm_asset table. 

on the form there is a serial number filed. if we submit the form with the same serial number we need to update the record instead of restricting to insert. 

 

need suggestions. 

we tried with BR as below.

(function executeRule(current, previous /*null when async*/) {
 
var grDupCheck = new GlideRecord('alm_asset');
 
  grDupCheck.addQuery('serial_number', current.serial_number).addOrCondition('asset_tag',current.asset_tag);
 
  grDupCheck.query();
 
  if (grDupCheck.next()) {
 
  gs.addErrorMessage("Serial number or asset tag already exists");
 
  current.setAbortAction(true);
 
  }
})(current, previous);
 
 
Thanks.
8 REPLIES 8

Tony Chatfield1
Kilo Patron

Hi, I think you need to utilize your record producer script, rather than using a BR.
Run a suitable glidequery in your producers script.
If a record match is found, update fields of the existing record as appropriate, then abort the record producer insert.
If a match is not found continue with the record producer insert.

I think you already have most of your code in your BR and just need to relocate it to the producer and tweak it a touch.

Community Alums
Not applicable

@Tony Chatfield1 

 

Any suggested script for reference.

 

 

Thanks.

As indicated above you already have most of your code, but here are some options for your record producer script

var grDupCheck = new GlideRecord('alm_asset');
 
 grDupCheck.addQuery('serial_number', producerSerialNumberField).addOrCondition('asset_tag',producerAssetTagField);
 
 grDupCheck.query();
 
 if (grDupCheck.next()) {
	//update fields for existing record if required
	//If you only want to update only if specific conditions are met, use a variable to decide when to update
	var makeAnUpdate = false;
	
	//Example update Serial number if different, but asset tag is same
	if(producerSerialNumberField != grDupCheck.serial_number) {
		grDupCheck.serial_number = producerSerialNumberField;
		makeAnUpdate = true;
	}
 
	//Otherwise update any field on the existing record, but 'grDupCheck.update()' will need to be outside of the makeAnUpdate conditon check
	grDupCheck.someField2 = someProducerValue2;
	
	if(makeAnUpdate == true) {
		grDupCheck.update();
	}
 
	gs.addErrorMessage("Serial number or asset tag already exists, so new record insert aborted");
 
  current.setAbortAction(true);
  } else {
   //Run your existing record producer script so that a new record is created and populated
  }

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

you want to update via record producer?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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