How to retrieve values from a related record that is a lookup

This question has suggested answer(s)

Hi

I'm trying to write some Javascript for CRM2011 that reads a value from a bit field on a related record.  The specific example is as follows:-

Using the Quote Product record, select a product, I'm trying to read a value from the related product record (productid) from a field called "new_overrideprice" and set the value of a field to the same value and has the same name on the Quote Product record.

I've tried using FetchXML and got completely lost.

Any help anybody can provide would be gratefully received

Regards

Dave

All Replies
  • Dave,

    Have you resolved this issue?

    If not let me know and I can send you something early next week when I'm back at the office.

    Thanks.

  • Hi Gus

    Thanks for the reply.  I haven't resolved the issue yet, I'd be very grateful for anything you could send me that would help

    Regards

    Dave

  • You could attaching something like this to the Existing Product's On Change event:

     

    function Product_OnChange() {
        var productid = Xrm.Page.getAttribute("productid").getValue()[0].id;
    
        var urlSelect = "http://YourServer/YourOrg/xrmservices/2011/OrganizationData.svc" +
           "/ProductSet?$select=new_overrideprice&$filter=ProductId eq guid'" + productid + "'";
    
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: urlSelect,
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, XmlHttpRequest) {
                if (data.d.results[0].new_overrideprice) {
    
                    if (data.d.results[0].new_overrideprice == true) {
                        Xrm.Page.getAttribute("new_overrideprice").setValue(1);
                    } else {
                        Xrm.Page.getAttribute("new_overrideprice").setValue(0);
                    }
    
                } else {
                    Xrm.Page.getAttribute("new_overrideprice").setValue(0);
                }
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert('Error');
            }
        });
    }
    

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

  • Hi.. thank you very much indeed for this.  I get an error that says $ is undefined.  What do I need to do?

  • Dave,

    Here is the code I created to pull the Tax Rate of a Store once a Store is selected on a Lookup field:

    http://pastebin.com/djMV8nhY

    We execute this script OnLoad and OnChange on the Store lookup.

    This should be all you need.

    Good Luck!

  • Oh yes - you will also need to add a separate web reference to jQuery before your script file containing the code to retrieve the values. 

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

  • OK thanks I'll give it a go

  • Hi gus.. I get an error that says CRMServiceHelper not defined

  • Do you have the JQuery library available?

    That should take care of it.

  • Hi Gus

    Where can I find it?

    Dave

  • http://code.jquery.com/jquery-1.7.1.min.js

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

  • Do I just add a new webresource of type "jscript" and import existing file, selecting the jquery-1.7.1.min.js file?

  • Import the full solution at Settings -> Customizations -> Solutions -> Import...

  • which full solution?

  • The one found here:

    spservices.codeplex.com/.../342338

    I have one question Dave, has this been resolved? I see you marked the answer as verified so I don;t understand why we are still working on it.

    Is it working for you right now?