Very recently a poster in the CRM Development forum asked why she could not copy the value from a Contact Lookup field into the “To” field of a Phone Call entity. On the surface, her code was very simple and would largely be expected to work. What ended up working was basically creating a new value for the PartyList that recreated values from the Contact Lookup.
Why was this necessary? I don’t really know, and haven’t looked any deeper into CRM 2011 yet, but what I know from CRM 4 is that certain expressions of the DataValue for a Lookup field can contain extra, undocumented data elements. I suspect that CRM 2011 has expanded the use and perhaps number of these undocumented data elements, and their presence from one Lookup field prevents the direct assignment of the value to another Lookup field.
Perhaps this is limited to Single-to-PartyList copies, but perhaps not. Either way, I’ve whipped up a handy function that can help easily copy or append values into Lookups when those values are taken from other Lookups. Also, it allows you to provide a delegate function to which it will pass each entity reference and expect a boolean indication as to whether or not the value should be accepted for inclusion in the target attribute.
function CopyLookupValue(sourceAttribute, targetAttribute, boolAppendSource, funcDelegate)
var sourceValue = sourceAttribute.getValue();
var newValue = ;
if (typeof boolAppendSource != "undefined" && boolAppendSource)
sourceValue = targetAttribute.getValue().concat(sourceValue);
foreach(var valueIndex in sourceValue)
var copyValue = true;
if (typeof funcDelegate == "Function")
copyValue = funcDelegate(sourceValue[valueIndex]);
Using the thread that initiated this code as an example, here is how the above code would be used to copy the value of a Contact field on a parent form window to a To field on the child form window:
var remoteAttribute = window.parent.opener.Xrm.Page.data.entity.attributes.get("bc_contact");