在SharePoint列表上使用自定义JavaScript的Nintex表单无法获得所需的用户

时间:2019-05-08 16:54:57

标签: javascript sharepoint nintex-workflow

带有自定义JS的Nintex NewItem Form,可以获取特定于用户选择的表单数据(位置)的ppl,对某些选择非常有用,而对其他选择则不起作用。

我已验证关联的SP列表数据正确-3个列表:

  • 位置
  • 部门
  • 批准人详细信息

“审批者详细信息”列表具有“位置”和“部门”的查找列。 Nintex表单按位置过滤部门。

我认为基于F12控制台结果,对于问题位置值,返回的值为null,但我不知道为什么。

以下是JSFiddle

上Nintex表单上的完整定制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 = "MySite";
}
//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;
    });
  }

}

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);
  });
}

location = this.options[this.selectedIndex].title;
getCoder(location);
getApprover(amount,location,department);

我希望JavaScript根据位置信息从批准者详细信息中获取“编码器1”或“编码器2”,并在表单上填充人员字段,该字段对于大多数位置都适用,但是...

  

问题:对于许多位置,都不会在表单字段上检索和填充用户。

0 个答案:

没有答案