4,013 views | View blog reactions

Dynamic, automatic Sugesstion

Posted on January 23rd, 2007 in JSCRIPT by Seidl Michael | 4,013 views

Now its time for something more difficult, on this Site
i saw an interesting example for auto suggestion fields, but the one thing, there are only static Words, i expand
this script, that you can dynamically query a CRM Entity, and take this data for the suggestion fields.

Here, you can see an example, to query your current products in CRM.

JAVASCRIPT [Show Plain Code]:
  1. //Here we assign an Function to a Field, that will be activated, when you click on the field
  2. crmForm.all.new_produkt.attachEvent("onfocus",Produkt);
  3.  
  4. //At the first we declare variables
  5. //This is the Field, where al this happen.
  6. var myfeld_produkt="new_produkt";
  7. //We create an Array, which will contain the result of our query.
  8. var myarr_produkt = new Array();
  9.  
  10. var my_var=null;
  11. var my_produkt="1";
  12. myarr_produkt=null;
  13. var myob_produkt;
  14.  
  15. liste("product","statuscode","name","1","new_produkt");
  16.  
  17. //This is the Function Produkt, which will be fired on the click event.
  18. function Produkt(){
  19. liste("product","statuscode","name","1","new_produkt");
  20. k = function GetListOfCountries()
  21. {
  22.  
  23.     return myarr_produkt;
  24. }
  25. //After we get our Data, we take this and put it in the Function SuggestionTextBox, which will create our Suggestion List
  26. my_produkt=my_var;
  27. myarr_produkt= my_produkt.split(‘,’);
  28. myob_produkt= SuggestionTextBox(document.getElementById(myfeld_produkt), k,false);
  29.  
  30. }
  31.  
  32. Produkt();
  33.  
  34. //This now is the Function Liste, which we talk a above, data we transport are very important my_entity=Schemaname from our Entity, my_query=the field we query, my_display=the field i want to get the result, my_wert=the data which i will query with my_query, my_felder=the field in crm, which will show the data. So a little summary, we query the entity "product", we want the data in the column "name", where "stauscode =1", so all active records and put the list in our field "my_produkt"
  35.  
  36. //Dont forget to change "myservername"
  37. function liste(my_entity,my_query,my_display,my_wert,my_felder)
  38. {
  39.         var templand;
  40.         var serverUrl = "http://myservername/mscrmservices/2006";
  41.         var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  42.         xmlhttp.open("POST", serverUrl + "/crmservice.asmx", false);
  43.         xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8")
  44.         xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2006/WebServices/RetrieveMultiple")
  45.         xmlhttp.send("<?xml version=’1.0′ encoding=’utf-8′?>"+"\n\n"+"<soap:Envelope"+
  46.         ‘ xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"’+
  47.         ‘ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"’+
  48.         ‘ xmlns:xsd="http://www.w3.org/2001/XMLSchema">’+
  49.         ‘ <soap:Body>’ +
  50.         ‘ <query xmlns:q1="http://schemas.microsoft.com/crm/2006/Query" xsi:type="q1:QueryExpression" xmlns="http://schemas.microsoft.com/crm/2006/WebServices">’+
  51.                      ‘<q1:EntityName>’+my_entity+‘</q1:EntityName>’+
  52.                      ‘<q1:ColumnSet xsi:type="q1:ColumnSet">’+
  53.                      ‘<q1:Attributes>’+
  54.                      ‘<q1:Attribute>’+my_display+‘</q1:Attribute>’+
  55.                      ‘</q1:Attributes>’+
  56.                      ‘</q1:ColumnSet>’+
  57.                      ‘<q1:Distinct>false</q1:Distinct>’+
  58.                      ‘<q1:Criteria>’+
  59.                      ‘<q1:FilterOperator>And</q1:FilterOperator>’+
  60.                      ‘<q1:Conditions>’+
  61.                      ‘<q1:Condition>’+
  62.                      ‘<q1:AttributeName>’+my_query+‘</q1:AttributeName>’+
  63.                      ‘<q1:Operator>Like</q1:Operator>’+
  64.                      ‘<q1:Values>’+
  65.                      ‘<q1:Value xsi:type="xsd:string">’+my_wert+‘</q1:Value>’+
  66.                      ‘</q1:Values>’+
  67.                      ‘</q1:Condition>’+
  68.                      ‘</q1:Conditions>’+
  69.                      ‘</q1:Criteria>’+
  70.                      ‘<q1:Orders>’+
  71.                      ‘<q1:Order>’+
  72.                      ‘<q1:AttributeName>’+my_query+‘</q1:AttributeName>’+
  73.                      ‘<q1:OrderType>Ascending</q1:OrderType>’+
  74.                      ‘</q1:Order>’+
  75.                      ‘</q1:Orders>’+
  76.                      ‘</query>’+
  77.                      ‘</soap:Body>’+
  78.         ‘ </soap:Envelope>’)
  79.         var result = xmlhttp.responseXML.xml;
  80. //From here, we have nearly a complete result, the RESULT is now an XML File, al we have to do is delete all XML Tags
  81. var my_index=result.indexOf(my_display);
  82. if (my_index ==-1)
  83. {
  84. return;
  85. }
  86.         var BEs= result.split("<BusinessEntities>");
  87.  
  88.         var BE = BEs[1].split("<BusinessEntity");
  89.         for (i = 0; i < BE.length; i++)
  90.        
  91.                 {
  92.                
  93.                 first = BE[i].indexOf("<"+my_display+">")+(2+my_display.length);
  94.                 second = BE[i].indexOf("</"+my_display+">");
  95.                 //document.writeln(BE[i].substring(first,second) + ",");
  96.                
  97.                 var tempcount = BE[i].substring(first,second) + ",";
  98.                 if (templand == null)
  99.                         {
  100.                         templand=tempcount;
  101.                         }
  102.                         {
  103.                         templand=templand+tempcount;
  104.                         }
  105.                
  106.                 }
  107. my_var=null;       
  108. my_var=templand;
  109.  
  110. }
  111. //We have our Result
  112.  
  113.  
  114. //No we generate our List
  115.  
  116. function SuggestionTextBox(textfield, method, preload)
  117. {
  118.            // Here you can define your max rows.
  119.     var maxItems = 6;
  120.     this.suggestionList = new Array();
  121.     this.suggestionListDisplayed = new Array();
  122.     var actual_textfield = textfield;
  123.     var actual_value = ;
  124.     var selectedNumber = 0;
  125.     var countMatches = 0;
  126.     if (preload)
  127.     {
  128.        
  129.         this.suggestionList = method();
  130.     }
  131.     textfield.attachEvent("onfocus", initTextfield);
  132.    
  133.     function initTextfield()
  134.     {
  135.         //If you leave the field, all will be deleted   
  136.         textfield.attachEvent("onblur", resetTextfield);
  137.         document.attachEvent("onkeydown", keyDown);
  138.     }
  139.     function resetTextfield(e)
  140.     {
  141.        
  142.         document.detachEvent("onkeydown", keyDown);
  143.         textfield.detachEvent("onblur",resetTextfield);
  144.     }
  145.     function keyDown(e)
  146.     {
  147.         keyCode = e.keyCode;
  148.         switch (keyCode)
  149.         {
  150.             case 9: case 13:
  151.                 // enter & tab key
  152.                 if (countMatches > 0)
  153.                 {
  154.                      actual_textfield.value = suggestionListDisplayed[selectedNumber];
  155.                      if (document.getElementById(’suggestion_table’) != null)
  156.                     {
  157.                        document.body.removeChild(document.getElementById(’suggestion_table’));
  158.                     }
  159.                   }
  160.                  break;
  161.             case 38:
  162.                
  163.                 if(selectedNumber > 0 && countMatches > 0)
  164.                 {
  165.                     selectedNumber–;
  166.                     createSuggestionTable();
  167.                 }
  168.                
  169.                 return false;
  170.                 break;
  171.             case 40:
  172.                
  173.                 if(selectedNumber < countMatches-1 && countMatches > 0 && selectedNumber < maxItems)
  174.                 {
  175.                     selectedNumber++;
  176.                        createSuggestionTable();
  177.                 }
  178.                
  179.                 return false;
  180.                 break;               
  181.             default:
  182.                 // We controll, that the function will not be called to often
  183.                 setTimeout(
  184.                             function()
  185.                             {
  186.                                 executeSuggestion(keyCode)
  187.                             }200 /* in ms */
  188.                            );
  189.                 break;
  190.         }
  191.     }
  192.  
  193.     function executeSuggestion(keyCode)
  194.     {
  195.         selectedNumber = 0;
  196.         countMatches = 0;
  197.        
  198.         actual_value = textfield.value;
  199.        
  200.    
  201.         if (!preload)
  202.         {
  203.  
  204.             this.suggestionList = method();
  205.         }
  206.        
  207.         var re = new RegExp(actual_value, "i");
  208.  
  209.         var re = new RegExp("^" + actual_value, "i");
  210.                
  211.         countMatches = 0;
  212.         this.suggestionListDisplayed = new Array();
  213.  
  214.         for (i = 0; i < this.suggestionList.length; i++)
  215.         {
  216.  
  217.             if (re.test(this.suggestionList[i]) && actual_value != )
  218.             {
  219.                 this.suggestionListDisplayed[countMatches] = this.suggestionList[i];
  220.                 countMatches++;
  221.                
  222.  
  223.                 if (maxItems == countMatches)
  224.                     break;
  225.             }
  226.         }
  227.        
  228.         if (countMatches > 0)
  229.         {
  230.             createSuggestionTable();
  231.         }
  232.         else
  233.         {
  234.             if (document.getElementById(’suggestion_table’))
  235.             {
  236.                 document.body.removeChild(document.getElementById(’suggestion_table’));
  237.             }
  238.         }
  239.     }
  240.    
  241.    
  242.     function createSuggestionTable()
  243.     {
  244.        
  245.         if (document.getElementById(’suggestion_table’))
  246.         {
  247.             document.body.removeChild(document.getElementById(’suggestion_table’));
  248.         }
  249.        
  250.  
  251.         table = document.createElement(‘table’);
  252.         table.id = ’suggestion_table’;
  253.        
  254.         table.width = actual_textfield.style.width;
  255.         table.style.position= ‘absolute’;
  256.         table.style.zIndex = ‘100000′;
  257.  
  258.         table.cellSpacing = ‘1px’;
  259.         table.cellPadding = ‘2px’;
  260.  
  261.  
  262.         topValue = 0;
  263.         objTop = actual_textfield;
  264.         while(objTop)
  265.         {
  266.             topValue += objTop.offsetTop;
  267.             objTop = objTop.offsetParent;
  268.         }
  269.        
  270.         table.style.top = eval(topValue + actual_textfield.offsetHeight) + "px";
  271.  
  272.         leftValue = 0;
  273.         objLeft = actual_textfield
  274.         while(objLeft)
  275.         {
  276.             leftValue += objLeft.offsetLeft;
  277.             objLeft = objLeft.offsetParent;
  278.         }
  279.  
  280.         table.style.left = leftValue + "px";
  281.        
  282.         table.style.backgroundColor = ‘#FFFFFF’;
  283.         table.style.border = "solid 1px #7F9DB9";
  284.         table.style.borderTop = "none";
  285.        
  286.         document.body.appendChild(table);
  287.        
  288.      
  289.         for ( i = 0; i < this.suggestionListDisplayed.length; i++)
  290.         {
  291.                 row = table.insertRow(-1);
  292.                
  293.                 row.id = ’suggestion_row’ + (i);
  294.                 column = row.insertCell(-1);
  295.                 column.id = ’suggestion_column’ + (i);
  296.                
  297.                
  298.                 if (selectedNumber == i)
  299.                 {
  300.                     column.style.color = ‘#ffffff’;
  301.                     column.style.backgroundColor = ‘#316AC5′;
  302.                 }
  303.                 else
  304.                 {
  305.                     column.style.color = ‘#000000′;
  306.                     column.style.backgroundColor = ‘#ffffff’;
  307.                 }
  308.                
  309.                 column.style.fontFamily = ‘Tahoma’;
  310.                 column.style.fontSize = ‘11px’;a
  311.                 column.innerHTML = this.suggestionListDisplayed[i];
  312.                
  313.         }
  314.     }
  315.  
  316.  
  317.     return this;
  318. }

