Issue with before Business Rule If condition

MohanambalN
Tera Contributor
In alm_asset table I have created the below Business Rule(OnBefore) which runs on insert and update.
 
But it always going to else part and updating Serial number in the Device Name.
If it is communication Device, then it should update the IMEI number as device name. 
 
(function executeRule(current, previous /*null when async*/)
{
   
var communicationDevice = gs.getProperty("itsm.asset.model_category.communication_device.sys_id");
var modelCategory = current.getValue('model_category');  //(category sys ID)
 

 

gs.log("Model Category :"+modelCategory);
gs.log("Model Cqategory1 :"+communicationDevice);

 

if (modelCategory == communicationDevice)
{
    current.setValue('u_device_name', current.u_imei_number);
}
else
{
    current.setValue('u_device_name', current.serial_number);
}

 

})(current, previous);
 
Please someone help me to fix the issue.
 
Thanks
11 REPLIES 11

No luck. 

 

Prathamesh G
Kilo Sage
Kilo Sage

Hello @MohanambalN ,

 

The Model Category is the 'Reference' typed field. So the 'current.getValue('model_category')' will return with the SysID of the model category.

 

So rather than this, use the 'current.getDisplayValue('model_category')' to get the Display value of the Model Category. And in the if condition use the Single or Double quotes like below :

 

if (modelCategory == 'communicationDevice')

 

 

Use the below full code:

(function executeRule(current, previous /*null when async*/)
{
   
var communicationDevice = gs.getProperty("itsm.asset.model_category.communication_device.sys_id");
var modelCategory = current.getDisplayValue('model_category');  //(Display Value of the Model Category)
 
 

gs.log("Model Category :"+modelCategory);
gs.log("Model Cqategory1 :"+communicationDevice);
 

if (modelCategory == 'communicationDevice') //Use the exact Display name of the Model category
{
    current.setValue('u_device_name', current.u_imei_number);
}
else
{
    current.setValue('u_device_name', current.serial_number);
}
 

})(current, previous);

 

Let me know if you have any further questions or issues.

 

And if the above solution resolve your issue, Please mark the solution as 'Accepted Solution' also mark it as Helpful.

 

Thank You.

Mark Manders
Mega Patron

First: with 'setValue' you also need an .update(); or is this just a part of the code?

Then: what do your logs say? Do you get the same sys_id returned? in your logs? Also: what is the reason for getting the sys_id through a property? Why not 'if(current.model_category = 'sys_id'){'

Also: it runs before insert/update, so with an insert you need to be sure the system already knows the model category.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

MohanambalN
Tera Contributor

As a best practice, I don't want to user any hard coded 'Sys_ID' in script. Hence, created the property and stored the Sys ID in property.

 

Noticed that if we directly use the 'sys_id' in script it is working as expected.

Sid_Takali
Kilo Patron
Kilo Patron

Hi @MohanambalN  Try below code

(function executeRule(current, previous /*null when async*/) {
    var communicationDeviceSysId = gs.getProperty("itsm.asset.model_category.communication_device.sys_id");
    var modelCategory = current.getValue('model_category'); 
    gs.log("Model Category from record: " + modelCategory);
    gs.log("Communication Device Sys ID from property: " + communicationDeviceSysId);
    if (modelCategory && communicationDeviceSysId && modelCategory.trim() === communicationDeviceSysId.trim()) {
        gs.log("Updating device name with IMEI number.");
        current.setValue('u_device_name', current.u_imei_number);
    } else {
        gs.log("Updating device name with Serial number.");
        current.setValue('u_device_name', current.serial_number);
    }
})(current, previous);