locked
MSCRM 2011 Javascript not working RRS feed

  • Question

  • Hi, the following code are the javascript that i apply in my crm case form.

    function AssignToMe() {
     var context;
     var serverUrl;
     var UserID;
     var ODataPath;
     context = Xrm.Page.context;
     serverUrl = context.getServerUrl();
     UserID = context.getUserId();
     ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
     var retrieveUserReq = new XMLHttpRequest();
     retrieveUserReq.open("GET", ODataPath + "/SystemUserSet(guid'" + UserID + "')", true);
     retrieveUserReq.setRequestHeader("Accept", "application/json");
     retrieveUserReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
     retrieveUserReq.onreadystatechange = function () {
     retrieveUserReqCallBack(this,UserID);
     };
     retrieveUserReq.send();
     
    }
     
    function retrieveUserReqCallBack(retrieveUserReq,UserID) {
    var CurrentUser = Xrm.Page.getAttribute('ownerid').getValue();
    var CurrentUserId = CurrentUser[0].id;
    if (retrieveUserReq.readyState == 4 /* complete */) {
    if (retrieveUserReq.status == 200) {
     var retrievedUser = this.parent.JSON.parse(retrieveUserReq.responseText).d;
     if ((retrievedUser.FullName != null) && (CurrentUserId != UserID))
      {
    
    var setUservalue = new Array();
     setUservalue[0] = new Object();
     setUservalue[0].id = Xrm.Page.context.getUserId();
     setUservalue[0].entityType = 'systemuser';
     setUservalue[0].name = retrievedUser.FullName;
     
    Xrm.Page.getAttribute("ownerid").setValue(setUservalue)
    Xrm.Page.data.entity.save();
     }
     }
    }
    }
    

    I would like to ask, why the javascript not working when i access  URL http://IP/DEV/. but its working if i access URL http://localhost/DEV/. both are same eviroment.

    Thank you

    Thursday, October 16, 2014 4:21 AM

Answers

  • Hi,

    getServerUrl() method returns  the url according to the configured URL in Deployment Manager and not based on the url you are using to access the page. It causes cross domain issue when you access CRM from IP address.

    Try to get the serverURL using below script.

    function getServerUrl() {
        var context, crmServerUrl;
        if (typeof GetGlobalContext != "undefined") {
            context = GetGlobalContext();
        }
        else if (typeof Xrm != "undefined") {
            context = Xrm.Page.context;
        }
        else {
            throw new Error("CRM context is not available.");
        }

        if (context.isOutlookClient() && !context.isOutlookOnline()) {
            crmServerUrl = window.location.protocol + "//" + window.location.host;
        } else {
            crmServerUrl = context.getServerUrl();
            crmServerUrl = crmServerUrl.replace(/^(http|https):\/\/([_a-zA-Z0-9\-\.]+)(:([0-9]{1,5}))?/, window.location.protocol + "//" + window.location.host);
            crmServerUrl = crmServerUrl.replace(/\/$/, ""); // remove trailing slash if any
        }
        return crmServerUrl;
    }

    Thanks!

    Kalim Khan

    • Proposed as answer by Kalim Khan Thursday, October 16, 2014 9:42 AM
    • Marked as answer by KennyChan6996 Tuesday, October 21, 2014 5:31 AM
    Thursday, October 16, 2014 5:03 AM
  • Hi, 

    Did you add "JSON" library web resource to the case form libraries?

    Try using var retrievedUser = JSON.parse(retrieveUserReq.responseText).d;

    instead of var retrievedUser = this.parent.JSON.parse(retrieveUserReq.responseText).d;


    Vikranth Pandiri Blog: http://howto-mscrm.blogspot.com Twitter: @TweetVikranth "Please Mark it as answer if it helps in resolving your query"

    • Marked as answer by KennyChan6996 Tuesday, October 21, 2014 5:30 AM
    Tuesday, October 21, 2014 4:00 AM

