onsubmit require attachement form widget

brendanwilson84
Kilo Guru

Hi Guys

I am wondering if any of you guys have done this.

I am using the form widget and have buttons as below.

Submit Online Referral is a server side button from the backend

Cancel and save as draft is custom buttons

Cancels just leaves the form, and save as draft saves the form without needed to complete required fields.

I have a requirement to make attachment mandatory if users states yes to has attachments and only when they click on the submit referral.

In the back-end, i would have used business rules previous to do this, but not sure how i could do this on the portal.

find_real_file.png

7 REPLIES 7

Gaurav Bajaj
Kilo Sage

Hi,



You can try with onSubmit catalog client scripts too, it will work for service portal as well.



Something like below will help.



  1. function onSubmit() {  
  2. var cat_id = gel('sysparm_item_guid').value;  
  3. var gr = new GlideRecord("sys_attachment");  
  4. gr.addQuery("table_name", "sc_cart_item");  
  5. gr.addQuery("table_sys_id", cat_id);  
  6. gr.query();  
  7. if (!gr.next()) {  
  8. alert("You must attach a file to submit.");  
  9. return false;  
  10. }  
  11. }


Please mark correct/helpful based on the impact of the response.




Thanks


Gaurav


Thanks Gaurav



That is returning me errors.



I have tried the following below, however even if i attach its giving me the pop up you need to attach.




Also, i only want this to happen on the submit button, is that possible?


function onSubmit() {


  try {


  var attachments = document.getElementById('header_attachment_list_label');


  if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {


  alert("message");


  return false;


  }


} catch(e) {


  if ((this.document.getElementsByClassName('get-attachment').length == 0)) {


  alert('Your are required to attach');


  return false;


}


} }


Hi Brendon,



I got this working with a cloned widget for cat item.( No need of onsubmit client script as most of functions are unsupported)


You need to check for attachments' length when a call is made to submit button.


Please try with highlighted code in client script of your widget.( cloned one)


You can check your condition here itself.




find_real_file.png





find_real_file.png




Please mark correct/helpful based on the impact of the response.




Thanks


Gaurav


thanks gaurav



Apologies, my code is quite low!   Iam not quite sure where to paste or add the code



This is the HTML   of the widget



<div class="panel-footer">


          <button ng-mousedown="triggerUIAction(action)" ng-repeat="action in getUIActions('button')" class="btn btn-success action-btn">{{action.name}}</button>


          <span>{{status}}</span>


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


              Save as Draft


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


              Cancel


        </div>



This is the client script



