0

I inherited a half baked o365 travel request site that I need to bring up ASAP. The only JavaScript knowledge I have is what I have come across online trying to resolve this issue.

Goal: User goes to site, types in travel info, request goes down manager tree to for approval.

Main page: Wiki page, with 2 links - Request and Approve

Flow: User goes to the "how many levels of approval" dropdown and selects 2 (others are hidden). They populate the Manager 1 name field, which resolves to their AD name. Then Manager 2 should pull from the "Approvers list" the name that is listed there. The workflow then emails each manager through the workflow as they approve via the appovers page.

Problem: When the last manager field populates via JS, it does not show up in the approvers list but the lower managers do. If I stop the JS, I can type in that field without an issue. The field needs to resolve the name to AD like the lower managers so when the workflow runs down to the last manager it can email them, currently it does not since the field is blank. When field does populate on the requester page with the text from the approvers list, it acts as it is read only. This issue is with any number selected, 1-4.

I have been flying blind here trying to piece what I have found on here and YouTube but nothing has worked or I just don't understand JS. Let me know if you need more information.

Thank for your time

<script type="text/javascript" src="https://sharepointsite/teams/Travel/All%20Scripts/Project/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://sharepointsite/teams/Travel/All%20Scripts/Project/jquery.SPServices-2014.02.min.js"></script>
<script type="text/javascript" src="https://sharepointsite/teams/Travel/All%20Scripts/Project/accounting.min.js"></script>
<script type="text/javascript">
    var numOfApprovers = 0;
    var lastmanager = "Manager 1";
    var str = "Last Approver";
    var thefield = "";

