21 Replies · Latest reply on Oct 20, 2017 7:42 AM by Chase Long

    Service Portal Catalog Client Script - past date

      Hello,

       

      We recently started using the Service Portal/ Mobile UI and are having an issue with a Catalog Client Script. We are trying to prevent users from selecting dates that are in the past.Here is the script that works fine in the non-Service Portal view:

       

      function onChange(control, oldValue, newValue, isLoading) { 

          if(isLoading || newValue == '') { 

              return; 

          } 

          var date = new Date(); 

          var ed= new Date(getDateFromFormat(g_form.getValue('end_date'),g_user_date_time_format)); 

          if (ed < date) { 

              alert('End date must be in the future.'); 

              g_form.clearValue('end_date'); 

          } 

      }

       

      I have found that with the SP the structure of scripting has changed some, being fairly new to ServiceNow and javascript in general, hasnt helped much.

       

      One thing I found on this site mentioned splitting the function into 2 pieces, a UI Policy and a Client Script. I would think there is a pretty simple change to the script that would work on both views.

       

      Thanks in advance for any help.

        • Re: Service Portal Catalog Client Script - past date
          Brad Tilton

          Since Service Portal only supports a subset of client scripting methods I think you'd have to use glideajax to pass the date to a script include, have it to the compare, and then return a true or false back.

           

          documentation/client_scripting.md at master · service-portal/documentation · GitHub

          1 of 1 people found this helpful
          • Re: Service Portal Catalog Client Script - past date
            Vladimir Markovic

            Hi,

            We use this script (UI Type = both)

             

            function onChange(control, oldValue, newValue, isLoading) {

              if (isLoading || newValue == '') {

              return;

              }

             

              if (newValue != '') {

              //current date

              var currentDateObj = new Date();

              var currentDateStr = formatDate(currentDateObj, g_user_date_time_format);

              var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_time_format);

             

              //get start date

              var startDateStr = g_form.getValue('v_event_start_timezone');

              var startDateNum = getDateFromFormat(startDateStr, g_user_date_time_format);

             

              var diff = startDateNum - currentDateNum;

              var maxDiff = 7 * 24 * 60 * 60 * 1000; //7 days * 24 hrs * 60 mins * 60 secs * 1000 ms

             

              if (startDateNum == 0) {

              alert('Please use the calendar icon to select a start date or enter the date/time in this format: ' + g_user_date_time_format);

              g_form.setValue('v_event_start_timezone', '');

              } else if (startDateNum < currentDateNum) {

              alert('You cannot select a start date in the past. Please use the calendar icon to select a start date or enter the date/time in this format: ' + g_user_date_time_format);

              g_form.setValue('v_event_start_timezone', '');

              }

              }

            }

            Thanks,
            Vlady

            • Re: Service Portal Catalog Client Script - past date
              kurt young

              Thanks for the response, this works on the admin side but not in Service Portal. We have found some information about synchronous and asynchronous difference sin service portal, but not a good reference on how to properly change the scripting between the two.

                • Re: Service Portal Catalog Client Script - past date
                  Soumyabrata Dash

                  Hi Kurt ,

                   

                  I have similar situation,  were you able to find a resolution/workaround to this or we have to wait for a patch fix,

                  Awaiting your valuable inputs.

                    • Re: Service Portal Catalog Client Script - past date
                      dvp

                      You can achieve this by using GLideAjax

                      Here is the sample script

                       

                      please update the 'var_eventStart' and 'var_eventStart' variables

                       

                       

                       

                      I highly suggest you to go through the link provided by Brad to know more about client side  functionalities that are supported in Service Portal.

                       

                      function onChange(control, oldValue, newValue, isLoading) {
                      
                        if (isLoading || newValue == '') {
                            return;
                        }
                      
                       if (g_form.getValue('var_eventStart') != '')
                        {
                        var cdt = g_form.getValue('var_eventStart'); //First Date/Time field
                        var sdt = g_form.getValue('var_eventEnd'); //Second Date/Time field
                        var dttype = 'second'; //this can be day, hour, minute, second. By default it will return seconds.
                       
                        var ajax = new GlideAjax('ClientDateTimeUtils');
                        ajax.addParam('sysparm_name','getDateTimeDiff');
                        ajax.addParam('sysparm_fdt', cdt);
                        ajax.addParam('sysparm_sdt', sdt);
                        ajax.addParam('sysparm_difftype', dttype);
                        ajax.getXML(function () {
                        
                        
                        
                       
                        var answer = ajax.getAnswer();
                          
                        if (answer <0){
                          alert('You cannot select an End Date prior to the Start Date.');
                          g_form.setValue('var_eventEnd', '');
                        }
                          
                        else if (answer == 0){
                          alert('You cannot select an End Date equal to Start Date.');
                                g_form.setValue('var_eventEnd','');
                        } 
                        });
                      }
                       else{
                        alert('Start Date cannot be empty');
                          g_form.setValue('var_eventEnd', '');
                       }
                      
                      }
                      
                      1 of 1 people found this helpful
                        • Re: Service Portal Catalog Client Script - past date
                          Soumyabrata Dash

                          Hi ,

                           

                          Thank you for the response , Could you please share the script include used here.

                           

                          Below is my version of the Client Script & Script Include, I continue facing the same issue , The script include returns the diff which is visible in admin instance

                          However Service portal fails to read it,

                           

                          if (startdate != '')

                            {

                            alert('in start date not empty');

                            var cdt = g_form.getValue('effective_start'); //First Date/Time field

                            alert(cdt);

                            var sdt = g_form.getValue('effective_end'); //Second Date/Time field

                            alert(sdt);

                            //this can be day, hour, minute, second. By default it will return seconds.

                            var dttype ='second';

                            alert(dttype);

                           

                            var ajax = new GlideAjax('ClientDateTimeUtils');

                            ajax.addParam('sysparm_name','getDateTimeDiff');

                            ajax.addParam('sysparm_fdt', cdt);

                            ajax.addParam('sysparm_sdt', sdt);

                            ajax.addParam('sysparm_difftype', dttype);

                            ajax.getXMLWait();

                          var answer = ajax.getAnswer();

                          alert(answer);

                          if(answer < 0)  {

                            alert('Hi');

                          }

                          }

                          }

                           

                           

                           

                           

                          -------------------------------

                          Script Include:

                          -------------------------------

                          var ClientDateTimeUtils = Class.create();

                          ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

                           

                           

                          getDateTimeDiff: function(){

                          var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field

                          var secondDT = this.getParameter('sysparm_sdt'); // Second Date-Time Field

                          //var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day

                          var diff = gs.dateDiff(firstDT, secondDT, true);

                           

                          return diff;

                          }

                          })

                           

                          Awaiting your valuable inputs, Thanks in advance!!.

                    • Re: Service Portal Catalog Client Script - past date
                      Jim Cheffer

                      Hello Kurt, did you ever find a solution? I have the same issue and very confused as I follow this thread. Jim

                        • Re: Service Portal Catalog Client Script - past date
                          Ujjawal Vishnoi

                          Hi Jim,

                           

                          You can use the below mentioned script

                           

                          function onChange(control, oldValue, newValue, isLoading) {

                            if (isLoading || newValue == '') {

                            return;

                            }

                            var ajax = new GlideAjax('MyDateTimeAjax');

                            ajax.addParam('sysparm_name', 'nowDateTime');

                            ajax.getXML(checkdate);

                            function checkdate(response){

                            var currentDate = response.responseXML.documentElement.getAttribute("answer");

                            var startDate = g_form.getValue('u_service_start_time');

                            if (startDate < currentDate){

                            alert('You cannot select a date in the past.');

                            g_form.setValue('u_service_start_time', '');

                            }

                            }

                          }

                           

                          Script Include:

                           

                          var MyDateTimeAjax = Class.create();

                          MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

                            nowDate: function () {

                            return gs.now();

                            },

                           

                            nowDateTime: function () {

                            return gs.nowDateTime();

                            },

                            type: 'MyDateTimeAjax'

                          });

                           

                          I am using the above setup and it is working fine on portal.

                           

                          Hope this helps

                           

                          Regards,

                          Ujjawal

                          2 of 2 people found this helpful
                          • Re: Service Portal Catalog Client Script - past date
                            Ujjawal Vishnoi

                            Hi Jim

                             

                            Did you try the code shared by me. Please do let me know if you still have any issue.

                             

                            Regards,

                            Ujjawal

                              • Re: Service Portal Catalog Client Script - past date
                                Jim Cheffer

                                Ujjawal, I will try it today and let you know. Thanks Jim

                                  • Re: Service Portal Catalog Client Script - past date
                                    Jim Cheffer

                                    Ujjawal,

                                    It works in our classic and portal catalogs!!!! I took your code and just replaced it with my variable name.

                                    We have other client scripts that compare start date before end date and future dates say 30 days out so I have something to work with.

                                    Will also try g_form.showerrorBox rather than alert.

                                     

                                    thanks for the help.

                                     

                                    Jim

                                     

                                     

                                     

                                      • Re: Service Portal Catalog Client Script - past date
                                        Jim Cheffer

                                        Hello Ujjawal,

                                         

                                        Sorry for the confusion. I must be losing my mind on this Service Portal. This code is not working all the way.

                                        In our classic view if I enter a future date it works, If I enter a past date message appears and then change it to future date message still appears.

                                        In the portal message always appears.

                                         

                                        I put in clear value but same result. Also to show the dates which looked good. Maybe need to convert to number? Appears April works but May dates do not? I think it is the date format.

                                         

                                        Client Script

                                         

                                        function onChange(control, oldValue, newValue, isLoading) {

                                          if (isLoading || newValue == '') {

                                          return;

                                          }

                                          var ajax = new GlideAjax('MyDateTimeAjax');

                                          ajax.addParam('sysparm_name', 'nowDateTime');

                                          ajax.getXML(checkdate);

                                          function checkdate(response){

                                          var currentDate = response.responseXML.documentElement.getAttribute("answer");

                                          var startDate = g_form.getValue('clone_date_time');

                                        //  g_form.setValue('additional_comments', 'Clone Date ' + startDate + '  ' + 'Today ' + currentDate);

                                          if (startDate < currentDate){

                                          alert('You cannot select a date in the past.');

                                        //  g_form.setValue('clone_date_time', '');

                                          g_form.clearValue('clone_date_time'); 

                                          }

                                          }

                                        }

                                         

                                        Script Include

                                         

                                         

                                        var MyDateTimeAjax = Class.create();

                                        MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

                                          nowDate: function () {

                                          return gs.now();

                                          },

                                         

                                          nowDateTime: function () {

                                          return gs.nowDateTime();

                                          },

                                          type: 'MyDateTimeAjax'

                                        });MydateTime

                                          • Re: Service Portal Catalog Client Script - past date
                                            Shannon Burns

                                            I was able to get the script include and the client script to work to compare to the current date.  But I actually need to make sure that the date is a minimum of 30 days in the future. 

                                             

                                            I don't understand working with dates enough to change it to compare for that.  If I try to simply add 30 to the value, it concatenates it to the date like a string.

                                             

                                            Shannon

                                              • Re: Service Portal Catalog Client Script - past date
                                                Ujjawal Vishnoi

                                                Hi Shannon,

                                                 

                                                You can use the below mentioned logic to add 30 days.

                                                var gdt = new GlideDateTime(<variable that store date>);

                                                gdt.addDays(30);

                                                 

                                                Hope this helps.

                                                 

                                                Regards

                                                Ujjawal

                                                1 of 1 people found this helpful
                                                  • Re: Service Portal Catalog Client Script - past date
                                                    Shannon Burns

                                                    Thanks.  That ended up causing issues becuase now it was date time and I was comparing it to a date, but I eventually got it sorted out.  Dates are such a pain to deal with.

                                                     

                                                    Thanks again,
                                                    Shannon

                                                      • Re: Service Portal Catalog Client Script - past date
                                                        Chase Long

                                                        Do you mind posting your client script / script include? I'm running into a similar issue trying to move a service catalog form over to ServicePortal. I need to compare two date/time fields and do the opposite of you - ensure there is no more than 30 days between them.

                                                          • Re: Service Portal Catalog Client Script - past date
                                                            Chase Long

                                                            Actually I was able to figure this out using the base code from dvp and the script include from this post. As always thanks ServiceNow community! Here's the code I ended up using in case anyone else needs it.

                                                             

                                                            function onChange(control, oldValue, newValue, isLoading) { 

                                                             

                                                            if (isLoading || newValue == '') {

                                                            return;

                                                            }

                                                             

                                                             

                                                            if (g_form.getValue('guest_wifi_access_begin') != '') {

                                                            var cdt = g_form.getValue('guest_wifi_access_begin'); //First Date/Time field

                                                            var sdt = g_form.getValue('guest_wifi_access_end'); //Second Date/Time field

                                                            var dttype = 'second'; //this can be day, hour, minute, second. By default it will return seconds.

                                                            var ajax = new GlideAjax('ClientDateTimeUtils');

                                                            ajax.addParam('sysparm_name','getDateTimeDiff');

                                                            ajax.addParam('sysparm_fdt', cdt);

                                                            ajax.addParam('sysparm_sdt', sdt);

                                                            ajax.addParam('sysparm_difftype', dttype);

                                                            ajax.getXML(function () {

                                                            var answer = ajax.getAnswer();

                                                             

                                                             

                                                            if (answer <= 0 ){

                                                            alert('You cannot select an Access End date prior to the Access Begin date.');

                                                            g_form.setValue('guest_wifi_access_end', '');

                                                            }

                                                            else if (answer > 2592000){

                                                            alert('You cannot select an Access End date greater than 30 days from the Access Begin date.');

                                                            g_form.setValue('guest_wifi_access_end','');

                                                            }

                                                            });

                                                            } else {

                                                            alert('You must first select an Access Begin date.');

                                                            g_form.setValue('guest_wifi_access_end', '');

                                                            }

                                                            }