locked
Javascript set Lookup values issue RRS feed

  • Question

  • Hi Forum!

    I am currently having an issue when trying to populate a lookup field using java script based on a selection from another lookup field.

    We have two fields:

     - The "Existing Product" field where a product is selected by the user.

    - The "Xero Account Code" field where a Xero account is selected based upon the previous fields selection.

    It is almost working, the xero account field is being assigned to the correct accounts however when they are assigned it looks like this:

    Where as it should look like this:

    When in the incorrect state I have found that the link to the Xero Account Entity does not work and the form is also unsaveable.

    Here is the code I am running. It is currently being run on the onChange event of the Existing Product field:

    function setLookupValue(fieldName, id, name, entityType)
    {
    	if(fieldName != null)
    	{
    		// Set variables of lookup to provided ones
    		var lookupValue = new Array();
    		lookupValue[0] = new Object();
    		lookupValue[0].id = id;
    		lookupValue[0].name = name;
    		lookupValue[0].entityType = entityType;
    		
    		// Set the lookup on the page
    		Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
    	}
    }
    
    function setSite()
    {
    	// Set the default lookup value to cardiff site
    	setLookupValue("siteid", "{589ECE5B-676D-E111-AF0D-1CC1DE6E4B49}", "Cardiff", "Site");
    }
    
    function setXeroAccount()
    {
    	// Set up function variables
    	var field = "mag_xeroaccountcodeid";
    	var entityType = "Xero Account Code";
    	
    	// grab the existing product fields lookup
    	var product = new Array();
    	product = Xrm.Page.getAttribute("productid").getValue();
    
    	// wipe the old lookup
    	Xrm.Page.getAttribute("mag_xeroaccountcodeid").setValue(null);
    	
    	if(product != null)
    	{
    		var name = product[0].name;
    	
    		if(name == "Security Deposit")
    			setLookupValue(field, "{FF9CA7CB-76D7-E111-915E-984BE1731321}", "42200", entityType);
    
    		if(name == "Ute")
    			setLookupValue(field, "{BF6E17DA-36DC-E111-BC3F-984BE1731321}", "42140", entityType);
    			
    		if(name == "Van")
    			setLookupValue(field, "{BF6E17DA-36DC-E111-BC3F-984BE1731321}", "42150", entityType);
    			
    		if(name.indexOf("Pantech") != -1)
    			setLookupValue(field, "{F89CA7CB-76D7-E111-915E-984BE1731321}", "42120", entityType);
    			
    		if(name == "Excess Klm")
    			setLookupValue(field, "{FC9CA7CB-76D7-E111-915E-984BE1731321}", "42160", entityType);
    		
    		if(name.indexOf("People Mover") != -1)
    			setLookupValue(field, "{F99CA7CB-76D7-E111-915E-984BE1731321}", "42130", entityType);
    		
    		if(name == "Sedan")
    			setLookupValue(field, "{F79CA7CB-76D7-E111-915E-984BE1731321}", "42110", entityType);
    	}
    }

    Any assistance, suggestions or improvements are greatly appreciated. Thanks in advance.

    - Spike

    Thursday, August 2, 2012 2:34 AM

Answers

  • Hi Spike,

    The entityType property accepts the entity schema name (not display name) so instead of:

    var entityType = "Xero Account Code";

    You should use:

    var entityType = "mag_xeroaccountcode";

    Hope that helps!

    -Paul


    Thursday, August 2, 2012 4:10 AM
  • Hello Spike,

    You should fix line

    var entityType = "Xero Account Code";

    You should put logical name of entity to this variable and your code should look like

    var entityType = "mag_xeroaccountcode";


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    • Marked as answer by Spike Tickner Thursday, August 2, 2012 4:34 AM
    Thursday, August 2, 2012 4:20 AM
    Moderator

All replies

  • Hi Spike,

    The entityType property accepts the entity schema name (not display name) so instead of:

    var entityType = "Xero Account Code";

    You should use:

    var entityType = "mag_xeroaccountcode";

    Hope that helps!

    -Paul


    Thursday, August 2, 2012 4:10 AM
  • Hello Spike,

    You should fix line

    var entityType = "Xero Account Code";

    You should put logical name of entity to this variable and your code should look like

    var entityType = "mag_xeroaccountcode";


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    • Marked as answer by Spike Tickner Thursday, August 2, 2012 4:34 AM
    Thursday, August 2, 2012 4:20 AM
    Moderator
  • Thank you Paul, spot on. I had been tricked by the previous simple function I wrote that was my first time doing this setSite() where I had used the entityType and it had, had a capital letter leading me to use the display name instead.

    Thanks again.

    Thursday, August 2, 2012 4:34 AM
  • Thank you too Andrii.
    Thursday, August 2, 2012 4:35 AM