So this is now the whole Code, please be indulgently with my english, and may Variable Names.

2 Responses to 'Dynamic, automatic Sugesstion'

Subscribe to comments with RSS or TrackBack to 'Dynamic, automatic Sugesstion'.

  1. Marcel said,

    on 2007-09-10 at 4.29 pm

    Hallo,

    da ich ein Neuling im Umgang mit Webservices und im MS CRM Umfeld bin hätte ich eine Frage:

    Wenn ich den oben hinterlegten Code im onLoad Event eingebe (myservername hab ich durch meinen Servernamen ersetzt und das Feld new_produkt habe ich hinzugefügt) bekomme ich zwei Fehlermeldungen

    1.
    Zeile: 473
    Zeichen: 72
    Fehler: Objekt erwartet
    Code: 0
    URL: http://“xxxxxx”/tools/fromEditor/preview.aspx

    und 2.

    Zeile: 501
    Zeichen: 33
    Fehler: Ungültiges Zeichen
    Code: 0
    URL: http://“xxxxxx”/tools/fromEditor/preview.aspx

    Wenn du mir sagen könntest was ich falsch mache bzw. vergessen habe wäre ich sehr dankbar.

    mfg Marcel


  2. on 2007-11-26 at 10.30 pm

    Ich werde in kurze den gesamten Code per Download als TXT Fiel anbieten, da es anscheinend Probleme mit der HTML Umsetzung des Codes gibt.