Custom.js: Problems working with "UserPicker" Fields/Objects? (e.g.: RequestedWorkItem)
Am a bit stuck here.. Hope someone can help with a couple of issues here...
1. On the Incident form, I am trying to trigger an event as a result of the user changing the affected user for an incident.
I tried the following code in "Custom.js", but it gives me strange results, where it prevents the entire userpicker from displaying anything, and does not allow the analyst to change the affected user either.
app.custom.formTasks.add('Incident', null, function(formObj, viewModel) {
viewModel.set("RequestedWorkItem", function() {
alert("test");
});
});
I figure there must be a way to properly handle events when the analyst updates a UserPicker object? Any assistance would be much appreciated!
2. Secondly I am having problems referencing data within the "RequestedWorkItem" field.
For example, If I create a button in an attempt to read out current data relating to the "RequestedWorkItem" object (e.g.: "pageForm.viewModel.RequestedWorkItem.<field>"), it will only return back results for the "Affected User" from when the form was last saved.
e.g.:
Form Loads with Affected User = "User X", Click on button to read "RequestedWorkItem", "User X" data returned.
Form Loads with Affected User = "User X", Analyst changes Affected User to "User Y". Click on button to read "RequestedWorkItem", "User X" data is still returned.
What I am trying to achieve:
Form Loads with Affected User = "User X", Analyst changes Affected User to "User Y". Click on button to read "RequestedWorkItem", "User Y" data is returned.
The only way I can retrieve data from "User Y" is to save the form, and then re-load it. (which is not what I am looking for).
Is there a variable I can reference to get the "current" UserPicker data that most recently has changed?
Thanks in advance!
Adrian
Comments
For number 1 try this.
app.custom.formTasks.add('Incident', null, function(formObj, viewModel) {
formObj.boundChange("RequestedWorkItem",function (formObj, viewModel) {
//code here
});
});
You code is setting the value of Requested Work Item to a function which is why its displaying nothing.
Can you share your code for what you are doing as in principle it should work as expected. Try this to test:
Form Loads with Affected User = "User X"
in F12 Developer console type 'pageForm.viewModel.RequestedWorkItem'
You will get the object of User X returned on the screen.
Change Affected User to "User Y" - DON'T SAVE
in F12 Developer console type 'pageForm.viewModel.RequestedWorkItem'
You will get the object of User Y returned on the screen.
Hi Geoff.
For response one, this still didn't work (the code never executes when you change the RequestedWorkItem userPicker). So not sure why that is.
However, I can now see that the "RequestedWorkItem" field is being updated when I check 'pageForm.viewModel.RequestedWorkItem' in the developer tools.
Great help pulling up the developer tools. Makes so much more sense now.
Where I was going wrong is I was trying to read out user attributes, which don't automatically update when you RequestedWorkItem is changed (only the display name, dn and ID change etc.)
Is there a simple way to use the REST API to pull out all of the remaining user attributes?
And finally, is it also possible to use the REST API to re-post user attribute changes? Or is this not possible?
Thanks again!
Adrian
Ok,
Now I am only stuck on how to re-post (change) user attributes back in SCSM.
After taking a look at the UserPicker controller.js I figured out how to pull out the user properties (for those who are interested, see code below)..
I am hoping the API will also allow changing user object properties, otherwise I might need to do it via scripting the creation of a SR which includes the properties to be updated (a bit long winded) :-/
Unless there is another, better way to do it?
function UpdateUserDetails(userGUID) {
var userProps;
var url_getUserProps = "/Search/GetObjectPropertiesByProjection?projectionId=490ab845-b14c-1d91-c39f-bb9e8a350933&id="
//Get User Properties
$.get(url_getUserProps + userGUID, function (data) {
userProps = data[0];
loadTemplate(userProps);
});
function loadTemplate (userProps) {
alert(userProps.FirstName);
//etc..
}
Any additional thoughts would be much appreciated.
Cheers,
Adrian
Got it working (pushing user details back to SCSM) with "/api/V3/Projection/Commit" (yay).
Just doing the final touches now. Once I have finished I will post up the solution for other people to use if they wish.
Thanks for everyone's help!