locked
distinguish account and contact RRS feed

  • Question

  • How can I distinguish in code whether a a customer id is a contact or an account?

    I have a plug in which gets a customer id. What I want to do at that point depends on whether it is a contact or account but all the calls I have found appear to be of the form ...

    service.Retrieve(LogicalName, id, columns)

    Or some variant of that, i.e. they all want me to say if it is an account or contact before I retrieve the data. OK that seems fair enough but I'm sure I missing some simple call which would tell me what sort of entity the id points to. Googling and the search here just returns information about the difference from the user interface, not how to distinguish it in code.

    So is there a simple way to know what it points at?

    Friday, March 15, 2013 11:53 AM

Answers

  • Take for example the parentcustomerid field of the contact entity, the parent customer can be an account or a contact, if you want to know the selected type you need to check the logicalname from the entityreference, is not possible before retrieve the field value if inside there is an account or a contact.

    Entity contact = service.Retrieve("contact", id, columns);
    EntityReference parentcustomer = (EntityReference)contact["parentcustomerid"];
    string type = parentcustomer.LogicalName;


    Friday, March 15, 2013 12:13 PM

All replies

  • Hi jn brand,

    I didnt get you.Please give it in clear.

    Friday, March 15, 2013 12:05 PM
  • Take for example the parentcustomerid field of the contact entity, the parent customer can be an account or a contact, if you want to know the selected type you need to check the logicalname from the entityreference, is not possible before retrieve the field value if inside there is an account or a contact.

    Entity contact = service.Retrieve("contact", id, columns);
    EntityReference parentcustomer = (EntityReference)contact["parentcustomerid"];
    string type = parentcustomer.LogicalName;


    Friday, March 15, 2013 12:13 PM
  • Hi Brand,

    When you get a customer id it must be an EntityRefrence. Am i right?
    Entity Reference has an attribute called LogicalName from this method you can check whather the customerID is contact or account and could write your logic further.

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!

    Friday, March 15, 2013 12:16 PM
  • Take for example the parentcustomerid field of the contact entity, the parent customer can be an account or a contact, if you want to know the selected type you need to check the logicalname from the entityreference, is not possible before retrieve the field value if inside there is an account or a contact.

    Entity contact = service.Retrieve("contact", id, columns);
    EntityReference parentcustomer = (EntityReference)contact["parentcustomerid"];
    string type = parentcustomer.LogicalName;

    Ta, I was trying all kinds of retrieve requests using the id. It never occurred to me just cast the attribute as an entity reference (insert embarassed numpty icon here)
    Friday, March 15, 2013 12:27 PM