How to set the value on a decimal field?

User177031
Kilo Guru

Hi All,

I need your help with the code of a fix script on below requirement.

For all the unexported time cards (u_export= false) on the time card table, if the minutes are not: 0,0.25,0.5,0.75 it should change it to the closest of those values.

Conditions are....

u_export= false &

1. If the provided time is equal or above 0.15 then it should set the value to 0.25 and if it is less that 0.15 then it should set the value to 0

2. If the provided time is equal or above 0.37 then it should set the value to 0.50 and if it is less that 0.37 then it should set the value to 0.25

3. If the provided time is equal or above 0.62 then it should set the value to 0.75 and if it is less that 0.62 then it should set the value to 0.50

4. If the provided time is equal or above 0.87 then it should set the value to 1.0 and if it is less that 0.87 then it should set the value to 0.75

Example:
Monday (u_monday)
8 -> No modification
Tuesday (u_tuesday)
5.25 -> No modification
Wednesday (u_wednesday)
7.11 ->becomes 7
Thursday (u_thursday)
7.18 ->becomes 7.25
Friday (u_friday)
2.70 ->becomes 2.75
Saturday (u_saturday)
2.60 ->becomes 2.50
Sunday (u_sunday)
9.74 ->becomes 9.75

 

Note - All are decimal fields.

- Thank you.

1 ACCEPTED SOLUTION

Paul Kunze
Tera Guru

Hi,

I am assuming that your u_export is a manually added field you want to filter by. So what you can do is to create a GlideRecord that queries all "u_export=false" records and then update all fields according to your requirements.

This script should work for you:

var days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
var values = [];

var grTimeCard = new GlideRecord('time_card');
grTimeCard.addQuery('u_export', false);
grTimeCard.query();
while(grTimeCard.next()){                                 // for every time card
	days.forEach(function(day, index){
		values[index] = grTimeCard.getValue(day);         // get the value for every day
	});
	days.forEach(function(day, index){                    // recalculate the value
		var numberSplitted = parseFloat(values[index]).toFixed(2).split('.');
		var predecimal = numberSplitted[0];
		var decimals =   numberSplitted[1];
		values[index] = decimals < 15 ? predecimal
					  : decimals < 37 ? predecimal + '.' + 25
					  : decimals < 62 ? predecimal + '.' + 50
					  : decimals < 87 ? predecimal + '.' + 75
					  : parseFloat(predecimal) + 1;
	});
	days.forEach(function(day, index){
		grTimeCard.setValue(days[index], values[index]);  // save the new value
		grTimeCard.update();
	});
}

View solution in original post

14 REPLIES 14

Hi, you are right. I updated one line of code, now it will work as expected.

Manas Kandekar
Kilo Guru

1. If the provided time is equal or above 0.15 then it should set the value to 0.25 and if it is less that 0.15 then it should set the value to 0

2. If the provided time is equal or above 0.37 then it should set the value to 0.50 and if it is less that 0.37 then it should set the value to 0.25

3. If the provided time is equal or above 0.62 then it should set the value to 0.75 and if it is less that 0.62 then it should set the value to 0.50

4. If the provided time is equal or above 0.87 then it should set the value to 1.0 and if it is less that 0.87 then it should set the value to 0.75

 
var data = '2.50';

var spl = data.split('.');
var afterPoint = parseInt(spl[1]);

if(afterPoint != 25 & afterPoint != 50 & afterPoint != 75)
{
makeRound(spl,afterPoint);
}

function makeRound(spl,afterPoint)
{
var beforePoint = parseInt(spl[0])

if(afterPoint >0 && afterPoint < 15 )
{
afterPoint=0;
}
elseif(afterPoint >= 15 && afterPoint < 37 )
{
afterPoint=25;
}
elseif(afterPoint >= 37 && afterPoint < 62)
{
afterPoint=50;
}
elseif(afterPoint >=62 && afterPoint < 87)
{
afterPoint=75;
}
elseif(afterPoint >=87 && afterPoint < 99)
{
afterPoint = 0;
spl[0] = beforePoint+1;
}
 
spl[1] = afterPoint;

}
var finalVal = parseFloat(spl[0]+'.'+spl[1]) // this is your result

alert(finalVal );

Hi Manas,

where are we calling the time_card table and the time card records with u_export = false?

- Thank you.

Hi Sai,

I already shared the complete code with you. Kindly check in the replies.

var gr = new GlideRecord("time_card");
gr.addQuery("u_export",false);
gr.query();
while(gr.next()) {
gr.monday = updateValue(gr.getValue("monday"));
gr.tuesday = updateValue(gr.getValue("tuesday"));
gr.wednesday = updateValue(gr.getValue("wednesday"));
gr.thursday = updateValue(gr.getValue("thursday"));
gr.friday = updateValue(gr.getValue("friday"));
gr.saturday = updateValue(gr.getValue("saturday"));
gr.sunday = updateValue(gr.getValue("sunday"));
gr.update();
}

function updateValue(data)

{

var spl = data.split('.');
var afterPoint = parseInt(spl[1]);
var beforePoint = parseInt(spl[0])

 

if(afterPoint != 25 & afterPoint != 50 & afterPoint != 75)
{
 
 
if(afterPoint >0 && afterPoint < 15 )
{
afterPoint=0;
}
elseif(afterPoint >= 15 && afterPoint < 37 )
{
afterPoint=25;
}
elseif(afterPoint >= 37 && afterPoint < 62)
{
afterPoint=50;
}
elseif(afterPoint >=62 && afterPoint < 87)
{
afterPoint=75;
}
elseif(afterPoint >=87 && afterPoint < 99)
{
afterPoint = 0;
spl[0] = beforePoint+1;
}
 
spl[1] = afterPoint;

}
 return parseFloat(spl[0]+'.'+spl[1]);
 }