locked
[CRM 4] Using SOAP javascript to retrieve an entity attribute by name (instead of ID) RRS feed

  • Question

  • Hi,

    I'm trying as the title says to retrieve a product by name instead of ID using :

    function GetObjectAttribute (objectname, entityname, attribute) {
        // Preparer the SOAP message
        var message =
            [
            "<?xml version='1.0' encoding='utf-8'?>",
            "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'",
            " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'",
            " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>",
            GenerateAuthenticationHeader(),
            "<soap:Body>",
            "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>",
            "<entityName>",
            entityname,
            "</entityName>",
            "<name>",
            objectname,
            "</name>",
            "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'",
            " xsi:type='q1:ColumnSet'>",
            "<q1:Attributes><q1:Attribute>",
            attribute,
            "</q1:Attribute></q1:Attributes>",
            "</columnSet></Retrieve>",
            "</soap:Body></soap:Envelope>"
            ].join("");
    
        var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        xmlhttp.open("POST", "/MSCrmServices/2007/CrmService.asmx", false);
        xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
        xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlhttp.setRequestHeader("Content-Length", message.length);
        xmlhttp.send(message);
    
        var result = xmlhttp.responseXML;
    
        var error = result.selectNodes('//error').length;
        if (error == 0) {
            var attributeNode = result.selectSingleNode('//q1:' + attribute);
           alert(attributeNode);
            if (attributeNode != null) {
                return attributeNode.text;
            }
        }
        return null;
    }

    I changed in the code the tag <id></id> to <name></name> thinking this would work but it doesn't. I always get an empty object.

    I know that the objectname i'm passing to the function is correct but still no result.

    Is this possible to do?

    Thanks

    Tuesday, January 21, 2014 2:55 PM

Answers

All replies

  • If my mind serves me correct you're using the wrong message type. A retrieve request only returns 1 record so expects you to pass it an ID. What you need to do is use a RetrieveMultiple request and then (if you're only expecting 1 back) take the first item in the list returned.

    See here for more details: http://msdn.microsoft.com/en-us/library/cc677077.aspx

    • Proposed as answer by Guido PreiteMVP Wednesday, January 22, 2014 12:52 AM
    • Marked as answer by Agenteusa Wednesday, January 22, 2014 11:22 AM
    Tuesday, January 21, 2014 11:54 PM
  • Hi,

    You're not clear.

    1. In title you say "trying to get entity attribute (e.g. entity field metadata) by name".

    2. In content message you say "to retrieve a product (e.g. get a record) by name".

    What's your goal?

    For point 1, you can check this MSDN article.

    For point 2 (I think you need this one), use Conor's link.

    Hope it helps.


    • Edited by Linh Giang Wednesday, January 22, 2014 2:34 AM
    • Marked as answer by Agenteusa Wednesday, January 22, 2014 11:22 AM
    Wednesday, January 22, 2014 2:32 AM
  • If you want to get product based on name, you need to use retrievemultiple message instead of retrieve. You can refer below example if you are working with 2011, if working with 4.0 refer Ms CRM 4.0sdk for retrievemultiple

    http://mahenderpal.wordpress.com/2011/06/28/set-default-price-list-in-ms-crm-2011-using-java-script/


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by Agenteusa Wednesday, January 22, 2014 11:22 AM
    Wednesday, January 22, 2014 6:24 AM
    Moderator
  • I meant:

    1. In title you say "trying to get entity attribute (e.g. entity field metadata) by name".

    entity atribute here is the field "currentcost" from the product entity.

    2. In content message you say "to retrieve a product (e.g. get a record) by name".

    Yes I didn't phrase it well, what I want is just a field not all metadata for product.

    Thanks for the answers. I will try a retrievemultiple, I thought since I only want a record (meaning I know only one product matches a specific name) I could use the simple retrieve.

    Going to try and I'll get back to you.

    Wednesday, January 22, 2014 10:39 AM
  • Yes, using the retrievemultiple that I had already customized for another implementation worked.

    Thanks for the answers.

    Wednesday, January 22, 2014 11:23 AM