Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, August 16, 2010

Using Regular Expressions

I had to re-learn (again) reg Exp so thought I would write it down :-)

The idea here is to search for a bit of text in antother:

The /gi option means global and case insensitive

var pattern = //gi
var match = stringToSearch.search( pattern )

If match > -1 a match has been found.

for example:
var stringToSearch = "Is Rob here"
var pattern = /rob/gi
var match = stringToSearch( pattern );

This "should" return 3.

Wednesday, June 23, 2010

Cloning-Entities using Javascript - great posting

http://mscrm4ever.blogspot.com/2008/06/cloning-entity-using-javascript.html

Wednesday, January 21, 2009

Changing the default / availability of options for an account & contact lookup

This is copied from http://umarkhan.wordpress.com/2007/10/22/how-to-change-the-default-entity-in-lookup-window/  (just in case it is ever removed)

For instance, in Opportunity form of Microsoft CRM you want to change the “Potential Customer” field’s default entity from Account to Contact. You need to use the following piece of code in onLoad event of the Opportunity Form.

if ( crmForm.all.customerid != null )
{
    crmForm.all.customerid .setAttribute(”defaulttype”, “2″);
}

Remember to enable the event and then save and publish the opportunity entity.

Following are codes of basic entities of Microsoft CRM 3 ;

  • Account    1
  • Contact     2
  • Lead         4

Limiting the selection List can be done this way:

crmForm.all.customerid.setAttribute(”lookuptypes”, “1″);

Thanks heaps uMar.

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);

Tuesday, February 5, 2008

An Awesome entry on filtering picklists

A much better way that the one included by MS

http://thecrmgrid.wordpress.com/2007/11/13/a-slightly-different-approach-to-dynamic-picklists/#more-12

Thank You!

Monday, January 7, 2008

Forcing field Save

crmForm.all.[fieldName].ForceSubmit=true;

Friday, December 14, 2007

Function Declarations in Javascript

Michael has documented how function declarations work and why really well here

Thank you once again Michael!

Changing Date Colors

The problem with the date field is that they all end up at the INPUT tag level with the same ID. This means that you can't go directly to the date field input element. It is therefore neccessary to traverse through the DOM tree to get to it. This example is for date only fields. I will do another version for date time fields.

//--------------------------------------------------------------------------------------------------
function setDateColors( tFieldName, tColor, tBackgroundColor, tFontWeight ) {
//--------------------------------------------------------------------------------------------------
var tBaseElement = window.document.getElementById( tFieldName );
var tElement = tBaseElement.childNodes[1].childNodes[0].childNodes[0].childNodes[0].style;
tElement.color = tColor;
tElement.backgroundColor = tBackgroundColor;
tElement.fontWeight = tFontWeight;
};


an example of how this might be called is (to make it bold white text on red background):

setDateColors( new_startDate, "#ffffff", "#ff0000", "bold" )

Tuesday, November 20, 2007

Checking a lookup field for null and setting a default value

//functions to be defined in onLoad
// Also requires the function getattribsByLookup - this is detailed elsaewhere on this site.

//----------------------------------------------------------
setLookupDefault = function( entityName, lookupAttrib, defaultValue, attribsToReturn, fieldToSet ) {
//----------------------------------------------------------

//get the lookup value and put it into the lookup field
var resultXml = getattribsByLookup(entityName, lookupAttrib, defaultValue, attribsToReturn );
var idValue = getXMLNodeText( resultXml, "//" + fieldToSet );
var tField = window.document.getElementById( fieldToSet );
var luObject = getLookupObject( idValue, defaultValue, entityName );
tField.DataValue = luObject;

}

//----------------------------------------------------------
getXMLNodeText = function( resultXml, bitToGet ) {
//----------------------------------------------------------
return resultXml.selectSingleNode( bitToGet ).text
};


//Code for object event
// This code sets the lookup field subjectid to 'Unclassified'
// 'Unclassified' is an entry in the subject
// The field subjectid appears on the Case form and is used by the Knowledgebase system


var subjectItem = new Array;
subjectItem = crmForm.all.subjectid.DataValue;

if ( subjectItem == null ) {
var attribsToReturn = ['subjectid', 'title'];
setLookupDefault( 'subject', 'title', 'Unclassified', attribsToReturn, 'subjectid' );
}

Setting the color of a picklist option

//get the field
var list = crmForm.all.prioritycode;

