คำถาม JScript WhoAmIRequest

  • Thursday, April 17, 2008 5:33 PM
     
     

    Hi All,

    I found this code that gets the current user (http://jianwang.blogspot.com/2008/01/crm-40-get-userid-businessunitid.html) but I can't get it to function.  I am using CRM 3.0 and changed the references to 2007 to 2006, put the code in the onLoad event and called GetCurrentUserInfo();

     

    When the form opens, I get this error:

    There was an error with this field's customized event.
    Field: crmForm
    Event: onLoad
    Error: Object expected.

     

    Does anyone know what's wrong with this code??

     

    Code Snippet

    function GetCurrentUserInfo() 

        var SERVER_URL = "http://CRM"; 

        var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
        xmlhttp.open("POST", SERVER_URL + "/mscrmservices/2006/crmservice.asmx", false); 
        xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 
        xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute"); 
         
        var soapBody = "<soap:Body>"+ 
        "<Execute xmlns='http://schemas.microsoft.com/crm/2006/WebServices'>"+ 
        "<Request xsi:type='WhoAmIRequest' />"+ 
        "</Execute></soap:Body>"; 
         
        var soapXml = "<soap:Envelope " + 
        "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' "+ 
        "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "+ 
        "xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"; 
     
        soapXml += GenerateAuthenticationHeader(); 
        soapXml += soapBody; 
        soapXml += "</soap:Envelope>"; 
     
        xmlhttp.send(soapXml); 
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); 
        xmlDoc.async=false; 
        xmlDoc.loadXML(xmlhttp.responseXML.xml); 
         
        var userid = xmlDoc.getElementsByTagName("UserId")[0].childNodes[0].nodeValue; 
        var buid = xmlDoc.getElementsByTagName("BusinessUnitId")[0].childNodes[0].nodeValue; 
        var orgid = xmlDoc.getElementsByTagName("OrganizationId")[0].childNodes[0].nodeValue; 
     
        alert("UserId: " + userid + "\r\nBusinessUnitId: " + buid + "\r\nOrganizationId: " + orgid); 
     
    }

     

    GetCurrentUserInfo();

     

     

     

    I need help!

    Jinx'd

All Replies

  • Thursday, April 17, 2008 6:14 PM
     
     

    Hi.

     

    CRM 3.0 does not have an organization and all code related to organization should be deleted

    Code Snippet

     

     

    change:  xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");

    to: xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2006/WebServices/Execute");

     

    delete : 

    soapXml += GenerateAuthenticationHeader(); 

    var orgid = xmlDoc.getElementsByTagName("OrganizationId")[0].childNodes[0].nodeValue; 

     

    change: alert("UserId: " + userid + "\r\nBusinessUnitId: " + buid + "\r\nOrganizationId: " + orgid); 
    to: alert("UserId: " + userid + "\r\nBusinessUnitId: " + buid ); 

     

     


    Cheers,

    Adi

  • Thursday, April 17, 2008 7:00 PM
     
     

    Hi,

    I just modified the code and now I'm getting the error below.  It was initally an Object Expected error, but now it's an Object required error.

     

    There was an error with this field's customized event.
    Field: crmForm
    Event: onLoad
    Error: Object required.

     

    Any ideas?

    Jinx'd

  • Monday, April 21, 2008 12:26 AM
     
     

    Hi,

     

    I also got the same error using the same script. What I did was that I put alert (xmlhttp.responseXML.xml); after the line xmlDoc.loadXML(xmlhttp.responseXML.xml); 

    Then you load the form and the alert will tell you what the error is.

     

    I found the same script you are using and it actually works in 4.0. I wonder how I can get current user's teams instead of roles? Any ideas?

     

    Thanks!