Hello - New to nintex and javascript.
I have a SharePoint custom list with a nintex NewForm including custom javascript (and HTML, CSS) and I need to make the attachment control required. I have found many solutions and suspect the reason I have not been able to solve the issue is placement of the attachment validation within the existing javascript:
var queryString = function(field, url)
{
var href = url ? url : window.location.href;
var reg = new RegExp('[?&]' + field + '=([^&#]*)','i');
var string = reg.exec(href);
return string ? string[1] : null;
};NWF$(document).ready(function()
{
NWF$(".nf-form-footer").hide();
NWF$("#"+ IIStatus).hide();
NWF$("#"+ ICStatus).hide(); var isEditMode = document.location.pathname.indexOf("/EditForm.aspx") > -1;
if(!isEditMode )
{
NWF$("#"+ SumComment).val('Please Approve.');
var amount = NWF$("#"+ IAmount).val();
var location = NWF$("#"+ ILocation).val() == null ? '' : NWF$("#"+ ILocation)[0].options.length > 0 ? NWF$("#"+ ILocation)[0].options[NWF$("#"+ ILocation)[0].selectedIndex].title : null;
var department = NWF$("#"+ IDepartment).val() == null ? '' : NWF$("#"+ IDepartment)[0].options.length > 0 ? NWF$("#"+ IDepartment)[0].options[NWF$("#"+ IDepartment)[0].selectedIndex].title : null;
NWF$("#"+ IAmount).blur(function(){
amount = this.value;
getApprover(amount,location,department);
}); NWF$("#"+ ILocation).change(function()
{
if(this.value != '')
{
location = this.options[this.selectedIndex].title;
getCoder(location);
getApprover(amount,location,department);
}
});
NWF$("#"+ IDepartment).change(function()
{
if(this.value != '')
{
department = this.options[this.selectedIndex].title;
getApprover(amount,location,department);
}
});
}
else
{
if(NWF$("#"+ ICStatus).val() == "Rejected" || NWF$("#"+ IIStatus).val() == "Rejected")
{
NWF$("#"+ IIStatus).val('Coder Approval Pending');
NWF$("#"+ ICStatus).val('Pending Approval');
}
}
});
function getApprover(amount,location,department)
{
approverClear();
if(amount != '' && location != '' && department != '')
{
NWF$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Approver Detail')/Items?$orderby=Amount asc&$select=Location/Title,Department/Title,Amount, Title&$expand=Location, Department&$filter=Location/Title eq '" + location + "' and Department/Title eq '" + department + "'", type: "GET",
headers: {"accept": "application/json;odata=verbose"},
success: function (data)
{
debugger;
if(data != undefined && data.d != undefined && data.d.results != undefined && data.d.results.length > 0)
{
var maxAmt = true;
for(var cnt = 0; cnt < data.d.results.length; cnt++)
{
if(data.d.results[cnt].Role != "Coder 1" && data.d.results[cnt].Role != "Coder 2")
{
if(data.d.results[cnt].Amount == amount)
{
AddUserToPPL(data.d.results[cnt].Title);
maxAmt = false;
return false;
}
}
} if(maxAmt)
{
for(var cnt = 0; cnt < data.d.results.length; cnt++)
{
if(data.d.results[cnt].Role != "Coder 1" && data.d.results[cnt].Role != "Coder 2")
{
if(data.d.results[cnt].Amount > amount)
{
AddUserToPPL(data.d.results[cnt].Title);
return false;
}
}
}
}
}
},
error: function (xhr) {
alert(JSON.stringify(xhr));
alert(xhr.status + ': ' + xhr.statusText);
}
});
}
}function AddUserToPPL(ApproverEmail)
{
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/siteusers/getbyemail(@v)?@v='" + ApproverEmail + "'";
try
{
NWF$.ajax({
url: requestUri,
type: 'GET',
headers: { 'ACCEPT': 'application/json;odata=verbose' },
success: GetApproverSuccess,
error: GetApproverError
});
}
catch (err)
{
//jQuery('#errorMsg').html('getListData Error: ' + err);
}
}function GetApproverSuccess(data)
{
//var Approver = data.d.GetUserProfilePropertyFor;
var Approver = data.d.LoginName;
var ApproverPicker = new NF.PeoplePickerApi('#' + IAssign);
ApproverPicker.search(Approver).done(function (data)
{
//ApproverPicker.clear();
ApproverPicker.add(data[0]);
});
}function GetApproverError(sender, args)
{
//$get("results").innerHTML = "Error: " + args.get_message();
approverClear();
} function getCoder(location)
{
coderClear(); if(location != '')
{
NWF$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Approver Detail')/Items?$select=Location/Title,Title,Role&$expand=Location&$filter=Location/Title eq '" + location + "'",
type: "GET",
headers: {"accept": "application/json;odata=verbose"},
success: function (data)
{
if(data != undefined && data.d != undefined && data.d.results != undefined && data.d.results.length > 0)
{
for(var cnt = 0; cnt < data.d.results.length; cnt++)
{
if(data.d.results[cnt].Role == "Coder 1" || data.d.results[cnt].Role == "Coder 2")
{
if(cnt == 0)
AddCoderToPPL(data.d.results[cnt].Title);
}
}
//data.d.results[0].Title
}
},
error: function (xhr) {
alert(JSON.stringify(xhr));
alert(xhr.status + ': ' + xhr.statusText);
}
});
}
}function AddCoderToPPL(ApproverEmail)
{
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/siteusers/getbyemail(@v)?@v='" + ApproverEmail + "'";
try
{
NWF$.ajax({
url: requestUri,
type: 'GET',
headers: { 'ACCEPT': 'application/json;odata=verbose' },
success: GetCoderSuccess,
error: GetCoderError
});
}
catch (err)
{
//jQuery('#errorMsg').html('getListData Error: ' + err);
}
}
function GetCoderSuccess(data)
{
//var Approver = data.d.GetUserProfilePropertyFor;
var Coder = data.d.LoginName;
var CoderPicker = new NF.PeoplePickerApi('#' + ICoder);
CoderPicker.search(Coder).done(function (data)
{
//CoderPicker.clear();
CoderPicker.add(data[0]);
});
}
function GetCoderError(sender, args)
{
//$get("results").innerHTML = "Error: " + args.get_message();
coderClear();
}
function coderClear()
{
var CoderPicker = new NF.PeoplePickerApi('#' + ICoder);
CoderPicker.clear();
}
function approverClear()
{
var ApproverPicker = new NF.PeoplePickerApi('#' + IAssign);
ApproverPicker.clear();
}
function cancel()
{
window.location = "MyCompanyURL";
}
//url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Approver Detail')/Items?$orderby=Amount&$select=Location/Title,Department/Title,Amount, Title&$expand=Location, Department&$filter=Location/Title eq '" + location + "' and Department/Title eq '" + department + "' and Amount ge '" + amount + "'", function InvoiceUpdate()
function InvoiceUpdate()
{
if(isEditMode )
{
debugger;
var itemType1 = GetItemTypeForListName("Invoice Pending");
var item1 = {
"__metadata": { "type": itemType1 },
"Title": NWF$("#" + _invoiceID).val(),
"LocationId":NWF$("#" + ILocation).val(),
"DepartmentId": NWF$("#" + IDepartment).val(),
"InvoiceAmount": NWF$("#" + IAmount).val(),
"InvoiceDate": NWF$("#" + _InvoiceDate).val(),
"InvoiceDueDate": NWF$("#" + _InvoiceDueDate).val(),
"AssignTo": NWF$("#" + IAssign).val(),
"Coder": NWF$("#" + ICoder).val(),
"Vendor": NWF$("#" + _VendorName).val(),
"VendorNo": NWF$("#" + _VendorNum).val(),
"Comment": NWF$("#" + _cmts).val()
}; updateListItem(NWF$("#_invoiceID").val(), "Invoice Pending", _spPageContextInfo.webAbsoluteUrl, item1, function () {
//amtCount = amtCount + 1;
//updatePendingInvoice(amtCount);
}, function () { errorMsg = true; });
}
// Run Validation on Attachments
function ValidateAttachment(source, arguments) {
var Control = NWF$("#" + CtrlControl).val();
var elm = NWF$("table[id*=idAttachmentsTable]"); // PRE ATTACHMENT CONTROL CHANGE
var elmAttachmentRow = NWF$("table[id*=idAttachmentsRow]"); // POST ATTACHMENT CONTROL CHANGE var elmAttachmentRow = NWF$("div[id*=idAttachmentsRow]");
if ((elm != null && elm.prop('rows').length > 0) && (elmAttachmentRow != null)) {
arguments.IsValid = true;
}
else {
if (Control == "Yes") {
arguments.IsValid = false;
alert("Attachment Required")
}
}
}
}function GetItemTypeForListName(name) { return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}function getListItemWithId(itemId, listName, siteurl, success, failure)
{
var url = siteurl + "/_api/web/lists/getbytitle('" + listName + "')/items?NWF$filter=Title eq '" + itemId + "'";
NWF$.ajax({
url: url,
method: "GET",
async: false,
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
if (data.d.results.length == 1) {
data.d.results[0].urls = url;
success(data.d.results[0]);
}
else {
failure("Multiple results obtained for the specified Id value");
}
},
error: function (data) {
failure(data);
}
});
}function updateListItem(itemId, listName, siteUrl, item, success, failure) {
getListItemWithId(itemId, listName, siteUrl, function (data)
{
NWF$.ajax({
url: data.__metadata.uri,
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": NWF$("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": data.__metadata.etag
},
success: function (data) { success(data); },
error: function (data) { failure(data); }
});
}, function (data) { alert('Failed2'); failure(data); });
}







