web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

(CRM 4) Accessing product entity from product lookup in opportunityproduct(javascript)

(0) ShareShare
ReportReport
Posted on by

Hello,

I think this answer will be fairly straightforward for most of the CRM users:

How can I access all fields from the product entity in the onchage event of the product lookup when I'm creating a new opportunity product?

I'm aiming at doing this through Javascript and the reason I need it is because I need a value from a field in that entity to populate the form whenever a user changes a the product.

I already have the productid (that's one of the few fields the lookup has) but I'm not sure how to get the whole record associated with it.

I'm using CRM 4 not 2011.

Thanks.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Muhammad Adeel Javaid Profile Picture
    5,580 on at

    You can access a Sub-Product from within a main product in the following way:

    if (entity.Attributes.Contains("new_product"))

               {

                 productGUID = ((EntityReference)entity["new_product"]).Id;

                 Entity member = service.Retrieve("product", ((EntityReference)entity["new_product"]).Id, new ColumnSet(true));

                 if (member.Attributes.Contains("name"))

                 {

                   productName = member.Attributes["name"].ToString();

                 }

               }

  • Suggested answer
    Muhammad Adeel Javaid Profile Picture
    5,580 on at

    If you are looking for Retrievemultiple then the code will go like this:

    CrmService service = new CrmService();

    service.Credentials = System.Net.CredentialCache.DefaultCredentials;

    // Define the target dynamic entity that is to be retrieved.

    TargetRetrieveDynamic target = new TargetRetrieveDynamic();

    target.EntityName = EntityName.account.ToString();

    target.EntityId = new Guid("176545F4-A0C9-DC11-91CB-00065BEF6FD3");

    // Define the entity attributes (database table columns) that are to be

    retrieved.

    ColumnSet columnSet = new ColumnSet();

    columnSet.Attributes = new string [] { "accountid", "address1_city",

    "address1_country" };

    // Create a retrieve request object.

    RetrieveRequest retrieve = new RetrieveRequest();

    retrieve.Target = target;

    retrieve.ColumnSet = columnSet;

    retrieve.ReturnDynamicEntities = true;

    // Create a response reference and execute the retrieve request.

    RetrieveResponse response = (RetrieveResponse) service.Execute(retrieve);

    DynamicEntity retrievedEntity = (DynamicEntity) response.BusinessEntity;

    // Access the retrieved properties of the dynamic entity.

    foreach (Property prop in retrievedEntity.Properties)

    {

    Console.WriteLine(prop.Name.ToString());

    Console.WriteLine(prop.ToString);

    }

  • Community Member Profile Picture
    on at

    Thanks for the answer.

    No, I'm just looking to retrieve one product at a time from the product entity as you can only choose one at a time when you're selecting a product in the opportunity product.

    By the way, from your first post , I have to write it on both the form on_load and on the lookup field onchange events. I was wondering if "Service" is a keyword as I don't see you instancing it on the code.

    I'm guessing your first answer will do.

    I'll come back to you tomorrow on the result and will mark as answer if it worked.

    Thanks again.

  • Suggested answer
    Muhammad Adeel Javaid Profile Picture
    5,580 on at

    The first code is only to set attributes to access a product from within an entity or as I previously said "to access a sub-product from within main product". If you want to set the value of lookup then the code for this is like this and you can set values of your choice and need in it:

    //Create an array to set as the DataValue for the lookup control.

    var lookupData = new Array();

    //Create an Object add to the array.

    var lookupItem = new Object();

    //Set the id, typename, and name properties to the object.

    lookupItem.id = '{1AAC1363-01A1-DB11-8432-0003FF9CE217}';

    lookupItem.typename = 'account';

    lookupItem.name = 'A Bike Store';

    // Add the object to the array.

    lookupData[0] = lookupItem;

    // Set the value of the lookup field to the value of the array.

    crmForm.all.parentaccountid.DataValue = lookupData;

  • Muhammad Adeel Javaid Profile Picture
    5,580 on at

    With regard to first code the point to remember is that a 'EntityReference' is a lookup object. So a lookup needs to know which entity(table) it is pointing to as well as which specific record in the table (the GUID). You can't pass it just a GUID and you cannot convert a GUID to a EntityReference. Instead you need to wrap the GUID up in an EntityReference object.

  • Community Member Profile Picture
    on at

    Yes, I think I got it, and I don't need to update the lookup field, just a regular nvarchar field.

    But of course I need the fields from the product entity in the lookup.

  • Community Member Profile Picture
    on at

    I tried your 2 blocks of code. Both of them are giving me errors.

    I'm wondering, did you notice I mentioned I was using CRM 4. From what I gathered Entityreference is only allowed in CRM 2011.

    The other part seems like server side code and I have no clue as to why it isn't working.

    I will reiterate , I need to do this in Javascript and I'm using CRM 4.0 .

  • Verified answer
    Community Member Profile Picture
    on at

    I used this piece of code and got it to work.

    function GetObjectAttribute(objectid, entityname, attribute) {

       // Preparer the SOAP message

       var message =

           [

           "<?xml version='1.0' encoding='utf-8'?>",

           "<soap:Envelope xmlns:soap='schemas.xmlsoap.org/.../&,

           " xmlns:xsi='www.w3.org/.../XMLSchema-instance&,

           " xmlns:xsd='www.w3.org/.../XMLSchema&,

           GenerateAuthenticationHeader(),

           "<soap:Body>",

           "<Retrieve xmlns='schemas.microsoft.com/.../WebServices&,

           "<entityName>",

           entityname,

           "</entityName>",

           "<id>",

           objectid,

           "</id>",

           "<columnSet xmlns:q1='schemas.microsoft.com/.../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", "schemas.microsoft.com/.../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);

           if (attributeNode != null) {

               return attributeNode.text;

           }

       }

       return null;

    }

    Thanks for the help.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans