Tuesday, August 12, 2008

Set Default Value to a Lookup Attribute using JavaScript


Below Example will set the Default Primary Contact ID for Account Entity.

if(crmForm.all.primarycontactid.DataValue == null)
      
        //Create an array to set as the DataValue for the lookup control.

        var lookupData = new Array();

       //Create an Object add to the array.
        var lookupItem= new Object();
 
       //Set the id, typename, and name properties to the object.
        lookupItem.id = 'SPECIFY GUID of an Entity';
        lookupItem.typename = 'SPECIFY Entity Name';
        lookupItem.name = 'SPECIFY Attribute DISPLAY Value'; 
       // Add the object to the array.
          lookupData[0] = lookupItem; 
      // Set the value of the lookup field to the value of the array.
        crmForm.all.primarycontactid.DataValue = lookupData; 
} 

9 comments:

Anonymous said...

Dear Ranjit,

After looking at your solution to default lookup I am sure You can resolve my issue which is the cause to a lot of hiccups in my customisation. I am a bit new to CRM customization . In the account section when I select the "parent account" lookup it always selects the "Account name", instead my client wants account number to be appearing in the lookup. Is it possible to change the attribute? I guess I have to write a onload script to change the attribute. Can you help me with it?

Anonymous said...

i tried your script however it's not working. i dont know why. is there a step i missed somewhere along the way?

Dynamics 365 Solution Architect | Azure / ALM/DevOps | Power Platform said...

Hi, look at the actual code, which sets the existing contact id to a primary contact lookup attribute in a account entity.


if(crmForm.all.primarycontactid.DataValue == null)
{
//Create an array to set as the DataValue for the lookup control.
var lookupData = new Array();

//Create an Object add to the array.
var lookupItem= new Object();

//Set the id, typename, and name properties to the object.
lookupItem.id = 'C05D603A-BCD0-DD11-8312-0003FFD551E2';
lookupItem.typename = 'contact';
lookupItem.name = 'Test Contact';

// Add the object to the array.
lookupData[0] = lookupItem;

// Set the value of the lookup field to the value of the array.
crmForm.all.primarycontactid.DataValue = lookupData;
}


Please enclose the braces of "if condition".

Cheers,
Ranjit

Anonymous said...

Thanks Ranjit!!! It's working now.

The error i did is i keep on pointing it to the wrong entity!

thank you for all your help!!!

Anonymous said...

Hi Ranjit, I was wondering if you have a script where when the value of the lookup change, it auto-fill the fields on the open entity.

thanks for you help

Unknown said...

Hi Ranjit

I'm trying to adapt your code to pre-select a currency value on the Order form. I have the following code in the form's OnLoad event:

var lookupData = new Array();
var lookupItem= new Object();
lookupItem.id = '1A940A0A-EFC5-DD11-A1C4-0003FF7172D3';
lookupItem.typename = 'transactioncurrencyid';
lookupItem.name = 'Currency';
lookupData[0] = lookupItem;
crmForm.all.transactioncurrencyid.DataValue = lookupData;

The code runs without error, but the value in the Currency look-up isn't changed.

Am I doing something dumb?

cybernyunt said...

Dear Ranjitsingh,
Thanks for your great post. It's save me.

Monte Perdido said...

Ok, you set a lookup with an object whose got a type (Account or contact, ....)
But is it possible de set a lookup with an object whose hasn't got any type ? set a lookup with simple text ...

Thanks !

Anonymous said...

Great help! I probably could have figured it out but it would have taken me a long, long time.