I want to find the difference between the two date time fields and need to update in other duration field

venkataci
Kilo Contributor

I want to find the difference between the two date time fields and need to update in other duration field.

 

find_real_file.png   Log in Time and Logoff time are Date/ Time fields and Total Log time is duration field.

 

I have written server side script for this to work calling a glide ajax call. But it is not calculating the difference.

 

Client script:

find_real_file.png

 

 

Script include is:

 

find_real_file.png

 

Please help me to resolve it.

 

 

Thanks in Advance.

40 REPLIES 40

I would recommend you put some debugging lines into your code so you can see how the script is progressing and what values are being returned by your variables. Coming back on here and just saying 'it doesn't work' isn't particularly helpful.

 

If you can say where your code is failing or inform us as to which variables aren't returning the expected values then we can provide more targeted assistance than just scatter gunning different versions of the code in the hope that one of them works.

 

Also, correct me if i'm wrong but the initial issue you were having is resolved, it would have made this thread more readable if you had closed it down as resolved and opened a new one for the duration issue.

venkataci
Kilo Contributor

Yes, i did some debugging by keeping some alerts to show values what they are getting before they perform subtraction..

 

there is some issue in it..will check on it and try again.

Can you please replace your client script and script include with the code given below.

Client Script

function onChange(control, oldValue, newValue, isLoading) {
var lgtym = g_form.getValue('u_log_time');//set this as login date
var brktym = g_form.getValue('u_total_break_time');//set this as logoff date
var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','durCalc');
ajax.addParam('sysparm_strt',lgtym);
ajax.addParam('sysparm_end',brktym);
ajax.getXMLWait();
var answer = ajax.getAnswer();
g_form.setValue('u_total_time', answer);
}

Script include
Name:AjaxDurCalc
Client callable:checked,client:checked

script:
var AjaxDurCalc = Class.create();
AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
durCalc: function() {
return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);
}
});

If the post help you out, mark it as correct and helpful.

Thanks.

I did with it also..but not showing the "u_total_time" value ....

Hi Venkat

Try this with business rule as on Display

 

(function executeRule(current, previous /*null when async*/) {

 

            getTimeDiff();  

  

   function getTimeDiff(){  

   var gdt1 = current.u_login.getGlideObject();   //field one

   var gdt2 = current.u_logout.getGlideObject(); // field two

               var dur = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2

var dur1= new GlideDuration();

dur1.setValue(dur.getValue());

current.u_duration=dur1.getDurationValue();// duration fieldgs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false);

               gs.addInfoMessage(gdt1);

                gs.addInfoMessage(gdt2);

                gs.addInfoMessage(dur1);

   }  

})(current, previous);

I hope this will works for u

Thanks&Regards 

Venkateswarlu K