[TIPS] Change the lookup Field Display name after selection

 [TIPS] Change the lookup Field Display name after selection 

The lookup Field display name is always its target entity's primary column value.

If you wanna change the display name, below solution using JS could be your reference.

Key steps:

var value = formContext.getAttribute("your lookup filed logical name").getValue();

value[0].name = "the display name you want"; 

formContext.getAttribute("your lookup filed logical name").setValue(value);


My script based on my actual senario.


//Added by Forrest on Dec 15, 2022.
//Purpose: Change the display name of "In-use Location" lookup field
function setInUseLocDisplayName(executionContext)
{
    debugger;
    var formContext = executionContext.getFormContext();  
    var attrInUseLocName = formContext.getAttribute("qt_msdyn_workorder");
    var valueInUseLocName = attrInUseLocName == null ? null : attrInUseLocName.getValue();
 
    if (valueInUseLocName != null)
    {
        Xrm.WebApi.retrieveRecord("msdyn_workorder", valueInUseLocName[0].id, "?$select=_msdyn_functionallocation_value").then(
            function success(result) {
                debugger;
                Xrm.WebApi.retrieveRecord("msdyn_functionallocation", result._msdyn_functionallocation_value, "?$select=msdyn_name").then(
                    function success(result2) {
                        debugger;
                        valueInUseLocName[0].name = result2.msdyn_name;
                        attrInUseLocName.setValue(valueInUseLocName);
                    },
                    function (error) {
                        console.log(error.message);
                    }
                );
            },
            function (error) {
                console.log(error.message);
            }
        );
    }   
}





No comments:

Post a Comment