CRM 2011 "$ is undefined"
Asked Answered
M

3

13

I have created following code, and I have included this as web resource on the CRM 2011 form to be called on field onchange event of lookup field. Everything is working fine before the $.ajax({... line and then I have an error “$ is undefined”. I am not very familiar with scripting so please help.

function GetAddress() {

    var accountId;
    var dataArray;
    var accountRequestUrl;

    if (crmForm.all.regardingobjectid.DataValue != null) {

        dataArray = crmForm.all.regardingobjectid.DataValue;
        accountId = dataArray[0].id;

        if (typeof GetGlobalContext == "function") {
            var context = GetGlobalContext();
            accountRequestUrl = context.getServerUrl();
        }
        else {
            if (typeof Xrm.Page.context == "object") {
                accountRequestUrl = Xrm.Page.context.getServerUrl();
            }
        }

        accountRequestUrl = Xrm.Page.context.getServerUrl();
        accountRequestUrl += "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'" +
            accountId + "')";

        crmForm.all.maxlife_addressname.DataValue = accountRequestUrl;

        GetAccountRecord(accountRequestUrl);
    }
    else {
        alert("null");
    }

}

function GetAccountRecord(accountRequestUrl) {

    $.ajax({
        type: "GET",
        url: accountRequestUrl,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (request, textStatus, errorThrown) {
            alert("Error occurred: " + request.responseXML + "from url " + requestUrl);
            return;
        },
        success: function (data) {
            var results = data.d["results"];
            var AccountValue = new Array();
            for (resultKey in results) {
                AccountValue.push(results[resultKey]);
            }

            FillValues(AccountValue);
        }
    });
}
Marianomaribel answered 2/3, 2011 at 14:1 Comment(0)
D
25

$ is shorthand for jQuery. jQuery is not natively included in CRM2011, so you'll have to add a web reference yourself. Simply create a JavaScript web resource for jQuery, paste in the jQuery code, and then add the web resource to your form. Also, in order to get the web resource to load on your form, you need to specify a function for CRM to call from it. Since in this case jQuery is a library and you won't be calling any of its functions onload, simply use isNaN (a native JavaScript function) as the function to call.

Disallow answered 2/3, 2011 at 14:14 Comment(3)
Thnaks. Sound like a solution, but can you give me some instructions how to do this. Pleaseeeee.Marianomaribel
This post should get you most of the way there. If you still need more help I will try to post additional info later. gtcrm.wordpress.com/2011/02/15/…Disallow
If this script is being called from the 'OnChange' event of a form control, then there is no need to call anything on-form-load. Just adding the scripts to the form will automatically load them in the order they appear on the form's script customization grid. The only time you'd need to call a function to ensure a script is loaded is if a ribbon script has a dependent script. (I hope it is clear what I am saying)Kirstenkirsteni
G
1

The entity form on which you are working. Go to Form customization->Form properties. You can see the Files(.js) already included for that form.

Click on 'Add'(left top)..and add the JQuery file(like JQuery1.4.4 or higher version) if JQuery file is added in your CRM Webresources, if not then you need to add this file in CRM webresources first.

Gertrudgertruda answered 10/4, 2013 at 7:44 Comment(3)
This won't work because CRM doesn't load the libraries added to your form if you don't call a function from within that library. call $.isNumeric() or !isNaN to load the library for use by other javascript on your form.Precast
I guess if a form is loaded then first it's included library will load then Form's body will get start to load. So that if any function is called that is present in included library then it will work. One thing is here that try to minimize the including your library in your form because more library more time it will take to load first then your form will start to load. Please correct me if i am wrong.Gertrudgertruda
I understand what you are trying to say. To be clear you have to make a call to a function in every library you include using the form editor otherwise the form will not load it. I think they were wrong to make it not load libraries that aren't called as it takes control away from the developer. If I want to develop slow forms that take a long time to load that's my business. Modern browsers cache these libraries anyway so the performance hit wouldn't be noticed after the first load anyway.Precast
A
0

Sounds like you need to include jquery on your form.

Basically you just add jquery the same way you would any other javascript file.

  • Download a copy of jquery (unzip if zipped, you need the .js file)
  • Navigate to Web Resources in your Solution
  • Click the New button in the toolbar
  • Fill in the form
  • Click the Browse button for the Upload File box
  • Select the .js file you downloaded
  • Click the Save button in the ribbon bar
  • Click the Publish button in the Ribbon bar

Add your newly created Web Resource to your form (Under Form Properties).
Be sure this is the first library listed on your form.
You don’t need anything in Event Handlers for jquery, just call it from any of your custom libraries as per usual.

Keep in mind that many of the things you may be tempted to use jquery for may not be supported. Microsoft wants you to use the Xrm.Page object:
Use the Xrm.Page Object Model
http://msdn.microsoft.com/en-us/library/gg328474.aspx

According to Microsoft:
Use of jQuery
Do not use jQuery to interact with Microsoft Dynamics CRM 2011 forms.
Use the Xrm.Page object model methods to access form elements.
The only supported use of jQuery in the Microsoft Dynamics CRM 2011 web
application is to use the jQuery.ajax method to retrieve data from
the REST endpoint. For more information, see Using jQuery.
Using jQuery to modify Microsoft Dynamics CRM 2011 application pages or forms is not supported.
You may use jQuery within your own HTML web resource pages.
http://msdn.microsoft.com/en-us/library/gg328261.aspx#BKMK_UsingjQuery>

Use the REST Endpoint with Ajax and JScript Web Resources
http://msdn.microsoft.com/en-us/library/1bb82714-1bd6-4ea4-8faf-93bf29cabaad#BKMK_UsingJQuery

CRM 2011 Useful JavaScript Tidbits
Call the onchange event of a field
http://www.powerobjects.com/blog/2011/01/14/crm-2011-useful-javascript-tidbits/

Abdul answered 14/2, 2014 at 16:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.