Home Analyst Portal

Auto populate a request offering field

Alex_MarshAlex_Marsh Premier Partner Advanced IT Monkey ✭✭✭
Bit of a strange scenario here. Have a customer currently who's using a resource domain (shudder) to host their SCSM environment with limited trusts between the 2 domains containing the users (everything must go through the resource domain). Due to a bit of an oversight on their part there are users in each domain which have identical samaccountnames.

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

Answers

  • Alex_MarshAlex_Marsh Premier Partner Advanced IT Monkey ✭✭✭
    edited August 2017
    Right, some progress already! I can auto-populate the box by using the following js
    $(document).ready(function () {
    var domain = session.user.Domain;
    var exists = document.getElementById("textArea7570cee3-cadd-46dd-bbb4-07a3239a1a14")
    if(exists){
    document.getElementById("textArea7570cee3-cadd-46dd-bbb4-07a3239a1a14").value = domain;}
    });
    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...
  • Alex_MarshAlex_Marsh Premier Partner Advanced IT Monkey ✭✭✭
    Awesome, that does the trick. Thanks @Martin_Blomgren

  • Alex_MarshAlex_Marsh Premier Partner Advanced IT Monkey ✭✭✭
    @Martin_Blomgren is there any way of modifying this for multiple properties? I need to get some additional values from the /api/V3/User/GetUserRelatedInfoByUserID and insert these into specific fields. I can't seem to be able to modify the above to do this
  • Alex_MarshAlex_Marsh Premier Partner Advanced IT Monkey ✭✭✭
    @Martin_Blomgren I've managed to get the following but I can't seem to feed it into the above. Any ideas how I would go about this?

    var dept;
        $.get('/Search/GetObjectProperties', { "id": session.user.Id }, 
    	function (data) {
    	var dept = data.Department
    	console.log(dept)}
    	);

  • Alex_MarshAlex_Marsh Premier Partner Advanced IT Monkey ✭✭✭
    Got it sorted after much perseverance. Narrowed the scope of the script so only runs on a specified request offering and I dare say there is some room for improvement but what I've tried to do (where logically it makes sense) is name the questions after the returned api values so they can be parsed without any translation.

    (function() {
      var inputLabel = ["Department","Job Title","Phone","E-Mail"];
      var observer = new MutationObserver(function (mutations) {
      var targetElement = $('.question-container .control-label');
      var userinfo;
      $.get('/Search/GetObjectProperties', { "id": session.user.Id }, 
    function (data) {
    var userinfo = data;
      for (i = 0; i < inputLabel.length; i++){
      if(targetElement.length > 0) {  
         targetElement.each(function(){
           if ($(this).text().indexOf(inputLabel[i]) > -1) {
             var that = this;
    		 var label = inputLabel[i]
    		 if (label == "Job Title") {label = "Title"};
    		 if (label == "Phone") {label = "Business Phone"};
    		 //console.log(label);
    		 if (label == "E-Mail")
    		 {var a = session.user.Email} else {
    		 var a = userinfo[label]};
    		 $(that).next().find('textarea').val(a).keyup();
    		 return false;
           }})
      }}});
          observer.disconnect();
        }
      );
    
      // Notify me of everything!
      var observerConfig = {
        attributes: true,
        childList: false,
        characterData: false,
        subtree: true
      };
    
      // Node, config
      $(document).ready(function () {
    	  if(document.URL.indexOf("3ccc5550-8d80-8bd8-650b-20e089677c41,dc68026c-7025-fbdf-18de-06ce70498ff3") > -1){
        var targetNode = document.getElementById('main_wrapper');
    	  observer.observe(targetNode, observerConfig)};
      });
    })();
    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.
  • Jeff_LangJeff_Lang Customer Ninja IT Monkey ✭✭✭✭
    @Alex_Marsh since I'll be grabbing some code you pasted there, here's a quick easy way to look up a users manager. I've attached a Type Projection management Pack, which just links the manager to the user. you can use this within the js on the portal with something similar to the following

    $.get("/api/V3/User/GetUserRelatedInfoByUserId", { userId: session.user.Id },<br>  function (data) {<br>    if (data.length > 0) {<br>      var userobj = jQuery.parseJSON(data);<br>      $.ajax({<br>        url: "/api/V3/Projection/GetProjectionByCriteria",<br>        data: JSON.stringify({<br>          "Id": "90bc5c2b-c7b9-71f3-a4d8-3acaa565f9ab",<br>          "Criteria": {<br>            "Base": {<br>              "Expression": {<br>                "And": {<br>                  "Expression": [{<br>                    "SimpleExpression": {<br>                      "ValueExpressionLeft": {"Property": "$Context/Property[Type='10a7f898-e672-ccf3-8881-360bfb6a8f9a']/UserName$"},<br>                      "Operator": "Equal",<br>                      "ValueExpressionRight": {"Value": userobj.UserName}<br>                    }<br>                  }]<br>                }<br>              }<br>            }<br>          }<br>        }),<br>        type: "Post",<br>        contentType: "application/json; charset=utf-8",<br>        dataType: "json",<br>        success: function (data) {<br>          ** do other stuff in here<br>          ** access class fields via data[0].Manager.XXXXXX<br>          ** E.G. data[0].Manager.DisplayName<br>          ** You may need to stringify some items<br>          ** EG - var usermgr = JSON.stringify(data[0].Manager.DisplayName);<br>        }<br>      });<br>    }<br>},"json");<br><br>
    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

Sign In or Register to comment.