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

No comments: