locked
Setting a lookup value in CRM 2011 with Javascript RRS feed

  • Question

  • I'm trying to set a value in a lookup field in an entity from javascript but I can't get this to work.

    In a lot of my CRM forms I have taken to using a technique where I separate the data handling and user interface. CRM is quite good at handling the data but pretty bloody awful as a user interface so I often have the CRM fields on the form but in a hidden panel. I then use jQuery to write a new block of HTML into a new panel with a sensible user interface. jQuery on that then updates the CRM fields in the background. So CRM handles the data but jQuery handles the user interface. For most cases this works well and overcomes some of the user interface problems in CRM.

    The scenario I have is that in a case entity I have a custom priority lookup field. Which is a lookup to a new custom entity. To allow a meaningful data entry I populate a standard javascript option list with the available priority fields. So on loading the form I run the code;

    function PopulatePriorityList() {
    	var sFetch="";
    	sFetch = sFetch + "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
    	sFetch = sFetch + "<entity name='qubic_servicelevel'>";
    	sFetch = sFetch + "<attribute name='qubic_servicelevelid' />";
    	sFetch = sFetch + "<attribute name='qubic_name' />";
    	sFetch = sFetch + "<attribute name='qubic_servicemodule_priority' />";
    	sFetch = sFetch + "<attribute name='qubic_case_responsetime' />";
    	sFetch = sFetch + "<attribute name='qubic_case_escalationtime' />";
    	sFetch = sFetch + "<order attribute='qubic_case_responsetime' descending='false' />";
    	sFetch = sFetch + "<order attribute='qubic_case_escalationtime' descending='false' />";
    	sFetch = sFetch + "</entity>";
    	sFetch = sFetch + "</fetch>";
    	_oService = new FetchUtil(_sOrgName, _sServerUrl);
    	_oService.Fetch(sFetch, PopulatePriorityListCallback);
    }
    
    function PopulatePriorityListCallback(results) {
    	var divbody = "";
    	for (var i = 0; i < results.length; i++) {
    		var slid = results[i].attributes["qubic_servicelevelid"].value.toLowerCase(); 
    		//var priority = results[i].attributes["qubic_servicemodule_priority"].value;
    		var priority = "";
    		if (priority == "") {
    			priority = results[i].attributes["qubic_name"].value;
    		}
    		divbody = divbody + "<option value='" + slid+ "' label='" + priority + "' />";		
    	}
    	$('#prioritylist').html(divbody);
    }
    

    That gives me a drop list of all the values rather than a lookup in a popup window Linked to the change event is the code;

    	$('#prioritylist').change(function() {
    		SetPriorityLevel();									  
    	});
    
    ...
    
    
    function GetObjectTypeCode(entityName) 
    {
    	/// <summary>
    	/// Gets the EntityTypeCode / ObjectTypeCode of a entity
    	/// </summary>
    	/// <param name="entityName" type="string">
    	/// Name of entity to return object type code of
    	/// </param>
    	/// <returns type="int" />
    	var lookupService = new RemoteCommand("LookupService","RetrieveTypeCode");
    	lookupService.SetParameter("entityName", entityName);
    	var result = lookupService.Execute();
    	if (result.Success && typeof result.ReturnValue == "number") {
    		return result.ReturnValue;
    	} else {
    		return null;
    	}
    }
    
    function SetPriorityLevel() {
    	var value = new Array();
    	value[0] = new Object();
    	value[0].id = $('#prioritylist').val();
    	value[0].name = "dummy";
    	alert(GetObjectTypeCode("qubic_servicelevel"));
    	value[0].entityType = GetObjectTypeCode("qubic_servicelevel");
    	Xrm.Page.getAttribute("qubic_servicemodule_servicelevel").setValue(value);
    }
    
    

    It sort of works. The CRM field is set but doesn't appear to link to the underlying priority entity that was used to populate the original drop down list So what am I doing wrong in trying to set a lookup from Javascript?

    Wednesday, July 17, 2013 11:28 PM

All replies

  • Hi,

    Your SetPriorityLevel code is correct - what are the values it is setting? The Guid format should be :

    {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}

    hth


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Thursday, July 18, 2013 7:23 AM
    Answerer
  • var lookup = new Object();
    var lookupValue = new Array();
    lookup.id = Result.PriceLevelId;
    lookup.entityType = “pricelevel”;
    lookup.name = Result.Name;
    lookupValue[0] = lookup;
    Xrm.Page.getAttribute(“pricelevelid”).setValue(lookupValue);

    Try this.... if it not works problem will be your $('#prioritylist').val();

    Thursday, July 18, 2013 7:28 AM
  • var lookup = new Object();
    var lookupValue = new Array();
    lookup.id = Result.PriceLevelId;
    lookup.entityType = “pricelevel”;
    lookup.name = Result.Name;
    lookupValue[0] = lookup;
    Xrm.Page.getAttribute(“pricelevelid”).setValue(lookupValue);

    Try this.... if it not works problem will be your $('#prioritylist').val();

    That's the bug. It wanted the entity type name and I was giving it the entity type code. The oddity was that I originally gave it the entity name but when I wrote the code as;

    Xrm.Page.getAttribute("qubic_servicemodule_servicelevel").setValue({[ id: $('#prioritylist').val(), name: something, entityType: qubic_servicemodule_servicelevel]});

    That may be slightly wrong as I've just written that from memory, but the point was that using the entity name in that scenario failed so I changed to using entity type codes. Write the code using an Object to pass in to the setvalue call and I can use the names again and type codes fail. Ta.

    Thursday, July 18, 2013 8:47 AM