Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xrm is undefined in Dynamics CRM 2011

I can't seem to get my Xrm variable working when I create .js code. Is there a library I need to include or a function I need to run first? Do I need to make sure that ClientGlobalContext.js.aspx is referenced correctly? I'm in a 'C++' mindset and just wondering if there is any sort of 'include' command that I need to be running.

My js file looks like this, and if I remove the 'window.parent.' from the 2nd function the code breaks if I call it.

///<reference path="C:\Users\steve.lee\Downloads\XrmPage-vsdoc.js"/>

if (typeof (SDK) == "undefined") 
{ SDK = { __namespace: true }; }

SDK.XRM = {
    getCurrentControl: function () {
        var currentControl = Xrm.Page.ui.getCurrentControl();
        if (currentControl == null) {
            alert("No controls currently have focus.");
        }
        else {
            alert("The control for the '" + currentControl.getLabel() + "' attribute currently has focus.");
        }
    },

    getCurrentGUID: function () {

        if (window.parent.Xrm.Page.data.entity != null) {
            var GUIDvalue = window.parent.Xrm.Page.data.entity.getId();
            if (GUIDvalue != null) {
                return GUIDvalue;
            }
            else {
                return null;
            }
        }
        else {
            return null;
        }
    },
 __namespace: true
};
like image 590
Steve Avatar asked Dec 28 '22 09:12

Steve


1 Answers

Is your code running as a javascript library added to a form or is it sat inside a web resource?

If its the latter, you need window.parent if its a HTML web resource. As this gets you access to the Xrm object on the form. In the parent window above it.

If it's a javascript library attached to the form properties then the XRM object is available by default.

The ClientGlobalContext.js.aspx is used when you have a web resource that may not be in the context of a form and still gives you access to the server url and the logged in user for example.

like image 54
Chris Avatar answered Jan 16 '23 02:01

Chris