$(function(){
         SP.SOD.executeFunc('sp.js', 'SP.ClientContext', ChangeSaveButtonText);
/*************************Hide Fields****************************************/
    //  alert("Dear Requester, Please make sure you have the following information before starting entering your request: \n (1)-All managers name \n (2)-Travel estimates ");
        $('nobr:contains("Manager 1")').closest('tr').hide();
        $('nobr:contains("Manager 2")').closest('tr').hide();
        $('nobr:contains("Manager 3")').closest('tr').hide();
        $('nobr:contains("Manager 4")').closest('tr').hide();
    /********************************************************/
    $("select[Title$='Travel Type Required Field']").change(function(){

        //alert($("select[Title$='Travel Type Required Field']").val() + $("input[Title$='Number of Work Days']").val());
        var tmp = 0,  tmp1 = 0;
                    tmp = $("input[Title$='Number of Work Days Required Field']").val();
         if($(this).val() == 'Domestic'){  
                    $("input[Title$='Estimated Cost']").val(accounting.formatMoney(250*tmp));
                    tmp1 = parseInt(250*tmp) ; tmp = parseInt(500);  
                    $("input[Title$='Estimated Total Cost']").val(accounting.formatMoney(tmp1+ tmp));
         }
         if($(this).val() == 'International'){
              $("input[Title$='Estimated Cost']").val(accounting.formatMoney(1000*tmp));
              tmp1 = accounting.formatMoney(parseInt(1000*tmp)+1500);
              $("input[Title$='Estimated Total Cost']").val(tmp1);
         } 
         if($(this).val() == 'Within Asia'){
            $("input[Title$='Estimated Cost']").val(accounting.formatMoney(250*tmp));
            tmp1 = accounting.formatMoney(parseInt(250*tmp)+500);
            $("input[Title$='Estimated Total Cost']").val(tmp1);
         }  

         $("input[Title$='Estimated Cost']").attr("readonly", "true");
         $("input[Title$='Estimated Total Cost']").attr("readonly", "true");
    });

        /****************************Store number of approvers*****************/
        $("select[Title$='How Many Levels Of Approval? Required Field']").change(function(){

            numOfApprovers = $("select[Title$='How Many Levels Of Approval? Required Field']").val();

            switch(numOfApprovers){ 
                case '1':                        
                    $('nobr:contains("Manager 1")').closest('tr').show();
                    $('nobr:contains("Manager 2")').closest('tr').hide();
                    $('nobr:contains("Manager 3")').closest('tr').hide();   
                    $('nobr:contains("Manager 4")').closest('tr').hide();   
                    SetLastApprover();
                    break;
                case '2':       

                    $('nobr:contains("Manager 1")').closest('tr').show();
                    $('nobr:contains("Manager 2")').closest('tr').show();
                    $('nobr:contains("Manager 3")').closest('tr').hide();   
                    $('nobr:contains("Manager 4")').closest('tr').hide();
                        SetLastApprover2();
                    break;
                case '3':                                   
                    $('nobr:contains("Manager 1")').closest('tr').show();
                    $('nobr:contains("Manager 2")').closest('tr').show();   
                    $('nobr:contains("Manager 3")').closest('tr').show();   
                    $('nobr:contains("Manager 4")').closest('tr').hide();
                        SetLastApprover3();
                    break;
                case '4':                                   
                    $('nobr:contains("Manager 1")').closest('tr').show();
                    $('nobr:contains("Manager 2")').closest('tr').show();   
                    $('nobr:contains("Manager 3")').closest('tr').show();   
                    $('nobr:contains("Manager 4")').closest('tr').show();
                        SetLastApprover4();
                    break;
                default:
                    break;  
            }
        });

        /**************************On Saving**********************/
         var buton = $("input[id$='SaveItem']"); 
    buton.click(function(){     
         var nApr = $("select[Title$='How Many Levels Of Approval? Required Field']").val();
        // alert($("div[Title= 'Manager 1']").text() + '\n' + nApr);     
        switch(nApr){
                case '2':                                                       
                        if($("div[Title= 'Manager 1']").text() == "Enter a name or email address..."){
                            alert("Please provide a name for Manager 1 field. ");                               
                        $("input[title='Manager 1']").focus();
                         return false;
                        }
                    break;  
                case '3':                                                       
                        if($("div[Title= 'Manager 1']").text() == "Enter a name or email address..."){
                            alert("Please provide a name for Manager 1 field. ");                               
                        $("input[title='Manager 1']").focus();
                         return false;
                        }
                        if($("div[Title= 'Manager 2']").text() == "Enter a name or email address..."){
                            alert("Please provide a name for Manager 2 field. ");                               
                        $("input[title='Manager 2']").focus();
                         return false;
                        }
                    break;  
                case '4':                                                       
                        if($("div[Title= 'Manager 1']").text() == "Enter a name or email address..."){
                            alert("Please provide a name for Manager 1 field. ");                               
                        $("input[title='Manager 1']").focus();
                         return false;
                        }
                        if($("div[Title= 'Manager 2']").text() == "Enter a name or email address..."){
                            alert("Please provide a name for Manager 2 field. ");                               
                        $("input[title='Manager 2']").focus();
                         return false;
                        }
                        if($("div[Title= 'Manager 3']").text() == "Enter a name or email address..."){
                            alert("Please provide a name for Manager 3 field. ");                               
                        $("input[title='Manager 3']").focus();
                         return false;
                        }
                    break;          
                default:
                    break;
        }
    });         
});
/*******************************************************/
SetLastApprover = function(){   
SP.SOD.executeFunc('sp.js', 'SP.ClientContext',function doLoad(){});
        var  clientContext = SP.ClientContext.get_current();             
          var  list = clientContext.get_web().get_lists().getByTitle('Approvers List');

          var query = new SP.CamlQuery();
            query.set_viewXml("<View>"
                                + "<Query>"
                                + "  <OrderBy>"
                                + "   <FieldRef Name='ID' />"
                                + "  </OrderBy>"                                                                    
                                + "</Query>"
                                + "<ViewFields>"
                                + "      <FieldRef Name='ID' />"
                                + "     <FieldRef Name='Title' />"                              
                                + "     <FieldRef Name='Approver_x0020_Name' />"    
                                + "     <FieldRef Name='Approval_x0020_Level' />"
                                + "     <FieldRef Name='approver' />"
                                + "</ViewFields>"
                                + "<RowLimit>5000</RowLimit>"
                                + "</View>");

            this.pendingItems = list.getItems(query);
            clientContext.load(this.pendingItems);      

            clientContext.executeQueryAsync(Function.createDelegate(this, this.onLoadSucceeded), Function.createDelegate(this, this.onLoadFailed)); 
}   
onLoadSucceeded = function(){           
 var listEnumerator = this.pendingItems.getEnumerator();
while(listEnumerator.moveNext()){ 
                var  lstItm = listEnumerator.get_current();
                var item = listEnumerator.get_current().get_fieldValues();

             if(item.Approval_x0020_Level== str){ 
                $("div[Title= 'Manager 1']").text(lstItm.get_item('Approver_x0020_Name').get_lookupValue());
            }

}

}
onLoadFailed = function(){  
 alert('From   NewForm.aspx: Unable to load Approvers list. ' + args.get_message() + '\n' + args.get_stackTrace());
}   

