- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2019 09:49 PM
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.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2019 01:29 AM
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();
});
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2019 10:58 PM
Hi, you can try this code. Did for 1 variable. You can follow same logic for others.
/*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 u_monday = "7.14";
var calc_output = "";
var str_decimal = u_monday.split(".");
if(str_decimal.length>1) {
val = checkValue(str_decimal[1]);
if(val == "0") {
u_monday = str_decimal[0];
} else if (val == "1") {
u_monday = parseInt(str_decimal[0])+parseInt(val);
} else {
u_monday = str_decimal[0].toString()+val;
}
}
gs.print("Final output is "+u_monday);
function checkValue(str) {
var val = parseInt(str);
if(val < 15) {
return 0;
} else if(val >=15 && val <=36) {
return ".25";
} else if(val >=37 && val <=61) {
return ".50";
} else if(val >=62 && val <=86) {
return ".75";
} else if(val >=87) {
return "1";
}
}
Mark the comment as a correct answer and also helpful once worked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2019 11:30 PM
Hi Asifnoor,
This fix script 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
Also I was bit confused between the variable u were calling as u_monday and the custom field label as u_monday. can you please show the difference on next reply.
- Thank you.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2019 01:33 AM
Hi Sai,
So you need to write a code to query the time_card table. I assume you want to query the existing records and update them.
Try the below code. if needed, change the field names as per your table.
var gr = new GlideRecord("time_card");
gr.addQuery("u_export",false); //check column names
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(str) {
var calc_output = str;
var str_decimal = str.split(".");
if(str_decimal.length>1) {
val = checkValue(str_decimal[1]);
if(val == "0") {
calc_output = str_decimal[0];
} else if (val == "1") {
calc_output = parseInt(str_decimal[0])+parseInt(val);
} else {
calc_output = str_decimal[0].toString()+val;
}
}
return calc_output;
}
function checkValue(str) {
if(str.length==1) {
str = str+"0"; //to take care of .5
}
var val = parseInt(str);
if(val < 15) {
return 0;
} else if(val >=15 && val <=36) {
return ".25";
} else if(val >=37 && val <=61) {
return ".50";
} else if(val >=62 && val <=86) {
return ".75";
} else if(val >=87) {
return "1";
}
}
Mark the comment as a correct answer and also helpful if it helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2019 02:24 AM
Hi Sai,
Use this code. This is working fine.