Home Analyst Portal

Updating Affected User Field

Matthew_DowstMatthew_Dowst Customer IT Monkey ✭
I would like to create a task that will auto populate the Affected User based on the value of another field. The field is a custom extension that was created to hold their internal employee id. I've created a task that executes when this field is updated, and it uses the API to query for the users information, then is sets the BaseId and DisplayName for the RequestedWorkItem relationship. However, it does not update the RequestedWorkItem field in the form. If I save and refresh I will then show the displayname in the RequestedWorkItem field. I ran the debugger and confirmed that the pageForm.viewModel.RequestedWorkItem is pointing to the values set by the task. My question is, is there a way to get the field to update when this task runs? I've done similar tasks with other fields and they updated, but I'm guessing this is different because it is a relationship. I have included the function below. 


app.custom.formTasks.add('Incident', null, function (formObj, viewModel) { 
formObj.boundChange("RequestedWorkItem.employid",function () {
var employid = viewModel.get("RequestedWorkItem.employid");
$.ajax({
url: "/api/V3/Projection/GetProjectionByCriteria",
data: JSON.stringify({
 "Id": "0e1313ab-dc5c-cf9d-d6b0-e2e9835a132a",
 "Criteria": {
"Base": {
 "Expression": {
"SimpleExpression": {
 "ValueExpressionLeft": {
"Property": "$Context/Property[Type='19640a07-0c14-e53a-eda7-4eb50dc365d5']/ffc7b86b-8cb6-6fc8-dac1-774c2ab3ab95$"
 },
 "Operator": "Equal",
 "ValueExpressionRight": {
"Value": employid
 }
}
 }
}
 }
}
),
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.length > 0){
var user = JSON.parse(JSON.stringify(data[0]));
viewModel.set("RequestedWorkItem", { BaseId: user.BaseId, DisplayName: user.DisplayName });
}
else {
alert('Not Found');
}
},
error: function(xhr, textStatus, errorThrown){
  alert('request failed');
  alert(errorThrown);
}
});
});
}); 




Comments

  • Geoff_RossGeoff_Ross Cireson Consultant O.G.
    Hi Matthew,

    You're 95% of the way there. Firstly for an object picker, you need set the whole property to the whole object, not just the BaseId and DisplayName properties. So you can do 

    viewModel.set("RequestedWorkItem", user)

    However, with the affetced user, there's an extra step as the control has validation on it so you need to first add the object to the drop down list as if it was typed in so the validation passes:

    var au = $('[data-control-bind="RequestedWorkItem"]').data("kendoAutoComplete")
    var user = JSON.parse(JSON.stringify(data[0]));
    au.dataSource.add(user);
    au.value(user.Name);
    au.trigger("change");`
  • Matthew_DowstMatthew_Dowst Customer IT Monkey ✭
    Thanks Geoff, I was able to get it working. I did have to make a slight change to code. When I added the code block for the validation I received an error about a null value in the control.js. It looks like the control.js file uses the variable Name for the Display Name and Id for the Base Id. I just create an object with the Name and Id mapped to the results from my search criteria.

    I've pasted the final code below, in case it could help anyone else in the future. 
    app.custom.formTasks.add('Incident', null, function (formObj, viewModel) { 
      formObj.boundChange("RequestedWorkItem.employid",function () {
        var employid = viewModel.get("RequestedWorkItem.employid");
      
        $.ajax({
          url: "/api/V3/Projection/GetProjectionByCriteria",
          data: JSON.stringify({
            "Id": "0e1313ab-dc5c-cf9d-d6b0-e2e9835a132a",
            "Criteria": {
              "Base": {
                "Expression": {
                    "SimpleExpression": {
                      "ValueExpressionLeft": {
                        "Property": "$Context/Property[Type='19640a07-0c14-e53a-eda7-4eb50dc365d5']/ffc7b86b-8cb6-6fc8-dac1-774c2ab3ab95$"
                      },
                      "Operator": "Equal",
                      "ValueExpressionRight": {
                      "Value": employid
                      }
                    }
                  }
                }
              }
          }),
          type: "POST",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (data) {
            if(data.length > 0){
              var user = JSON.parse(JSON.stringify(data[0]));
              var au = $('[data-control-bind="RequestedWorkItem"]').data("kendoAutoComplete")
              var userModel = {
                Name: user.DisplayName,
                Id: user.BaseId,
                }
              au.dataSource.add(userModel);
              au.value(userModel.Name);
              au.trigger("change");
            }
            else {
              alert('Not Found');
            }
            },
          error: function(xhr, textStatus, errorThrown){
            alert('request failed');
            alert(errorThrown);
          }
        });
      });
    }); 
  • Geoff_RossGeoff_Ross Cireson Consultant O.G.
    Great. Glad you are sorted. Out of interest, not sure exactly what you are using this for, but it is possible to set the user picker to search other user properties, for example, employeeID. So you can type the ID right into the affected user box and it will autocomplete on found numbers and then fill in the display name once selected. Anyway, glad you got it working, thanks for getting involved in the community.
  • Matthew_DowstMatthew_Dowst Customer IT Monkey ✭
    The client wanted it this way, because a certain subset of their users use these numbers to identify themselves, so they wanted as a separate field. However, if I'm able to allow them to search on it in the people picker, it may be simpler just to make that a read only field. Do you have any guidance on adding additional properties to the user picker? I looked in the KB but I wasn't able to find any information on it. Again thanks for all your help. 
  • Matthew_DowstMatthew_Dowst Customer IT Monkey ✭
    I found the KB for updating the search criteria. 

    https://support.cireson.com/KnowledgeBase/View/1327#/ 
  • Geoff_RossGeoff_Ross Cireson Consultant O.G.
    There's as setting in the Settings Items section called User Query. Admin Settings --> Scroll right down the page --> Settings Item.

    Find the User Query setting and it should contain:
    DisplayName LIKE '%' + @Query + '%' OR UserName LIKE '%' + @Query + '%'
    which is the default settings. Add another OR for your extra property to search. eg
    DisplayName LIKE '%' + @Query + '%' OR UserName LIKE '%' + @Query + '%' OR EmployeeId LIKE '%' + @Query + '%'

    The custom task you build is a great solution but this could just be another way.
  • Matthew_DowstMatthew_Dowst Customer IT Monkey ✭
    I tested this out and it doesn't seem to work with extensions of the user class. The field I am trying to search on is a custom extension of the Microsoft.AD.User class. When I put it in the UserQuery setting, the search does not work. 
Sign In or Register to comment.