4
votes

I have a lookup field in the form, before select the related entity for the lookup, I check some conditions, if not pass, I overwrite the lookup onclick event to alert user; else, I need to overwrite the onclick event to show the lookup window to allow user to select entity.

So I need the object type code of this lookup, but before select there is no value then I can't get the object type code by use this code: var objecttypecode = Xrm.Page.getAttribute("field id").getValue()[0].type;

How to get object type code by entity name?

2
I find out the way: function GetObjectTypeCode(entityName) { try { 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; } } catch (ex) { throw ex; } } - nixjojo
Please answer your own question and accept it. - Anwar

2 Answers

5
votes

I find out the way:

function GetObjectTypeCode(entityName) {
    try {
        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;
        }
    }
    catch (ex) {
        throw ex;
    }
}
0
votes

One of the supported way of doing this is using the Metadata service and then retrieving the Object Type Code or etc (entity Type Code).