/*******************************************************/
SetLastApprover2 = function(){  
SP.SOD.executeFunc('sp.js', 'SP.ClientContext',function doLoad(){});
        var  clientContext = SP.ClientContext.get_current();             
          var  list = clientContext.get_web().get_lists().getByTitle('Approvers List');

          var query = new SP.CamlQuery();
            query.set_viewXml("<View>"
                                + "<Query>"
                                + "  <OrderBy>"
                                + "   <FieldRef Name='ID' />"
                                + "  </OrderBy>"                                                                    
                                + "</Query>"
                                + "<ViewFields>"
                                + "     <FieldRef Name='ID' />"
                                + "     <FieldRef Name='Title' />"                              
                                + "     <FieldRef Name='Approver_x0020_Name' />"    
                                + "     <FieldRef Name='Approval_x0020_Level' />"
                                + "     <FieldRef Name='approver' />"
                                + "</ViewFields>"
                                + "<RowLimit>5000</RowLimit>"
                                + "</View>");

            this.pendingItems = list.getItems(query);
            clientContext.load(this.pendingItems);      

            clientContext.executeQueryAsync(Function.createDelegate(this, this.onLoadSucceeded2), Function.createDelegate(this, this.onLoadFailed2)); 
}   
onLoadSucceeded2 = function(){              
 var listEnumerator = this.pendingItems.getEnumerator();
while(listEnumerator.moveNext()){ 
                var  lstItm = listEnumerator.get_current();
                var item = listEnumerator.get_current().get_fieldValues();

             if(item.Approval_x0020_Level== str){ 
                $("div[Title= 'Manager 2']").text(lstItm.get_item("Approver_x0020_Name").get_lookupValue());
            }

}

}
onLoadFailed2 = function(){ 
     alert('From   NewForm.aspx: Unable to load Approvers list. ' + args.get_message() + '\n' + args.get_stackTrace());
}   
/*******************************************************/
SetLastApprover3 = function(){  
  SP.SOD.executeFunc('sp.js', 'SP.ClientContext',function doLoad(){});
        var  clientContext = SP.ClientContext.get_current();             
          var  list = clientContext.get_web().get_lists().getByTitle('Approvers List');

          var query = new SP.CamlQuery();
            query.set_viewXml("<View>"
                                + "<Query>"
                                + "  <OrderBy>"
                                + "   <FieldRef Name='ID' />"
                                + "  </OrderBy>"                                                                    
                                + "</Query>"
                                + "<ViewFields>"
                                + "      <FieldRef Name='ID' />"
                                + "     <FieldRef Name='Title' />"                              
                                + "     <FieldRef Name='Approver_x0020_Name' />"    
                                + "     <FieldRef Name='Approval_x0020_Level' />"
                                + "     <FieldRef Name='approver' />"
                                + "</ViewFields>"
                                + "<RowLimit>5000</RowLimit>"
                                + "</View>");

            this.pendingItems = list.getItems(query);
            clientContext.load(this.pendingItems);      

            clientContext.executeQueryAsync(Function.createDelegate(this, this.onLoadSucceeded3), Function.createDelegate(this, this.onLoadFailed3)); 
}   
onLoadSucceeded3 = function(){              
     var listEnumerator = this.pendingItems.getEnumerator();
    while(listEnumerator.moveNext()){ 
                    var  lstItm = listEnumerator.get_current();
                    var item = listEnumerator.get_current().get_fieldValues();

                 if(item.Approval_x0020_Level== str){ 
                    $("div[Title= 'Manager 3']").text(lstItm.get_item("Approver_x0020_Name").get_lookupValue());
                }

    }

}
onLoadFailed3 = function(){ 
     alert('From   NewForm.aspx: Unable to load Approvers list. ' + args.get_message() + '\n' + args.get_stackTrace());
}   
/*******************************************************/
SetLastApprover4 = function(){  
  SP.SOD.executeFunc('sp.js', 'SP.ClientContext',function doLoad(){});
            var  clientContext = SP.ClientContext.get_current();             
              var  list = clientContext.get_web().get_lists().getByTitle('Approvers List');

              var query = new SP.CamlQuery();
                query.set_viewXml("<View>"
                                    + "<Query>"
                                    + "  <OrderBy>"
                                    + "   <FieldRef Name='ID' />"
                                    + "  </OrderBy>"                                                                    
                                    + "</Query>"
                                    + "<ViewFields>"
                                    + "      <FieldRef Name='ID' />"
                                    + "     <FieldRef Name='Title' />"                              
                                    + "     <FieldRef Name='Approver_x0020_Name' />"    
                                    + "     <FieldRef Name='Approval_x0020_Level' />"
                                    + "     <FieldRef Name='approver' />"
                                    + "</ViewFields>"
                                    + "<RowLimit>5000</RowLimit>"
                                    + "</View>");

                this.pendingItems = list.getItems(query);
                clientContext.load(this.pendingItems);      

                clientContext.executeQueryAsync(Function.createDelegate(this, this.onLoadSucceeded4), Function.createDelegate(this, this.onLoadFailed4)); 
}   
onLoadSucceeded4 = function(){              
     var listEnumerator = this.pendingItems.getEnumerator();
    while(listEnumerator.moveNext()){ 
                    var  lstItm = listEnumerator.get_current();
                    var item = listEnumerator.get_current().get_fieldValues();

                 if(item.Approval_x0020_Level== str){ 
                    $("div[Title= 'Manager 4']").text(lstItm.get_item("Approver_x0020_Name").get_lookupValue());
                }

    }

}
onLoadFailed4 = function(){ 
     alert('From   NewForm.aspx: Unable to load Approvers list. ' + args.get_message() + '\n' + args.get_stackTrace());
}   
/*************************************Change button name*******************************/
function ChangeSaveButtonText()
{
var inputcontrols = document.getElementsByTagName("input");
for(i = 0; i<inputcontrols.length; i++)
{
if(inputcontrols[i].type == "button" && inputcontrols[i].value == "Save")
inputcontrols[i].value = "Submit";
}
}
</script>