All replies

  • Hi,

    getServerUrl() method returns  the url according to the configured URL in Deployment Manager and not based on the url you are using to access the page. It causes cross domain issue when you access CRM from IP address.

    Try to get the serverURL using below script.

    function getServerUrl() {
        var context, crmServerUrl;
        if (typeof GetGlobalContext != "undefined") {
            context = GetGlobalContext();
        }
        else if (typeof Xrm != "undefined") {
            context = Xrm.Page.context;
        }
        else {
            throw new Error("CRM context is not available.");
        }

        if (context.isOutlookClient() && !context.isOutlookOnline()) {
            crmServerUrl = window.location.protocol + "//" + window.location.host;
        } else {
            crmServerUrl = context.getServerUrl();
            crmServerUrl = crmServerUrl.replace(/^(http|https):\/\/([_a-zA-Z0-9\-\.]+)(:([0-9]{1,5}))?/, window.location.protocol + "//" + window.location.host);
            crmServerUrl = crmServerUrl.replace(/\/$/, ""); // remove trailing slash if any
        }
        return crmServerUrl;
    }

    Thanks!

    Kalim Khan

    • Proposed as answer by Kalim Khan Thursday, October 16, 2014 9:42 AM
    • Marked as answer by KennyChan6996 Tuesday, October 21, 2014 5:31 AM
    Thursday, October 16, 2014 5:03 AM
  • HI, i was try to apply ur javascript into crm. but still the same problem.

    thank you

    Thursday, October 16, 2014 10:33 AM
  • Hi,

    Would you please post the error? I think it is related to URL only. Would you please check what URL you are getting through method provided by me by adding some alerts?

    Thanks!

    Kalim Khan


    • Edited by Kalim Khan Thursday, October 16, 2014 10:57 AM
    Thursday, October 16, 2014 10:56 AM
  • Hi Kalim Khan

    the following javascript are the latest javascript i apply into my crm

    function AssignToMe() {
     var UserID;
     var ODataPath;
     context = Xrm.Page.context;
     UserID = context.getUserId();
     var context, crmServerUrl;
         if (typeof GetGlobalContext != "undefined") {
             context = GetGlobalContext();
         }
         else if (typeof Xrm != "undefined") {
             context = Xrm.Page.context;
         }
         else {
             throw new Error("CRM context is not available.");
         }
     
        if (context.isOutlookClient() && !context.isOutlookOnline()) {
             crmServerUrl = window.location.protocol + "//" + window.location.host;
         } else {
             crmServerUrl = context.getServerUrl();
             crmServerUrl = crmServerUrl.replace(/^(http|https):\/\/([_a-zA-Z0-9\-\.]+)(:([0-9]{1,5}))?/, window.location.protocol + "//" + window.location.host);
             crmServerUrl = crmServerUrl.replace(/\/$/, ""); // remove trailing slash if any
         }
     ODataPath = crmServerUrl + "/XRMServices/2011/OrganizationData.svc";
     var retrieveUserReq = new XMLHttpRequest();
     retrieveUserReq.open("GET", ODataPath + "/SystemUserSet(guid'" + UserID + "')", true);
     retrieveUserReq.setRequestHeader("Accept", "application/json");
     retrieveUserReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
     retrieveUserReq.onreadystatechange = function () {
     retrieveUserReqCallBack(this,UserID);
     };
     retrieveUserReq.send();
     
    }
     
    function retrieveUserReqCallBack(retrieveUserReq,UserID) {
    var CurrentUser = Xrm.Page.getAttribute('ownerid').getValue();
    var CurrentUserId = CurrentUser[0].id;
    if (retrieveUserReq.readyState == 4 /* complete */) {
    if (retrieveUserReq.status == 200) {
     var retrievedUser = this.parent.JSON.parse(retrieveUserReq.responseText).d;
     if ((retrievedUser.FullName != null) && (CurrentUserId != UserID))
      {
    
    var setUservalue = new Array();
     setUservalue[0] = new Object();
     setUservalue[0].id = Xrm.Page.context.getUserId();
     setUservalue[0].entityType = 'systemuser';
     setUservalue[0].name = retrievedUser.FullName;
     
    Xrm.Page.getAttribute("ownerid").setValue(setUservalue)
    Xrm.Page.data.entity.save();
     }
     }
    }
    }

    When i browsing using http://localhost will not have problem, the problem happen when browse using IP address. The following are the error i get when i browse using IP addres.

    Microsoft Dynamics CRM Error Report Contents
    
    <CrmScriptErrorReport>
      <ReportVersion>1.0</ReportVersion>
      <ScriptErrorDetails>
       <Message>'this.parent.JSON' is null or not an object</Message>
       <Line>492786619</Line>
       <URL>/userdefined/edit.aspx?_gridType=112&etc=112&id=%7b2E8726BE-1655-E411-AD5E-005056AF35E7%7d&pagemode=iframe&preloadcache=1413858265945&rskey=202012935</URL>
       <PageURL>/userdefined/edit.aspx?_gridType=112&etc=112&id=%7b2E8726BE-1655-E411-AD5E-005056AF35E7%7d&pagemode=iframe&preloadcache=1413858265945&rskey=202012935</PageURL>
       <Function>retrieveUserReqCallBack(retrieveUserReq,UserID)</Function>
       <CallStack>
        <Function>retrieveUserReqCallBack(retrieveUserReq,UserID)</Function>
        <Function>anonymous(){retrieveUserReqCallBack(this,UserID);}</Function>
        <Function>AssignToMe()</Function>
        <Function>anonymous($p0,$p1,$p2,$p3){Mscrm.CrmHeader.setScriptFile(Mscrm.CrmUri.create($p1),true);for(var$v_0=window,$v_1=$p0.split("."),$v_2=0;$v_2<$v_1.length;$v_2++)if($v_0)$v_0=$v_0[$v_1[$v_2]];if(!IsNull($v_0)&&typeof$v_0===Mscrm.TypeNames.functionType){var$v_3=this.$CN_1($p2,$p3);return$v_0.apply(null,$v_3)}returnnull}</Function>
        <Function>anonymous($p0,$p1,$p2,$p3){var$v_0=this.getCommandDefinition($p0,$p1);if(IsNull($v_0)||IsNull($v_0.Actions))returnfalse;for(var$v_1=0;$v_1<$v_0.Actions.length;$v_1++){var$v_2=$v_0.Actions[$v_1];switch($v_2.ActionType){case2:this.$DF_1($v_2.Attributes);break;case3:var$v_3=$v_2.Attributes;this.$9z_1($v_3.FunctionName,$v_3.Library,$v_2.Parameters,$p2);break;case1:this.$DG_1($v_2.Attributes,$v_2.Parameters);break}}returntrue}</Function>
        <Function>anonymous($p0,$p1,$p2){var$v_0=this.parseCommandFromRibbon($p0),$v_1=this.$7w_1($v_0.command,$v_0.entityLogicalName,$p1,$p2),$v_2=$p1;if(!IsNull($v_2)&&!isNullOrEmptyString($v_2.PopulationXML)&&!$v_2.SuppressCommandIncludes){var$v_3=null;if($v_0.entityContext==="Form"){var$v_4=$find("crmFormSelector");if($v_4)$v_3=$v_4.get_currentFormId()}$v_2.PopulationXML=this.$Ep_1($v_2.PopulationXML,$v_0.entityLogicalName,$v_3)}return$v_1}</Function>
       </CallStack>
      </ScriptErrorDetails>
      <ClientInformation>
       <BrowserUserAgent>Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E; .NET CLR 3.5.30729; .NET CLR 3.0.30729)</BrowserUserAgent>
       <BrowserLanguage>en-us</BrowserLanguage>
       <SystemLanguage>en-us</SystemLanguage>
       <UserLanguage>en-us</UserLanguage>
       <ScreenResolution>1364x768</ScreenResolution>
       <ClientName>Web</ClientName>
       <ClientTime>2014-10-21T10:24:36</ClientTime>
      </ClientInformation>
      <ServerInformation>
        <OrgLanguage>1033</OrgLanguage>
        <OrgCulture>2057</OrgCulture>
        <UserLanguage>1033</UserLanguage>
        <UserCulture>2057</UserCulture>
        <OrgID>{737C6007-91A7-455C-B3B2-DE9E2FE58252}</OrgID>
        <UserID>{455DD73A-AC55-E311-9B8D-005056AF35E7}</UserID>
        <CRMVersion>5.0.9690.4150</CRMVersion>
      </ServerInformation>
    </CrmScriptErrorReport>
    
    
    Thank you
    Tuesday, October 21, 2014 2:37 AM
  • Hi, 

    Did you add "JSON" library web resource to the case form libraries?

    Try using var retrievedUser = JSON.parse(retrieveUserReq.responseText).d;

    instead of var retrievedUser = this.parent.JSON.parse(retrieveUserReq.responseText).d;


    Vikranth Pandiri Blog: http://howto-mscrm.blogspot.com Twitter: @TweetVikranth "Please Mark it as answer if it helps in resolving your query"

    • Marked as answer by KennyChan6996 Tuesday, October 21, 2014 5:30 AM
    Tuesday, October 21, 2014 4:00 AM
  • Thank you very much! its working now!

    Tuesday, October 21, 2014 5:30 AM