business rule error - Help from a helpful community member required ;o)

CandyDee
Kilo Sage

I was hoping someone could be my 2nd pair of eyes and point out my error. Ill provide the code to business rule below, when the business rule triggers its providing this error in the log. 

 

'Unable to retrieve room type record for booking undefined' for the love of me I cant figure it out.

 

The Business rule is as follows- Runs before on insert/update

 

(function executeRule(current, previous) {
// Get the room type record corresponding to the booking record
var roomType = new GlideRecord('sn_hbm_room_type');
if (roomType.get(current.room_type.name)) {
// Get the price and number of nights from the booking record
var price = parseFloat(roomType.getValue('price'));
var numberOfNights = parseInt(current.getValue('number_of_nights'));

// Calculate the total cost
var totalCost = price * numberOfNights;

// Set the value of the total cost field on the current record
current.total_cost = totalCost;

// Log the total cost calculation
gs.log('Total cost for booking ' + current.number + ' is ' + totalCost);
} else {
gs.log('Unable to retrieve room type record for booking ' + current.number);
}
})(current, previous);

 

The room type table has a field named 'name' which is a name field and field called 'Price which is of a pricetype'. Im trying to capture that price of the room to then multiple it by the number of nights booked. driving me insane for the past few days. Thanks for looking all. 

3 REPLIES 3

Bert_c1
Kilo Patron

hi,

 

what specific table is the business rule defined on, it has a reference field named 'room_type', and where does the 'sn_hbm_room_type' table come from? Knowing this will help in some here understand what you're trying to do. 

Rahul Kumar17
Tera Guru

Hi,

 

Based on the error message you provided, it seems like the issue is with the "current.room_type.name" value. It might be undefined or not set properly, which is causing the GlideRecord query to fail.

To fix this issue, you can check if the "current.room_type" field is not null and is valid before querying the "sn_hbm_room_type" table. You can also add some error handling to log any errors that might occur during the query.

Here's the updated business rule code:

 

(function executeRule(current, previous) {
if (current.room_type && current.room_type.name) {
// Get the room type record corresponding to the booking record
var roomType = new GlideRecord('sn_hbm_room_type');
if (roomType.get('name', current.room_type.name)) {
// Get the price and number of nights from the booking record
var price = parseFloat(roomType.getValue('price'));
var numberOfNights = parseInt(current.getValue('number_of_nights'));

// Calculate the total cost
var totalCost = price * numberOfNights;

// Set the value of the total cost field on the current record
current.total_cost = totalCost;

// Log the total cost calculation
gs.log('Total cost for booking ' + current.number + ' is ' + totalCost);
} else {
gs.log('Unable to retrieve room type record for booking ' + current.number);
}
} else {
gs.log('Room type is not set or invalid for booking ' + current.number);
}
})(current, previous);

 

Thanks,

Rahul Kumar

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

Please accept the solution and closed the thread. If it's working for you.

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar