Parse a Serial Number from a String

Nate10
Kilo Contributor

SCCM creates a serial number value for a CMDB CI that has the following format:


/12BDMQ2/CNCMK0089P011E/

It needs to be:

12BDMQ2

Using a business rule, can someone suggest a method to replace the first value with the extracted test between the  first and second '/'  ? 

 

(function executeRule(current, previous /*null when async*/) {

// Select out the string between the first two slash characters and discard the rest.

// read in the value of cmdb_ci_computer.serial serial number
var u_sccm_computer_sn = user.getValue("lob").split(',');

})(current, previous);

Thanks for any suggestions. 

 

 

 

1 ACCEPTED SOLUTION

HarshTimes
Tera Guru

YOu can try

var str =  "/12BDMQ2/CNCMK0089P011E/" ;

str.split("/");

var serial_number = str[1] ;

 

 

-Harsh

View solution in original post

8 REPLIES 8

Brent Sutton
Mega Sage

Hi Nate,

I'm going to assume that the name of your field, that contains your SCCM serial number, is "lob" and it is located on CMDB table that this business rule runs on ("lob" if what you used for your getValue on in your example code). I also assume that the business rule is running on a table extended from "cmdb_ci".

Try the following:

var sccmSerial = current.getValue("lob"); //getValue from field called "lob" i.e. field containing sccm serial number "/12BDMQ2/CNCMK0089P011E/"
var serialStr = sccmSerial.substring(1,sccmSerial.indexOf("/",1)); //use substring and indexOf to extact the data between the first two "/"

current.setValue("serial_number",serialStr); //set the serial field on the cmdb table.

Let me know how you get along.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information. 

HarshTimes
Tera Guru

YOu can try

var str =  "/12BDMQ2/CNCMK0089P011E/" ;

str.split("/");

var serial_number = str[1] ;

 

 

-Harsh

Nate10
Kilo Contributor

Here's what I've got so far. I'm still testing. I'll try Harsh's array code after I get the first one working.

 

I added some code to append the short description field so I don't totally blow away the rest of the SN string in case that data is needed somewhere else. 

 

(function executeRule(current, previous /*null when async*/) {
// Select out the string following the first slash and discard the rest.

var sccmSerial = current.getValue("serial_number"); //getValue from field called "lob" i.e. field containing sccm serial_number "/12BDMQ2/CNCMK0089P011E/"
var shrtdscr= current.getValue("short_description"); // Insert code to append the value of sccmSerial to some other field so we don't lose that information.
current.setValue("short_description",'shrtdscr'+' '+'sccmSerial'); //append the full serial number string to the short description field with space between.
var serialStr = sccmSerial.substring(1,sccmSerial.indexOf("/",1)); //use substring and indexOf to extact the data between the first two "/"
current.setValue("serial_number",serialStr); //set the serial field on the cmdb table.

})(current, previous);

 

 

Hi Nate,

You would be better off storing the SCCM serial number in its own field if you intend to keep it long term. I had to do that when monitor serial numbers were reported differently in SCCM when compared to the physical serial number.

For the example below I am going to use "u_flash_serial_number" as the field to store the SCCM serial number.

Config:

find_real_file.png

Code:

(function executeRule(current, previous /*null when async*/) {
	
	var sccmSerial = current.getValue("serial_number"); //getValue from serial_number field i.e. field containing sccm serial number "/12BDMQ2/CNCMK0089P011E/"
	current.setValue("u_flash_serial_number", sccmSerial); //set your other field to original sccm serial number. NOTE: you will need to create this field below running script
	
	/* If you need to use the short_description field then uncomment the following code and comment out the line above*/
	/*
	var shortDesc = current.getValue("short_description");
	if (shortDesc != "") {
		shortDesc = gs.getMessage("{0}: SCCM Serial: {1}",[shortDesc,sccmSerial]);
	}
	else {
		shortDesc = gs.getMessage("SCCM Serial: {0}",[sccmSerial]);
	}
	current.setValue("short_description", shortDesc); //set short_description.
	*/
	
	var serialStr = sccmSerial.substring(1,sccmSerial.indexOf("/",1)); //use substring and indexOf to extact the data between the first two "/"

	current.setValue("serial_number",serialStr); //set the serial field on the cmdb table.


})(current, previous);

 Let me know how you get along.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.