<< Click to Display Table of Contents >> Navigation: Legacy Tools > User Info Filler > Configuring User Info Filler > Using JavaScript > Custom JavaScript > Submit Button |
Submit Action Button - Sample Code
Overview: A Submit Action button can be placed on a form. It has two parts - Script and Crow Canyon Custom Action. At least of one of these has to be specified for the Submit action. Script code is similar in functionality to Script action with additional feature of setting column values that are not shown on the Form. If script is specified, then at the end of processing, it must call.
1.Success: functionCallback();
2.Success with item column values to update: functionCallback(false, listColumnValueobj); // refer example below
3.Failure and show a message to user: functionCallback(true, 'failure message text');
On clicking of Submit action, processing happens in following steps:
1.Execute the script. If it times out or returns an error as part of functionCallback then further processing is stopped. Script processing should finish within 30 seconds otherwise the Forms code continues with the processing without waiting for result of script execution
2.Call the Form Save functionality. This will in-turn call the validations configured in the form and if validations fail then processing will stop with validation errors highlighted on the Form
3.If Save is successful, then call the custom action specified in the Submit action
Note: If #2 fails, then user will fix the validation errors and click the submit action button again. This means, the script (if configured) will execute again. Due to this, script code should first check the state before modifying it. For example, if you are creating a SharePoint list item, then first check if it is already created before creating another one. For the example below, check is not required as it is just for setting a column value based on other column values
Two Scenarios when submitting a Form:
Update columns based on other columns
Update column based on information in another SharePoint list
Scenario One: Update value of "Risk Level" column based on value of columns "Impact" and "Probability of Failure". Subsequently item Save will be invoked and if any custom action is specified in the Submit action then that will be executed.
// Parameters:
// spContext: SharePoint client context object
// formContext: NITRO Form context
// listColumnsInfo: Object with key as the internal name of column and value as list column meta data
// currentItem: SharePoint object for the current list item on which action is being executed
// functionCallback: To be called after finishing the processing. Called automatically after the 30 seconds if not called.
try{
// Add check to see if Impact exists
if(listColumnsInfo.hasOwnProperty("Impact")){
if(listColumnsInfo.hasOwnProperty("ProbabilityofFailure")){
if(listColumnsInfo.hasOwnProperty("RiskLevel")){
// Get the value of Impact from Form.
var Impact = formContext.fetchColumnValueUI("Impact");
// Get the value of Probability of Failure from Form.
var probabilityofFailure = formContext.fetchColumnValueUI("ProbabilityofFailure");
var RiskLevel = "Low";
if(Impact == "Low" && probabilityofFailure = "Low"){
RiskLevel = "Low";
}else if(Impact == "Low" && probabilityofFailure = "Normal"){
RiskLevel = "Normal";
}
else if(Impact == "Low" && probabilityofFailure = "High"){
RiskLevel = "Normal";
}
else if((Impact == "Normal" && probabilityofFailure = "Normal")
|| (Impact == "Normal" && probabilityofFailure = "High")
|| (Impact == "Normal" && probabilityofFailure = "Low")){
RiskLevel = "Normal";
}
else if(Impact == "High"){
RiskLevel = "High";
}
// formContext.setColumnControlValueByName("RiskLevel",RiskLevel);
// if column not exist on form
var listColumnValueobj = {};
listColumnValueobj["RiskLevel"] = RiskLevel;
functionCallback(false, listColumnValueobj);
}
else{
//true indicates error occured and second parameter is used for error message will be shown below the button
functionCallback(true, "'Risk Level' column does not exist");
}
}
else{
//true indicates error occured and second parameter is used for error message will be shown below the button
functionCallback(true, "'Probability of Failure' column does not exist");
}
}
else {
//true indicates error occured and second parameter is used for error message will be shown below the button
functionCallback(true, "'Impact' column does not exist");
}
}
catch(ex){
//true indicates error occured and second parameter is used for error message will be shown below the button
functionCallback(true, ex.message);
}
Scenario Two: Update Approver based on Department from Approver list. Subsequently item Save will be invoked and if any custom action is specified in the Submit action, that action will be executed.
// Parameters:
// spContext: SharePoint client context object
// formContext: NITRO Form context
// listColumnsInfo: Object with key as the internal name of column and value as list column meta data
// currentItem: SharePoint object for the current list item on which action is being executed
// functionCallback: To be called after finishing the processing. Called automatically after the 30 seconds if not called.
try{
// Add check to see if Department exists
if(listColumnsInfo.hasOwnProperty("Department")){
// Get the value of Department from Form.
var selectedDepartment = formContext.fetchColumnValueForLookup("Department");
if(selectedDepartment && selectedDepartment.length == 1){
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
'<View>'
+'<Query>'
+'<Where>'
+'<Eq>'
+'<FieldRef Name="Department" LookupId="TRUE"/>'
+'<Value Type="Lookup">'
+ selectedDepartment[0]
+ '</Value>'
+'</Eq>'
+'</Where>'
+'</Query>'
+'</View>'
);
var list = spContext.get_web().get_lists().getByTitle("Approvers");
var approverItem = list.getItems(camlQuery);
// spContext.load(collListItem);
spContext.load(approverItem);
spContext.executeQueryAsync(function () {
var approver = [];
if (approverItems.get_count() > 0) {
var listItemEnumerator = approverItems.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var objVal = oListItem.get_item("Approver");
if (objVal != null) {
var approverID = objVal.get_lookupId();
approver.push(approverID);
}
}
}
formContext.setColumnControlValueByName("Approver", approvers);
functionCallback();
},
function(){
functionCallback(true, "Reading Approvers from Approver list failed");
});
}
}
else {
functionCallback(true, "'Department' column does not exist");
}
}
catch(ex){
//true indicates error occured and second parameter is used for error message will be shown below the button
functionCallback(true, ex.message);
}