Showing posts with label Lookups. Show all posts
Showing posts with label Lookups. Show all posts

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.

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

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

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