//# sourceURL=callwebservice.js
/// <reference path="~/SharePointRoot\Template\Layouts\FlowForma\js\handlebars-v2.0.0.js" />
/// <reference path="~/SharePointRoot\Template\Layouts\FlowForma\js\jquery-1.9.1.intellisense.js"/>
/// <reference path="~/SharePointRoot\Template\Layouts\FlowForma\js\FlowForma.js" />
/// <reference path="~/SharePointRoot\Template\Layouts\FlowForma\js\kendo.all.js" />
$.extend(FlowForma.Rules.Widgets,
{
callwebservice: {
// This fuction is called when the rule is triggered
Execute: function (action, question, step, expandUndo) {
// Rule deferred for awaiting rule to complete
var deferred = $.Deferred();
deferred.type = 'callwebservice';
FlowForma.FormContext.Deferred.push(deferred);
// Getting the definition that was saved in the flow designer
var definition = $.parseJSON(action.Definition);
if (definition) {
// Data that will be passed to the web service
var data = {};
data['flowId'] = definition.FlowId;
// Calling the web service
$.ajax({
url: (FlowForma.FlowFormaServerUrl == '/' ? "" : FlowForma.FlowFormaServerUrl) + '/' + '_vti_bin/FlowForma.svc/GetFlowById',
headers: FlowForma.Services.Headers,
type: "POST",
dataType: 'json',
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
success: function (data) {
// Checking if the web service returned any data
if (data.GetFlowByIdResult != null) {
// Getting the question that was saved in the rule
var setTargetQuestion = FlowForma.FormContext.GetQuestionByPickerValue(definition.TargetQuestion);
// Setting the question with the value that was recieved from the web service
FlowForma.Questions.SetQuestionByValue(setTargetQuestion, data.GetFlowByIdResult.Title, function () {
// Triggering question update event in order for other rules that are under that question to be triggered
$(setTargetQuestion).trigger(FlowForma.Events.Question.Updated, []);
// Resolving the deferred in order to know that the rule finished
deferred.resolve();
});
}
else {
deferred.resolve();
}
},
error: function (qXHR, textStatus, errorThrown) {
// Logging errors in case the web service call failed
FlowForma.LogDebug('Error getting flow');
FlowForma.HandleAjaxQueryFailure(qXHR);
deferred.resolve();
}
});
}
},
// This function is called when the rule is being created or opened in flow designer.
CreateInit: function (action, question, step, container) {
// Getting the definition that was saved in the flow designer
var definition = $.parseJSON(action.Definition);
var pickerValue = null;
var inpNumber = $(container).find('#inpNumber');
// Checking if definition exists
if (definition) {
// Filling the controls with saved values
pickerValue = definition.TargetQuestion;
inpNumber.val(definition.FlowId);
}
// Creating the FlowForma picker control (this control allows the user to select steps and questions that are located in the flow)
var ffPickerControl = FlowForma.Widgets.FlowEntityPicker($(container).find('#ffPicker'), pickerValue);
// Attaching a function that will be fired once rule saving event will be triggered. [after pressing the save button in the rule configuration dialogue]
$(FlowForma.FlowContext.Flow).on(FlowForma.Events.Flow.RuleSaving, function (e, args) {
// Creating an object that will store all of the data that is needed for the rule
var actionData = {};
// Reading the data from the controls and setting it to the data object
actionData.TargetQuestion = ffPickerControl.Value();
actionData.FlowId = inpNumber.val();
// Stringifying and saving the data
action.Definition = JSON.stringify(actionData);
});
//#endregion
},
ExpandUndo: false
}
});