function ($scope, $rootScope, $timeout, spUtil, $location, $window, nowAttachmentHandler) {


$scope.mandatory = [];


$scope.data.show_sql = false;


$scope.saveButtonSuffix = spUtil.getAccelerator('s');


$scope.adminMenu = {


encodedPageUrl: encodeURIComponent($location.url()),


getClientScriptCount: function() {


var count = 0;


if ($scope.data.f.client_script) {


count += $scope.data.f.client_script.onChange.length;


count += $scope.data.f.client_script.onLoad.length;


count += $scope.data.f.client_script.onSubmit.length;


}


return count;


}


};




$scope.getUIActions = function(type) {


if ($scope.data.disableUIActions)


return [];


if (type) {


return $scope.data.f._ui_actions.filter(function(action) {


//We handle the primary action button separately.


return !action.primary && action['is_' + type];


});


} else {


return $scope.data.f._ui_actions;


}


}





$scope.getPrimaryAction = function() {


var primaryActions = $scope.data.f._ui_actions.filter(function(action) {


return action.primary;


});


return (primaryActions.length) ? primaryActions[0] : null;


}



$scope.getUIActionContextMenu = function(event) {


var menu = [];


if (event.ctrlKey)


return menu;




var contextActions = $scope.getUIActions('context');


contextActions.forEach(function(action) {


menu.push([action.name, function() {


$scope.triggerUIAction(action);


}]);


});




if (contextActions.length > 0)


menu.push(null);


menu.push([$scope.data.exportPDFMsg, function() {exportPDF("");}]);


menu.push([$scope.data.exportPDFLandMsg, function() {exportPDF('true');}]);




return menu;


}




function exportPDF(landscape) {


$window.open("/" + $scope.data.f.table + ".do?PDF&landscape=" + landscape + "&sys_id=" + $scope.data.sys_id + "&sysparm_view=" + $scope.data.f.view);


}




//trigger the primary UI Action on save (if there is one)


var deregister = $scope.$on('$sp.save', function() {


var primaryAction = $scope.getPrimaryAction();


if (primaryAction)



$scope.triggerUIAction(primaryAction);


});


$scope.$on('$destroy', function() {deregister()});




$scope.triggerUIAction = function(action) {


if ($scope.data.disableUIActions)


return;



if (g_form) {


$timeout(function() {


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




}


)}


}




$scope.$on("spModel.uiActionComplete", function(evt, response) {


var sysID = (response.isInsert) ? response.sys_id : $scope.data.sys_id;


loadForm($scope.data.table, sysID).then(constructResponseHandler(response));


});




function constructResponseHandler(response) {


return function() {


var message;


var eventName = "sp.form.record.updated";


if (response.isInsert) {


message = $scope.data.recordAddedMsg;


//message = $scope.data.recordAddedsaveMsg


var search = $location.search();


search.sys_id = response.sys_id;


search.spa = 1;


$location.search(search).replace();


} else


message = $scope.data.updatedMsg;




$scope.data.hideRelatedLists = hideRelatedLists();


$scope.$emit(eventName, $scope.data.f._fields);


$rootScope.$broadcast(eventName, $scope.data.f._fields);


$scope.status = message;


spUtil.addTrivialMessage(message);


$timeout(clearStatus, 2000);


}


}




var ctrl = this;


// switch forms


var unregister = $scope.$on('$sp.list.click', onListClick);


$scope.$on("$destroy", function() {


unregister();


})




function onListClick(evt,arg) {


loadForm(arg.table, arg.sys_id);


}




function loadForm(table, sys_id){


var f = {};


$scope.data.table = f.table = table;


$scope.data.sys_id = f.sys_id = sys_id;


f.view = $scope.data.view;


return $scope.server.update().then(setupAttachmentHandler);


}




function openRelatedList(e, queryString){


// todo: Open this in a modal


$location.search(queryString);


e.preventDefault();


}




$scope.$on('spModel.fields.rendered', function() {


if (ctrl.panels)


ctrl.panels.removeClass('shift-out').addClass('shift-in');


});




var g_form;


$scope.$on('spModel.gForm.initialized', function(e, gFormInstance) {


if (gFormInstance.getTableName() == $scope.data.f.table)


g_form = gFormInstance;


});


//brendan adding save as draft function


$scope.saveAsDraft = function() {


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


              g_form.setMandatory(x, false)


      }



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


}


//brendan adding cancel button


$scope.onlinereferralCan = function() {


if(confirm("Your changes will not be saved")){


$location.url('/onlinereferral?id=homepage');


}else{


return false;


}


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


}



// Show or hide related lists


$scope.$watch('data.f._related_lists', function(){


$scope.data.hideRelatedLists = hideRelatedLists();


}, true);




function hideRelatedLists() {


if (!$scope.data.f._related_lists)


return true;




if ($scope.options.hideRelatedLists == true)


return true;




if ($scope.data.sys_id == '-1')


return true;




// If all related lists are visible=false then hide


if ($scope.data.f._related_lists.length > 0) {


for (var i in $scope.data.f._related_lists) {


var list = $scope.data.f._related_lists[i];


if (list.visible) {


return false;


}


}


}


return true;


}




function clearStatus() {


$scope.status = "";


}




function setupAttachmentHandler(){


$scope.attachmentHandler = new nowAttachmentHandler(appendSuccess, appendError);




$timeout(function() {


var sizeLimit = 1024 * 1024 * 24; // 24MB


$scope.attachmentHandler.setParams($scope.data.table, $scope.data.f._attachmentGUID, sizeLimit);


})


}


setupAttachmentHandler();




function appendSuccess() {


spUtil.addTrivialMessage($scope.data.attachmentUploadSuccessMsg);


$scope.$broadcast("sp.attachments.update", $scope.data.f._attachmentGUID);


}







function appendError(error) {


$scope.errorMessages.push(error);


}


}