buttons on form widget

brendanwilson84
Kilo Guru

Hi Guys

I have a form widget, in that we are going to be creating records from. I can get my form on page fine, using sys_id=-1.

It is quite a lengthy form so therefore they required a save as draft and submit button. Using a record producer is prob out of the question now as they want to save as draft and come back later to complete.

I cloned the form widget and looked at the html part and could get the submit button added

find_real_file.png

However, what i need is, if save is clicked, then remove the required field stamp and save the record as a draft. I have a draft state in my form.   My coding experience is pretty low, especially on the portal side

Any Pointers or anyone do this before?

1 ACCEPTED SOLUTION

Justin Abbott
Giga Guru

You could try this, Brendan.



HTML Button near the bottom of the form:


<button type="button" ng-click="saveAsDraft()" class="btn btn-primary action-btn pull-right">


              Save as Draft


</button>



Method added to the Client Script (controller), just before the last brace.


$scope.saveAsDraft = function() {


      for (var x in $scope.data.f._fields) {


              g_form.setMandatory(x, false);


      }


      $scope.triggerUIAction($scope.getPrimaryAction());


}



The saveAsDraft function gets all of the fields on the form, sets them as not mandatory, then triggers the save.


View solution in original post

17 REPLIES 17

Justin Abbott
Giga Guru

You could try this, Brendan.



HTML Button near the bottom of the form:


<button type="button" ng-click="saveAsDraft()" class="btn btn-primary action-btn pull-right">


              Save as Draft


</button>



Method added to the Client Script (controller), just before the last brace.


$scope.saveAsDraft = function() {


      for (var x in $scope.data.f._fields) {


              g_form.setMandatory(x, false);


      }


      $scope.triggerUIAction($scope.getPrimaryAction());


}



The saveAsDraft function gets all of the fields on the form, sets them as not mandatory, then triggers the save.


Justin, thanks very much, that worked well. I was about to give up on it!



One point, it does not remove required for fields that are set mandatory on dictory, it does for fields with UI policy setting them, but i can work with that.   Again, thank you


Hi Justin



Got another question and wondering if you could help. So i have my form loaded into form widget and got the save as draft working- thank you.!



On the out of box save button can i change the status on my form to Submitted? I have used the form widget and placed sys_id=-1 to load a new record each time, once all fields completed and saved, the status should change to submitted.


In the $timeout, just before the g_form.submit, you could try building logic to check if it's a new record and NOT a draft, then set the state to submitted:



$timeout(function() {


      if (g_form) {


              if (g_form.isNewRecord() && g_form.getValue('state') != 'YOUR_VALUE_FOR_DRAFT') {


                      g_form.setValue('state', 'YOUR_VALUE_FOR_SUBMITTED');


              }


              g_form.submit(action.action_name || action.sys_id);


      }


});



But it might be wiser to handle that with a business rule on the back-end. Something like, on insert, if State is not Draft, set State to Submitted.