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

Manas Kandekar
Kilo Guru
Hi
 
Find the below logic,
 
var data = '2.55';

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 < 25 && afterPoint > 0)
{
afterPoint=25;
}
elseif(afterPoint < 50 && afterPoint > 25)
{
afterPoint=50;
}
elseif(afterPoint < 75 && afterPoint > 51)
{
afterPoint=75;
}
elseif(afterPoint <= 99 && afterPoint > 75)
{
afterPoint = 0;
spl[0] = beforePoint+1;
}
spl[1] = afterPoint;

}
 
 
alert(parseFloat(spl[0]+'.'+spl[1]));
 
If my answer helped you in any way, please then mark it correct and helpful.

Kind regards,
Manas

Hi Manas,

Can you help me with the exact code as the code should run on time_card table with below conditions.

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

- Thank You.

User177031
Kilo Guru

@Ankur Bawiskar can you help me on this? Urgent...!

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();
	});
}

Hi Paul,

When I give the time as 0.89 then it is setting the value to 0. It should actually set to 1.

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

- Thank you.