Client Script populate Date/Time field

brkelly
Kilo Expert

I have a UI action that needs to record the current date/time in a field and set other values, and then it runs a server side function.

It uses the client OnClick and Action name enabling both client and server side functions to run.  

The challenge is that on the server side, I am creating a pdf of the record that needs to have the current date/time.   If I populate the date/time on the server side, it doesn't get captured in the pdf that is created.   All of the scripts that I am finding to do this on the client side, muck with the server side which I don't know if it will work because of the script type.

Is there a way to get the current date/time to populate a field from a true client script?   Thus far my attempts at using glide ajax on the client side have failed.   It seems like I could use new Date() and parse it on the client but I haven't seen anything like that.   Any ideas would be a huge help.

It seems like I could use new Date() and parse it on the client but I haven't seen anything like that.   Any ideas would be a huge help.

A second, or maybe the real issue is that the pdf is created before the fields are populated so the information is all old.   Is there a way to ensure the pdf isn't created until after the form is saved?

//Client-side 'onclick' function

function PrePopDist(){

  //var rightNow = new Date();

  //var rightNow = new GlideDateTime().getDisplayValue();

  //alert ("date time == " + rightNow);

  g_form.setValue('u_status', 'Sent for distribution');

  g_form.setValue('u_notes', 'CPEN Sent for Distribution by CSL');

  //form.setValue('u_csl_transmitted_date_time', rightNow);

  gsftSubmit(null, g_form.getFormElement(), 'send_for_dist_test');

}

if(typeof window == 'undefined')

saveRecord3();

//Server-side function

function saveRecord3() {

//the date set here works great but it doesn't come over on the pdf that is created.

  current.u_csl_transmitted_date_time=now();

  var rm = new sn_ws.RESTMessageV2();

  rm.setHttpMethod('GET');

  var url = gs.getProperty("glide.servlet.uri") + current.getTableName()+ '.do?PDF&sys_id=' + current.sys_id;

  rm.setEndpoint(url);

  rm.setBasicAuth(gs.getProperty('App_Serviceaccount'), gs.getProperty('App_Serviceaccount_pwd'));

  rm.saveResponseBodyAsAttachment(current.getTableName(),current.sys_id,current.u_cpen_number+".pdf");

  var response = rm.execute();

  if(response.getStatusCode()==200) {

        gs.addInfoMessage('Attachment added, sending to distribution.');

  action.setRedirectURL(current);

  current.update();

  }

  else

  gs.addInfoMessage(response.getStatusCode() + ": failed to attach CPEN Distribution not issued!");

}

Thank you!

Brian

1 ACCEPTED SOLUTION

The reason you are seeing old data is because you are doing the current.update() after your REST message. How about moving it up right before the line



current.update();     // move current.update from inside the if statement to here


var rm = new sn_ws.RESTMessageV2();


View solution in original post

4 REPLIES 4

Chuck Tomasi
Tera Patron

Hi Brian,



I'm not seeing the need for the client side operation. This appears to all be do-able and much simpler with a server side only script something like this:



  var now = new GlideDateTime();


    current.u_status = 'Sent for distribution';


    current.u_notes = 'CPEN Sent for Distribution by CSL';


  current.u_csl_transmitted_date_time= now;  


  var rm = new sn_ws.RESTMessageV2();  


  rm.setHttpMethod('GET');  


  var url = gs.getProperty("glide.servlet.uri") + current.getTableName()+ '.do?PDF&sys_id=' + current.sys_id;  


  rm.setEndpoint(url);  


  rm.setBasicAuth(gs.getProperty('App_Serviceaccount'), gs.getProperty('App_Serviceaccount_pwd'));  


  rm.saveResponseBodyAsAttachment(current.getTableName(),current.sys_id,current.u_cpen_number+".pdf");  


  var response = rm.execute();  


  if(response.getStatusCode()==200) {  


        gs.addInfoMessage('Attachment added, sending to distribution.');  


        action.setRedirectURL(current);  


        current.update();  


  } else {


        gs.addInfoMessage(response.getStatusCode() + ": failed to attach CPEN Distribution not issued!");  


    }


Chuck,


The challenge is that the pdf is created with the old data.   This is how I had the script running in the past, it just doesn't populate the pdf correctly.   I guess the real problem is timing.     I was dreaming that an 'on click action' would help that, but I was clearly wrong.



thank you!


Brian


The reason you are seeing old data is because you are doing the current.update() after your REST message. How about moving it up right before the line



current.update();     // move current.update from inside the if statement to here


var rm = new sn_ws.RESTMessageV2();


Funny how complicated we can make things isn't it!


thank you so much Chuck!