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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

ReferenceError: Sdk is not defined at eval

(0) ShareShare
ReportReport
Posted on by 297

Hello Everyone !

I hope that all of you are happy. I created a JS web resource which execute when form on load so when form loads "ReferenceError: Sdk is not defined at eval" error occurs. Can anyone tell me how to resolve this issue?

Following is the error message: 

6036.query-to-post.PNG

Following is my code:

// A namespace defined for the sample code
// As a best practice, you should always define 
// a unique namespace for your libraries
var Sdk = window.Sdk || {};
(function () {
// Code to run in the form OnLoad event
this.formOnLoad = function (executionContext) {
alert("hello world");
var rolename;
var count = 0;
var formContext = executionContext.getFormContext();
var userSettings = Xrm.Utility.getGlobalContext().userSettings;
var currentUserRoles = userSettings.securityRoles;

for (var i = 0; i < currentUserRoles.length; i++) {
rolename = getUserRoleName(currentUserRoles[i]); 
if (rolename == “CMS User”) {
formContext.getControl("prioritycode").setDisabled(true);
formContext.getControl("scheduledend").setDisabled(true);
}
}
}

function getUserRoleName(Roleid) {
var name;
var globalContext = Xrm.Utility.getGlobalContext();
var req = new XMLHttpRequest();
req.open(“GET”, globalContext.getClientUrl() + “/api/data/v9.1/roles?$select=name&$filter=roleid eq ” + Roleid, false);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“Prefer”, “odata.include-annotations=\”*\””);
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
name = results.value[i][“name”];

}
} else {
// Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
return name;
}

}).call(Sdk);

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: ReferenceError: Sdk is not defined at eval

    Hi,

    Try with this  , I just replace double comma with double quote .  

    // A namespace defined for the sample code
    // As a best practice, you should always define 
    // a unique namespace for your libraries
    var Sdk = window.Sdk || {};
    (function () {
        // Code to run in the form OnLoad event
        this.formOnLoad = function (executionContext) {
            alert("hello world");
            var rolename;
            var count = 0;
            var formContext = executionContext.getFormContext();
            var userSettings = Xrm.Utility.getGlobalContext().userSettings;
            var currentUserRoles = userSettings.securityRoles;
    
            for (var i = 0; i < currentUserRoles.length; i++) {
                rolename = getUserRoleName(currentUserRoles[i]); 
                if (rolename == "CMS User") {
                    formContext.getControl("prioritycode").setDisabled(true);
                    formContext.getControl("scheduledend").setDisabled(true);
                }
            }
        }
    
        function getUserRoleName(Roleid) {
            var name;
            var globalContext = Xrm.Utility.getGlobalContext();
            var req = new XMLHttpRequest();
            req.open("GET", globalContext.getClientUrl() + "/api/data/v9.1/roles?$select=name&$filter=roleid eq " + Roleid, false);
            req.setRequestHeader("OData-MaxVersion", "4.0");
            req.setRequestHeader("OData-Version", "4.0");
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
            req.onreadystatechange = function () {
                if (this.readyState === 4) {
                    req.onreadystatechange = null;
                    if (this.status === 200) {
                        var results = JSON.parse(this.response);
                        for (var i = 0; i < results.value.length; i++) {
                            name = results.value[i]["name"];
    
                        }
                    } else {
                        // Xrm.Utility.alertDialog(this.statusText);
                    }
                }
            };
            req.send();
            return name;
        }
    
    }).call(Sdk);


  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: ReferenceError: Sdk is not defined at eval

    Doing above code make sure when you are calling the function in form onload you need to put name with the namespace like "Sdk.formOnLoad" .

    Why not you are writing code straight forward way ? just call "formOnLoad" and dont forget to pass execution context while registering the function.

    function formOnLoad(executionContext) {
        alert("hello world");
        var rolename;
        var count = 0;
        var formContext = executionContext.getFormContext();
        var userSettings = Xrm.Utility.getGlobalContext().userSettings;
        var currentUserRoles = userSettings.securityRoles;
    
        for (var i = 0; i < currentUserRoles.length; i++) {
            rolename = getUserRoleName(currentUserRoles[i]); 
            if (rolename == "CMS User") {
                formContext.getControl("prioritycode").setDisabled(true);
                formContext.getControl("scheduledend").setDisabled(true);
            }
        }
    }
    
    function getUserRoleName(Roleid) {
        var name;
        var globalContext = Xrm.Utility.getGlobalContext();
        var req = new XMLHttpRequest();
        req.open("GET", globalContext.getClientUrl() + "/api/data/v9.1/roles?$select=name&$filter=roleid eq " + Roleid, false);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    var results = JSON.parse(this.response);
                    for (var i = 0; i < results.value.length; i++) {
                        name = results.value[i]["name"];
    
                    }
                } else {
                    // Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();
        return name;
    }
  • gupta.ambar2009@gmail.com Profile Picture
    797 on at
    RE: ReferenceError: Sdk is not defined at eval

    Hello ,

    This type of error comes either if we not included java script library or syntax is not proper

    after complete with the coding test 1 time syntax in online website like esprima.org/.../validate.html

  • Community Member Profile Picture
    on at
    RE: ReferenceError: Sdk is not defined at eval

    Since you're using Jscript notation the function : this.functionName = function (executionContext)  

    this.getUserRoleName = function(executionContext){}

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans