Creating Action Handlers for User Actions

The form provides Submit, Close and Cancel actions (displayed to the user as buttons).

Handlers for these actions are added using the Runtime Forms Adapter setActionHandler method. The handlers are added once the form is successfully initialized, via the formLoadSuccessCallback function

var cancelHandler = function (actionName, form) {
   try {
        window.external.CancelForm();
        form.destroy();
       } catch (err) {
           alert("Error cancelling form: " + err.Description);
       }
};
var closeHandler = function (actionName, form) {
   try {
        var data = form.getSerializedParameters();
        window.external.CloseForm(data);
       } catch (err) {
           alert("Error closing form: " + err.Description);
       }
}
var submitHandler = function (actionName, form) {
   try {
        var data = form.getSerializedParameters();
        window.external.SubmitForm(data);
       } catch (err) {
           alert("Error submitting form: " + err.Description);
       }
}
var formLoadSuccessCallback = function (form) {
   form.setActionHandler(form.ACTION_CANCEL, cancelHandler);
   form.setActionHandler(form.ACTION_CLOSE, closeHandler);
   form.setActionHandler(form.ACTION_SUBMIT, submitHandler);
};
var formLoadErrorCallback = function (error) {
   alert('Form load failed with error: ' + error);
};

When the user performs an action by clicking a button, the handler calls an appropriate method to process the action. For a Close or Submit action, data is passed back.

For example, the submitHandler calls the ViewWorkItem.SubmitForm method,

public void SubmitForm(string data)
{
   try
   {
      _adapter.SubmitWorkItem(_workItem, _item.workTypeDetail.uid, _item.workTypeDetail.version, _guid, data);
      _formClosed = true;
      Close();
   }
   catch (Exception e)
   {
      MessageBox.Show("Error closing form: " + e.Message);
   }
}

which in turn calls the Adapter.SubmitWorkItem method. This method completes the work item, by building the request data for and then invoking a completeWorkItem operation from the WorkPresentationService.

public void SubmitWorkItem(WorkItem item, string workTypeUid, string workTypeVersion, string guid, string data)
{
   WorkPresentationServiceClient wpSvcClient = _handler.GetWorkPresentationServiceClient();
   WorkRequest workRequest = new WorkRequest();
   WorkRequest.responsePayloadMode = payloadModeType.JSON;
   workRequest.payloadDetails = new dataPayload();
   workRequest.payloadDetails.payloadMode = payloadModeType.JSON;
   workRequest.payloadDetails.Item = data;
   workRequest.channelId = "openspaceGWTPull_DefaultChannel";
   workRequest.channelType = ChannelType.openspaceChannel;
   workRequest.workItem = new WorkItem1();
   workRequest.workItem.id = item.id.id;
   workRequest.workItem.version = -1;
   
   workRequest.workTypeDetail = new BaseWorkRequestWorkTypeDetail();
   workRequest.workTypeDetail.uid = workTypeUid;
   workRequest.workTypeDetail.version = workTypeVersion;
   workRequest.workTypeDetail.openNextPiled = false;
   workResponse response = wpSvcClient.completeWorkItem(workRequest);
}