Auto populate a request offering field
Is it possible to create a piece of js which gets the logged in users domain and inserts it into a request offering text field. This would mean not having to ask the end user to make a choice with regards to their domain and would allow requests which rely on the token: portal username to function properly. This has stemmed from my post in the Service Manager General section
Best Answer
-
Martin_Blomgren Customer Ninja IT Monkey ✭✭✭✭@Alex_Marsh, we have a similar setup were we fetch the needed data via a API call. Changed the code so it should reflect your needs:
<div>(function() { <span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> var inputLabel = "Domain"; </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> var observer = new MutationObserver(function (mutations) { </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> var targetElement = $('.question-container .control-label'); </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> if(targetElement.length > 0) { </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> targetElement.each(function(){ </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> if ($(this).text().indexOf(inputLabel) > -1) { </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> var that = this; </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> $(that).next().find('textarea').val(session.user.Domain).keyup().prop('disabled', true); </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> return false; </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> } </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> });</span></div><div><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> observer.disconnect(); </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> } </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> }); </span></div><div> // Notify me of everything! <span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> var observerConfig = { </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> attributes: true, </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> childList: false, </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> characterData: false, </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> subtree: true </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> }; </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> // Node, config </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> $(document).ready(function () { </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> var targetNode = document.getElementById('main_wrapper'); </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> observer.observe(targetNode, observerConfig); </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"> }); </span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;">})();</span></div>
6
Answers
The next step is going to be to try and identify the text box by it's label (which will be kept consistent across all the required request offerings) and set the value that way and disable the text box as I don't fancy getting the textarea value for each form and checking if it exists!. This is where my js limits are coming in as I'm not sure how I'd find the label for the question then get the textarea value in order to set it and make the field readonly.
Edit: Hmmm that doesn't seem to work actually. The text box gets populated but for some reason the portal think's the field is empty (i.e. when you go to submit when field is required it says it's empty). Back to the drawing board...
Next stop is finding a way of looking up the users manager and providing the option to change that, although I may stick with the query results and a checkbox for that one.
you will need to change the "ID" GUID above to the GUID the projection becomes in your environment.
The above will get the current users class object, then pass the username through to the expression used in the $ajax query. then you can get any of the values associated with the users managers AD class instance and use them however required.
don't forget to import the management pack too