Showing posts with label fetchXML. Show all posts
Showing posts with label fetchXML. Show all posts

Wednesday, April 9, 2008

Alerting a fetchXML Query

Taken from http://ronaldlemmen.blogspot.com/2006/11/using-advanced-find-for-fetchxml.html

You can just open the Advanced find page and build the query as you like. Then run the query to see if it returns the data as you wish it should. If you are satisfied with the results and you want to know what query was sent into the CRM Framework, then press F11 to get the address bar and enter this script and press enter.



javascript:alert(resultRender.FetchXml.value);


If you get a warning about leaving the page, just press 'ok' and then the query which is sent to the framework opens up in a popup. Unfortunately you cannot select the text to copy and paste. But with Windows XP and Windows Server 2003 you can copy all text on the popup by clicking somewhere on the popup (not on the button "OK" ofcourse) and pressing ctrl+c. Now in notepad you can paste the text of the FetchXML.

Good luck!

Update: Thanks to Piotr in the comment section I have learned the javascript prompt command. Try this instead of the alert:


javascript:prompt("my query:", resultRender.FetchXml.value);

Monday, November 19, 2007

MetaData Services

Another great article by Michael Hohne I didn't want to loose track of.

http://blogs.msdn.com/crm/archive/2007/07/13/advanced-metadata-service-scenarios.aspx

Tuesday, November 13, 2007

Setting Multiple vales from a lookup

This function should be embeded on the form onLoad and then called by the relevant fields onChange event.

The code for getattribsByLookup and getLookupObject can be found here. They should be included either in the forms onLoad function definitins or ina common .js library

//----------------------------------------------------------
setSystemUserInformation = function() {
//----------------------------------------------------------
//setup some vars to use in constructing the formula and storing the values
var f = crmForm.all;
var entityForList = 'systemuser';
var attribForFilter = 'systemuserid' ;
var idToFetch = f.ownerid.DataValue[0].id+ '' ;
var attribsToReturn = ['mobilephone', 'new_departmentid' ];


//clear the existing values
f.new_departmentid.DataValue = "";
f.new_mobilephone.DataValue="";
var resultXml = getattribsByLookup( entityForList, attribForFilter, idToFetch, attribsToReturn );

if ( resultXml.selectSingleNode("//mobilephone") != null ) {
var tempText = resultXml.selectSingleNode("//mobilephone").text;
f.new_mobilephone.DataValue = tempText;
f.new_mobilephone.ForceSubmit=true;
}

if ( resultXml.selectSingleNode("//new_departmentid") != null ) {
f.new_departmentid.DataValue = getLookupObject( resultXml.selectSingleNode("//new_departmentid").text,
resultXml.selectSingleNode("//new_departmentid/@name").text,
'new_department' ) ;
}
};

Friday, November 9, 2007

Caclulating with values from siblings

I have a form where I want to be able to specify a percentage value. I also want to make sure that the total value of all siblings is not more than 100 and to calculate the value based on teh value in the parent document.

In this case it was Operatives for a Deal. My structure is
Deal $20,0000
Operative 1 : 25%
Operative 2: 55%
Operative 3: 20%

What I want to prevent is somebody coming along and changing the value of Operative 3to 30%.
To do this i include this code in the forms onLoad event:

//----------------------------------------------------------
checkPercentage = function() {
//----------------------------------------------------------
var f = crmForm.all;
var retVal = false;


//setup some vars to use in constructing the formula and storing the values
var entityForList = 'new_listingoperative';
var attribForFilter = 'new_dealid' ;
var idToFetch = f.new_dealid.DataValue[0].id+ '' ;
var attribToReturn = 'new_percentage';
var resultXml = getattribByLookup( entityForList, attribForFilter, idToFetch, attribToReturn );
var totalPerc = 0.00;


if ( resultXml.selectSingleNode("//RetrieveMultipleResult") != null ) {
var node = resultXml.selectSingleNode("//RetrieveMultipleResult");
var percNodes = node.selectNodes("//new_percentage");
var idNodes = node.selectNodes("//new_listingoperativeid");

// if this is a new & unsaved form add it to the total before cycling through the return list
var totalPerc = 0.00;
if ( crmForm.FormType == 1 ) { totalPerc += f.new_percentage.DataValue; }

//this list will only include saved forms
for ( i = 0; i < percNodes.length; i++ ) {
if( isCurrentRecord( idNodes[ i ].text ) ) {
totalPerc += f.new_percentage.DataValue;
} else {
totalPerc += parseFloat( percNodes[ i ].text );
}
}
}

if( totalPerc <= 100.00 ) { retVal = true; }
return retVal ;

};

And this code in the fields onChange event
if( !checkPercentage() ) {
alert( 'ERROR: The total of all percentage amounts must be no greater than 100%');
crmForm.all.new_percentage.DataValue = 0.00;
}

Wednesday, November 7, 2007

Getting an attribute from a related entity

This code / function can be used to retrieve a particular field/attribte from related records. It passes back an XML doc which can then be parsed to get the particular value.

I have substituted the <> chars with [ and ] due to the probs of rendering in blogger

//--------------------------------------------------------------------------------------------------
getattribByLookup = function(entityForList, attribForFilter, idToFetch, attribToReturn ) {
//--------------------------------------------------------------------------------------------------

var f = crmForm.all;
var xml = "" +
"[?xml version=\"1.0\" encoding=\"utf-8\"?]" +
"[?xml:namespace prefix = soap /][soap:envelope xsd="\" xsi="\" soap="\"]" +
" [soap:body]" +
" [query xmlns="\" type="\" q1="\"]" +
" [?xml:namespace prefix = q1 /][q1:entityname]" + entityForList + "[/q1:entityname]" +
" [q1:columnset type="\"]" +
" [q1:attributes]" +
" [q1:attribute]" + attribToReturn + "[/q1:attribute]" +
" [/q1:attributes]" +
" [/q1:columnset]" +
" [q1:distinct]true[/q1:distinct]" +
" [q1:criteria]" +
" [q1:filteroperator]And[/q1:filteroperator]" +
" [q1:conditions]" +
" [q1:condition]" +
" [q1:attributename]" + attribForFilter + "[/q1:attributename]" +
" [q1:operator]Equal[/q1:operator]" +
" [q1:values]" +
" [q1:value type="\"]" + idToFetch + "[/q1:value]" +
" [/q1:values]" +
" [/q1:condition]" +
" [/q1:conditions]" +
" [/q1:criteria]" +
" [/query]" +
" [/soap:body]" +
"[/soap:envelope]" +
"";

var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2006/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2006/WebServices/RetrieveMultiple");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
return( resultXml );
};