Dynamic, automatic Sugesstion
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
-
//Here we assign an Function to a Field, that will be activated, when you click on the field
-
crmForm.all.new_produkt.attachEvent("onfocus",Produkt);
-
-
//At the first we declare variables
-
//This is the Field, where al this happen.
-
var myfeld_produkt="new_produkt";
-
//We create an Array, which will contain the result of our query.
-
var myarr_produkt = new Array();
-
-
var my_var=null;
-
var my_produkt="1";
-
myarr_produkt=null;
-
var myob_produkt;
-
-
liste("product","statuscode","name","1","new_produkt");
-
-
//This is the Function Produkt, which will be fired on the click event.
-
function Produkt(){
-
liste("product","statuscode","name","1","new_produkt");
-
k = function GetListOfCountries()
-
{
-
-
return myarr_produkt;
-
}
-
//After we get our Data, we take this and put it in the Function SuggestionTextBox, which will create our Suggestion List
-
my_produkt=my_var;
-
myarr_produkt= my_produkt.split(‘,’);
-
myob_produkt= SuggestionTextBox(document.getElementById(myfeld_produkt), k,false);
-
-
}
-
-
Produkt();
-
-
//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"
-
-
//Dont forget to change "myservername"
-
function liste(my_entity,my_query,my_display,my_wert,my_felder)
-
{
-
var templand;
-
var serverUrl = "http://myservername/mscrmservices/2006";
-
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
-
xmlhttp.open("POST", serverUrl + "/crmservice.asmx", false);
-
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8")
-
xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2006/WebServices/RetrieveMultiple")
-
xmlhttp.send("<?xml version=’1.0′ encoding=’utf-8′?>"+"\n\n"+"<soap:Envelope"+
-
‘ xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"’+
-
‘ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"’+
-
‘ xmlns:xsd="http://www.w3.org/2001/XMLSchema">’+
-
‘ <soap:Body>’ +
-
‘ <query xmlns:q1="http://schemas.microsoft.com/crm/2006/Query" xsi:type="q1:QueryExpression" xmlns="http://schemas.microsoft.com/crm/2006/WebServices">’+
-
‘<q1:EntityName>’+my_entity+‘</q1:EntityName>’+
-
‘<q1:ColumnSet xsi:type="q1:ColumnSet">’+
-
‘<q1:Attributes>’+
-
‘<q1:Attribute>’+my_display+‘</q1:Attribute>’+
-
‘</q1:Attributes>’+
-
‘</q1:ColumnSet>’+
-
‘<q1:Distinct>false</q1:Distinct>’+
-
‘<q1:Criteria>’+
-
‘<q1:FilterOperator>And</q1:FilterOperator>’+
-
‘<q1:Conditions>’+
-
‘<q1:Condition>’+
-
‘<q1:AttributeName>’+my_query+‘</q1:AttributeName>’+
-
‘<q1:Operator>Like</q1:Operator>’+
-
‘<q1:Values>’+
-
‘<q1:Value xsi:type="xsd:string">’+my_wert+‘</q1:Value>’+
-
‘</q1:Values>’+
-
‘</q1:Condition>’+
-
‘</q1:Conditions>’+
-
‘</q1:Criteria>’+
-
‘<q1:Orders>’+
-
‘<q1:Order>’+
-
‘<q1:AttributeName>’+my_query+‘</q1:AttributeName>’+
-
‘<q1:OrderType>Ascending</q1:OrderType>’+
-
‘</q1:Order>’+
-
‘</q1:Orders>’+
-
‘</query>’+
-
‘</soap:Body>’+
-
‘ </soap:Envelope>’)
-
var result = xmlhttp.responseXML.xml;
-
//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
-
var my_index=result.indexOf(my_display);
-
if (my_index ==-1)
-
{
-
return;
-
}
-
var BEs= result.split("<BusinessEntities>");
-
-
var BE = BEs[1].split("<BusinessEntity");
-
for (i = 0; i < BE.length; i++)
-
-
{
-
-
first = BE[i].indexOf("<"+my_display+">")+(2+my_display.length);
-
second = BE[i].indexOf("</"+my_display+">");
-
//document.writeln(BE[i].substring(first,second) + ",");
-
-
var tempcount = BE[i].substring(first,second) + ",";
-
if (templand == null)
-
{
-
templand=tempcount;
-
}
-
{
-
templand=templand+tempcount;
-
}
-
-
}
-
my_var=null;
-
my_var=templand;
-
-
}
-
//We have our Result
-
-
-
//No we generate our List
-
-
function SuggestionTextBox(textfield, method, preload)
-
{
-
// Here you can define your max rows.
-
var maxItems = 6;
-
this.suggestionList = new Array();
-
this.suggestionListDisplayed = new Array();
-
var actual_textfield = textfield;
-
var actual_value = ”;
-
var selectedNumber = 0;
-
var countMatches = 0;
-
if (preload)
-
{
-
-
this.suggestionList = method();
-
}
-
textfield.attachEvent("onfocus", initTextfield);
-
-
function initTextfield()
-
{
-
//If you leave the field, all will be deleted
-
textfield.attachEvent("onblur", resetTextfield);
-
document.attachEvent("onkeydown", keyDown);
-
}
-
function resetTextfield(e)
-
{
-
-
document.detachEvent("onkeydown", keyDown);
-
textfield.detachEvent("onblur",resetTextfield);
-
}
-
function keyDown(e)
-
{
-
keyCode = e.keyCode;
-
switch (keyCode)
-
{
-
case 9: case 13:
-
// enter & tab key
-
if (countMatches > 0)
-
{
-
actual_textfield.value = suggestionListDisplayed[selectedNumber];
-
if (document.getElementById(’suggestion_table’) != null)
-
{
-
document.body.removeChild(document.getElementById(’suggestion_table’));
-
}
-
}
-
break;
-
case 38:
-
-
if(selectedNumber > 0 && countMatches > 0)
-
{
-
selectedNumber–;
-
createSuggestionTable();
-
}
-
-
return false;
-
break;
-
case 40:
-
-
if(selectedNumber < countMatches-1 && countMatches > 0 && selectedNumber < maxItems)
-
{
-
selectedNumber++;
-
createSuggestionTable();
-
}
-
-
return false;
-
break;
-
default:
-
// We controll, that the function will not be called to often
-
setTimeout(
-
function()
-
{
-
executeSuggestion(keyCode)
-
}, 200 /* in ms */
-
);
-
break;
-
}
-
}
-
-
function executeSuggestion(keyCode)
-
{
-
selectedNumber = 0;
-
countMatches = 0;
-
-
actual_value = textfield.value;
-
-
-
if (!preload)
-
{
-
-
this.suggestionList = method();
-
}
-
-
var re = new RegExp(actual_value, "i");
-
-
var re = new RegExp("^" + actual_value, "i");
-
-
countMatches = 0;
-
this.suggestionListDisplayed = new Array();
-
-
for (i = 0; i < this.suggestionList.length; i++)
-
{
-
-
if (re.test(this.suggestionList[i]) && actual_value != ”)
-
{
-
this.suggestionListDisplayed[countMatches] = this.suggestionList[i];
-
countMatches++;
-
-
-
if (maxItems == countMatches)
-
break;
-
}
-
}
-
-
if (countMatches > 0)
-
{
-
createSuggestionTable();
-
}
-
else
-
{
-
if (document.getElementById(’suggestion_table’))
-
{
-
document.body.removeChild(document.getElementById(’suggestion_table’));
-
}
-
}
-
}
-
-
-
function createSuggestionTable()
-
{
-
-
if (document.getElementById(’suggestion_table’))
-
{
-
document.body.removeChild(document.getElementById(’suggestion_table’));
-
}
-
-
-
table = document.createElement(‘table’);
-
table.id = ’suggestion_table’;
-
-
table.width = actual_textfield.style.width;
-
table.style.position= ‘absolute’;
-
table.style.zIndex = ‘100000′;
-
-
table.cellSpacing = ‘1px’;
-
table.cellPadding = ‘2px’;
-
-
-
topValue = 0;
-
objTop = actual_textfield;
-
while(objTop)
-
{
-
topValue += objTop.offsetTop;
-
objTop = objTop.offsetParent;
-
}
-
-
table.style.top = eval(topValue + actual_textfield.offsetHeight) + "px";
-
-
leftValue = 0;
-
objLeft = actual_textfield
-
while(objLeft)
-
{
-
leftValue += objLeft.offsetLeft;
-
objLeft = objLeft.offsetParent;
-
}
-
-
table.style.left = leftValue + "px";
-
-
table.style.backgroundColor = ‘#FFFFFF’;
-
table.style.border = "solid 1px #7F9DB9";
-
table.style.borderTop = "none";
-
-
document.body.appendChild(table);
-
-
-
for ( i = 0; i < this.suggestionListDisplayed.length; i++)
-
{
-
row = table.insertRow(-1);
-
-
row.id = ’suggestion_row’ + (i);
-
column = row.insertCell(-1);
-
column.id = ’suggestion_column’ + (i);
-
-
-
if (selectedNumber == i)
-
{
-
column.style.color = ‘#ffffff’;
-
column.style.backgroundColor = ‘#316AC5′;
-
}
-
else
-
{
-
column.style.color = ‘#000000′;
-
column.style.backgroundColor = ‘#ffffff’;
-
}
-
-
column.style.fontFamily = ‘Tahoma’;
-
column.style.fontSize = ‘11px’;a
-
column.innerHTML = this.suggestionListDisplayed[i];
-
-
}
-
}
-
-
-
return this;
-
}
So this is now the whole Code, please be indulgently with my english, and may Variable Names.


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
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.