4

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; } } Commented Feb 21, 2012 at 7:25
  • 3
    Please answer your own question and accept it. Commented Feb 22, 2012 at 6:12

2 Answers 2

5

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;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

While this may work now, to my best knowledge, using RemoteCommand in custom code has always been unsupported and is not guaranteed to always work.
The GetObjectTypeCode(entityName) method mentioned above will not work in a HTML web resource because a HTML web resource is always contained within an IFrame & calling the CRM methods becomes a challenge.
@GCATNM: While you are absolutely right, I think the simplicity & lightweightnes of the RemoteCommand justifies this approach above the RetrieveEntityMetaData approach
@Arsenal: The justification of using unsupported and undocumented APIs is debatable. If simplicity outweighs putting oneself (and a potential customer) at the risk of having your code broken by future CRM updates, that may "justify" it for the individual. I highly doubt it can be called good practice if you're concerned with quality and reliability.
0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.