enter image description here

This is the dropdown, as you can see Manager 3 is underlined and workes fine in the workflow but Manager 4 which is what JS is pulling from the "Aprovers List" is not resolved and will not work with the workflow as an email address. Manager 4 or any of the "last managers" has this same problem.

How do I get this field to resolve to an email account? All Manager fields and the Approval List Name field are "people" Dropdown with typed and JS entered fields

1
  • @fuetty I added a screenshot of the two fields. You can see the first one is underlined and the second which comes from the JS running is not. I can now see all the console information but I do not know what to look for. What should I pull out of it to post? Feb 17 '17 at 17:31
0

Maybe you could provide little more info/ screenshots, on how it should look and what not working. It helps to understand.

I checked the code and - besides its not very well written - it looks ok. But I since I don't have the SharePoint Site, I cant check if there is an error in fieldnames.

Since you seem not to get any errors (alerts) I guess the code works. So from my perspective I would investigate this part a little more in detail.

onLoadSucceeded4 = function(){              
 var listEnumerator = this.pendingItems.getEnumerator();
while(listEnumerator.moveNext()){ 
                var  lstItm = listEnumerator.get_current();
                var item = listEnumerator.get_current().get_fieldValues();

             if(item.Approval_x0020_Level== str){ 
                $("div[Title= 'Manager 4']").text(lstItm.get_item("Approver_x0020_Name").get_lookupValue());
            }

}

}

You could update the code to this, and press F12 on your browser and check if th

onLoadSucceeded4 = function(){              
 var listEnumerator = this.pendingItems.getEnumerator();
while(listEnumerator.moveNext()){ 
                var  lstItm = listEnumerator.get_current();
                var item = listEnumerator.get_current().get_fieldValues();
                console.log("current approver: ", item);
                 console.log("Approver level: ", item.Approval_x0020_Level);
             if(item.Approval_x0020_Level== str){
                console.log("last approver: ", lstItm.get_item("Approver_x0020_Name").get_lookupValue());
                $("div[Title= 'Manager 4']").text(lstItm.get_item("Approver_x0020_Name").get_lookupValue());
            }

}

}

If this all works out, try to investigate the difference between the "SetlLastAppover" 1-n functions.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.