Hi Everyone , Kindly assist me to proceed further. Given below the code which i tried in background script and the code which i used in field map script. Code is working in background but not in field map script.

Anup Ghormode
Tera Contributor

Background script -

var macAddress = "D4:3B:04:EB:87:00";
var array = macAddress.split("|");
macAddress = array[0];
gs.info("inside 1 if : " + array[0]);
// return array[0];
if (macAddress) {
gs.info("inside 1 if : "+macAddress);
var deviceMac = '';
deviceMac = macAddress.toUpperCase().toString();
if (deviceMac.length >= 3 && deviceMac.length <= 17) {
gs.info("inside 2 if : "+ deviceMac);
deviceMac = deviceMac.replace(/\W/ig, '');
deviceMac = deviceMac.replace(/(.{2})/g, "$1-");
deviceMac = deviceMac.slice(0, -1);
gs.info("devicemac updated : "+ deviceMac );
}
}

**************************************************************************************************************

Field Map Script :-

answer = (function transformEntry(source) {

var macAddress = source.u__device_mac_address.toUpperCase().toString();
var array = macAddress.split("|");
macAddress = array[0];
//gs.info("inside 1 if : " + array[0]);
// return array[0];
if (macAddress) {
// gs.info("inside 1 if : " + macAddress);
// Input string should be one less in lenght than a qualified MAC address (17)
if (macAddress.length >= 3 && macAddress.length <= 17) {
// gs.info("inside 2 if : " + deviceMac);
// Remove all but alphanumeric characters
macAddress = macAddress.replace(/\W/ig, '');
// Append a colon after every two characters
macAddress = macAddress.replace(/(.{2})/g, "$1-");
macAddress = macAddress.slice(0, -1);
// gs.info("devicemac updated : " + deviceMac);
return macAddress;
}
}
else
return '';

})(source);

 

Please help me and assist where do i need to make changes in my field map script

1 ACCEPTED SOLUTION

Nagaraj Tofin5
Tera Expert

Hi Anup,

Please try to execute below script.

Script:

 

answer = (function transformEntry(source) {

var macAddress = source.u__device_mac_address.toString().toUpperCase();
var array = macAddress.split("|");
macAddress = array[0];
if (macAddress) {
var deviceMac = '';
deviceMac = macAddress.toString().toUpperCase();
if (deviceMac.length >= 3 && deviceMac.length <= 17) {
deviceMac = deviceMac.replace(/\W/ig, '');
deviceMac = deviceMac.replace(/(.{2})/g, "$1-");
deviceMac = deviceMac.slice(0, -1);
return deviceMac.toString();
}
} else {
return '';
}
})(source);

 

Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions

Regards,
Nagaraj Tofin

View solution in original post

4 REPLIES 4

Anil Lande
Kilo Patron

Hi,

I guess there was type mistake on below line:

var macAddress = source.u__device_mac_address.toUpperCase().toString();

 

Can you please try below:

answer = (function transformEntry(source) {

var macAddress = source.u_device_mac_address.toUpperCase().toString();  // I guess youdid typo on this line, it was source.u__device_mac_address
var array = macAddress.split("|");
macAddress = array[0];
//gs.info("inside 1 if : " + array[0]);
// return array[0];
if (macAddress) {
// gs.info("inside 1 if : " + macAddress);
// Input string should be one less in lenght than a qualified MAC address (17)
if (macAddress.length >= 3 && macAddress.length <= 17) {
// gs.info("inside 2 if : " + deviceMac);
// Remove all but alphanumeric characters
macAddress = macAddress.replace(/\W/ig, '');
// Append a colon after every two characters
macAddress = macAddress.replace(/(.{2})/g, "$1-");
macAddress = macAddress.slice(0, -1);
// gs.info("devicemac updated : " + deviceMac);
return macAddress;
}
}
else{
return '';
}
})(source);

 

Also can you please share what logs you are getting by uncommenting logs, where your code is reaching?

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi Anil,

Thanks for your response. The field source.u__device_mac_address is correct. The extra _ is present in source field still i am not getting the expected output.

Thanks !

Nagaraj Tofin5
Tera Expert

Hi Anup,

Please try to execute below script.

Script:

 

answer = (function transformEntry(source) {

var macAddress = source.u__device_mac_address.toString().toUpperCase();
var array = macAddress.split("|");
macAddress = array[0];
if (macAddress) {
var deviceMac = '';
deviceMac = macAddress.toString().toUpperCase();
if (deviceMac.length >= 3 && deviceMac.length <= 17) {
deviceMac = deviceMac.replace(/\W/ig, '');
deviceMac = deviceMac.replace(/(.{2})/g, "$1-");
deviceMac = deviceMac.slice(0, -1);
return deviceMac.toString();
}
} else {
return '';
}
})(source);

 

Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions

Regards,
Nagaraj Tofin

@Nagaraj Tofin wow man great ! It's working now. Thanks a lot !!