var option = list.options[0]; //select the option to be coloured
option.style.backgroundColor = "#FF0000"; //Set the background color to red.
option.style.color = "#FFFFFF"; //Set the text color to white.

Monday, November 19, 2007

Launching a Workflow Rule from Javascript

This is an absolutely awesome post By Mitch Milam.

http://blogs.infinite-x.net/2007/06/15/launching-a-workflow-rule-from-javascript


The SQL Code for getting the WF ID is
SELECT
processID,
[name]
FROM
wfprocess
WHERE
ProcessTypeCode = 1

Thanks Mitch!

Saturday, November 17, 2007

Showing related entities in an iFrame

Updated for Version 4...

Sometimes I want to show related entities in iFrames on the forms rather than via the LHS navigator. The technique shown below allows you to do this quickly and easily and allows you to have a number of related entities showing this way on the same page.



For this example I will show a related entity called new_city in an iFrame called Cities. In the IFrame properties it is important that restricting scripting is UNticked and that the ID is passed. The IFrame is therefore referenced as crmForm.all.IFRAME_Cities (ignore the old entiy names in the pics below).





I set the contents of the IFrame in the onLoad event using the following line of code and the function shown below




The function that I use to set the contents of the IFrame is included as part of the onLoad for the page (make sure it is loaded before the line above is called)

//----------------------------------------------------------------------------------------------
function getFrameSource( tabSet ) {
//----------------------------------------------------------------------------------------------

if (crmForm.ObjectId != null) {
var oId = crmForm.ObjectId;
var oType = crmForm.ObjectTypeCode;
var security = crmFormSubmit.crmFormSubmitSecurity.value;

//this two lines are optional and used to hide th link on the LHS navigation
var navItem = window.document.getElementById( 'nav_'+ tabSet );
navItem.style.display = 'none';

return "areas.aspx?oId=" + oId + "&oType=" + oType + "&security=" + security + "&tabSet=" + tabSet;

} else {

return "about:blank";

}
};


//Line to in set IFrame contents
crmForm.all.IFRAME_Cities.src = getFrameSource('new_new_state_new_city');


Tuesday, November 13, 2007

Setting Lookup Fields

This function is intended to be included in the onLoad library of a page or a common .js library. Its purpose is to construct the required object/array for setting a lookup value on a form.

An example of The calling function is also shown (using a value retrieved from a xml doc retrieved from another lookup function)

//--------------------------------------------------------------------------------------------------
function getLookupObject( idValue, lookupLabel, typeName ) {
//Create an array to set as the DataValue for the lookup control.
//then Create an Object add to the array.
//Set the attibutes of teh object
//--------------------------------------------------------------------------------------------------
var lookupData = new Array();
var lookupItem= new Object();
//Set the id, typename, and name properties to the object.
lookupItem.id = idValue;
lookupItem.typename = typeName;
lookupItem.name = lookupLabel;
// Add the object to the array.
lookupData[0] = lookupItem;
// Set the value of the lookup field to the value of the array.
return lookupData;
};



Example Usage (to set the lookup field new_departmentid):
if ( resultXml.selectSingleNode("//new_departmentid") != null ) {
f.new_departmentid.DataValue = getLookupObject( resultXml.selectSingleNode("//new_departmentid").text,
resultXml.selectSingleNode("//new_departmentid/@name").text,
'new_department' ) ;
}

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;
}

Friday, October 26, 2007

Debugging Javascript

Want to be able to step through your Javascript? There are two really easy steps...

1. Add: debugger to the top of the onload javbascript code



2. Enable the debugger in your browser...

(Uncheck the IE debug option)

Thursday, October 18, 2007

showing the page source

Enter the following into the address bar:

javascript:alert(document.body.outerHTML)

Wednesday, October 17, 2007

Setting a Lookup Value

This code snippet should be copied into the form onLoad event code so that it is available as a global function for the form.

//--------------------------------------------------------------------------------------------
setlookupValue = function( idValue, textValue, entityName ) {
//---------------------------------------------------------------------------------------------
var lookupItem = new Object();
var lookupData = new Array();
lookupItem.id = idValue;
lookupItem.name = textValue;
lookupItem.typename = entityName;
lookupData[0] = lookupItem;
return( lookupData );
};


It can then be called from within the form (replace the bits in square brackets[ ]):
var f = crmForm.all;
f.[fieldSchemaName].DataValue = setlookupValue( [idValue], [lookupText], [entitySchemaName] );