3 Replies · Latest reply on Sep 13, 2017 7:59 AM by Jaspal singh

    Prevent Update if assigned user is me within Client Script

      I have the below onSubmit client script running on to prevent the field "expected_completion_date" from being a date value within the past. I need to update it so that it only applies to the assigned_to user or the requested_for user. I have tried multiple options but I can't seem to verify the current logged in user against the two fields assigned_to and requested_for. Can someone help me get my script correct? If the date is in the past and I'm the assigned to user, then prevent the form from being saved?

       

      function onSubmit() {

        var jsStartDate =getDateFromFormat(g_form.getValue('expected_completion_date'), g_user_date_format);

        var jsToday =getDateFromFormat(formatDate( new Date(), g_user_date_format),g_user_date_format);

        var usrID = g_form.userID; //Get current user ID

        var assignedTo = g_form.getReference('assigned_to');

        var status = g_form.getValue('current_status');

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

        if ( jsStartDate < jsToday ) {

        alert(getMessage('Expected Completion Date cannot be before todays date.'));

        g_form.showErrorBox("expected_completion_date", "Please Update"); 

        return false;

        }}}

        • Re: Prevent Update if assigned user is me within Client Script
          Dylan Mann

          It would probably be a lot easier to write a before business rule which aborts the action based on the conditions you specified. You can use GlideDateTime and all its other supporting methods with server side scripting.

           

          Hope this helps

          • Re: Prevent Update if assigned user is me within Client Script
            Jaspal singh

            Hi Edwin,

             

            You can try using below snippet

             

            function onSubmit() {

              var jsStartDate =getDateFromFormat(g_form.getValue('expected_completion_date'), g_user_date_format);

              var jsToday =getDateFromFormat(formatDate( new Date(), g_user_date_format),g_user_date_format);

              var usrID = g_form.userID; //Get current user ID

              var assignedTo = g_form.getReference('assigned_to');

            alert('Assigned to is ', assignedTo);

            var reqfor=g_form.getReference('requested_for');

            alert('Requested for is ', reqfor);

              var status = g_form.getValue('current_status');

            if(g_form.getValue('expected_completion_date')!='')

            {

            alert('in loop');

              if (( jsStartDate < jsToday ) && ((usrID==assignedTo) || (usrID==reqfor)))

            {

            alert('In loop2');

              alert(getMessage('Expected Completion Date cannot be before todays date.'));

              g_form.showErrorBox("expected_completion_date", "Please Update");

              return false;

              }

            }

            }

            Thanks,
            Jaspal Singh

            Hit Like or Correct on the impact of response.