Question Status

Suggested Answer
Dave Cockerham asked a question on 22 Feb 2012 7:16 AM

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

Reply
Suggested Answer
Jason Lattimer responded on 27 Feb 2012 7:08 AM

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

Reply
Suggested Answer
Gus Gonzalez responded on 27 Feb 2012 7:31 AM

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!

Reply
Gus Gonzalez responded on 24 Feb 2012 7:09 PM

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.

Reply
Dave Cockerham responded on 27 Feb 2012 3:51 AM

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

Reply
Suggested Answer
Jason Lattimer responded on 27 Feb 2012 7:08 AM

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

Reply
Dave Cockerham responded on 27 Feb 2012 7:29 AM

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

Reply
Suggested Answer
Gus Gonzalez responded on 27 Feb 2012 7:31 AM

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!

Reply
Jason Lattimer responded on 27 Feb 2012 7:33 AM

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

Reply
Dave Cockerham responded on 27 Feb 2012 7:40 AM

OK thanks I'll give it a go

Reply
Dave Cockerham responded on 27 Feb 2012 7:41 AM

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

Reply
Gus Gonzalez responded on 27 Feb 2012 7:58 AM

Do you have the JQuery library available?

That should take care of it.

Reply
Dave Cockerham responded on 27 Feb 2012 8:00 AM

Hi Gus

Where can I find it?

Dave

Reply
Dave Cockerham responded on 27 Feb 2012 8:15 AM

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

Reply
Gus Gonzalez responded on 27 Feb 2012 8:25 AM

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

Reply
Dave Cockerham responded on 27 Feb 2012 8:26 AM

which full solution?

Reply
Gus Gonzalez responded on 27 Feb 2012 8:35 AM

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?

Reply