hi
You can use the Dynamics 365 Web API to dynamically retrieve the metadata for the current entity and then find the Primary Name field. Here is an example of how you could modify your function to accomplish this:
javascript code:
function setRecordName(entityName, entityId, nameFieldLogicalName, ...lookupFieldLogicalNames) {
// Get metadata for current entity
Xrm.WebApi.retrieveEntity(entityName, null)
.then(function(entityMetadata) {
// Find Primary Name field
var primaryNameField = entityMetadata.PrimaryNameAttribute;
// Use Primary Name field if nameFieldLogicalName parameter not provided
if (!nameFieldLogicalName) {
nameFieldLogicalName = primaryNameField;
}
// Retrieve lookup entity
Xrm.WebApi.retrieveRecord(entityId, lookupEntityName, null)
.then(function(lookupEntity) {
// Concatenate fields
var name = "";
for (var i = 0; i < lookupFieldLogicalNames.length; i++) {
name += lookupEntity[lookupFieldLogicalNames[i]] + " ";
}
name = name.trim();
// Set record name
var record = {};
record[nameFieldLogicalName] = name;
Xrm.WebApi.updateRecord(entityName, entityId, record);
});
});
}
In this example, entityName and entityId are the logical name and ID of the current entity, nameFieldLogicalName is the logical name of the field to set as the record name (defaults to Primary Name field if not provided), and lookupFieldLogicalNames are the logical names of the fields to concatenate from the lookup entity.
By using the retrieveEntity method, we can get the metadata for the current entity, which includes the logical name of the Primary Name field. We can then use this field name in our function.
DAniele