locked
Jscript to identify who the user is RRS feed

  • Question

  • I've got a small problem that i need to jscript my way out of. Does anyone know how to write a script to have crm look to see who the user is and compare it against another value.

    Ideally i want on the OnLoad for CRM to identify who the user is (either by name or ID) and compare it to the 'owner' field. If the person is the same i want something special to happen (make fields visible etc).

    I can't use the code to grab the person's role. It's complicated but i want someone who has a certain security role to have all fields disabled on an opportunity but a few, unless they're also the owner of the opp then they can change everything.
    Thursday, October 1, 2009 7:10 PM

Answers

  • The first link contains following function which returns the user id of the calling user (This was the reason, why I posted the links)

    function getUserId()
    {
          try
          {
                var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                xmlhttp.open("POST", "../../mscrmservices/2007/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/2007/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;
                return userid;
          }
          catch(e)
          {
                return null;
          }
    } 
    You can take this user id and compare it with the owner id.
    • Marked as answer by Sleahy Friday, October 2, 2009 6:06 PM
    Thursday, October 1, 2009 9:24 PM

All replies

  • Again not interested in Security role based permissions. They're already in place. I have a tab on the opportunity form that i only want 2 people to have access to for every single opportunity created, but they can't have write permissions on the rest of the opportunity if they're not the owner to protect other sales people's data.

    So the role has write permissions on all opps at the organization level. Then a J script that runs that says if user has role 'X' disable all fields on all opps. After that comes another script saying if they have role 'X' (same as 'x' above) make fields on the tab editable. That would mean they can edit the fields they're suppose to and save all opportunities. However, it also means that they can't write on their own opportunities which they should be able to.

    I was looking for a script that was something like
    If User = Owner && role 'x' then they have access to edit all the fields in their opp.
    Else User Not Equal Owner && has role 'x' they have write permissions to the one tab but not the rest of opp.

    It's a really confusing situation that the basic script for security roles doesn't cover all the bases.
    Thursday, October 1, 2009 7:44 PM
  • The first link contains following function which returns the user id of the calling user (This was the reason, why I posted the links)

    function getUserId()
    {
          try
          {
                var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                xmlhttp.open("POST", "../../mscrmservices/2007/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/2007/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;
                return userid;
          }
          catch(e)
          {
                return null;
          }
    } 
    You can take this user id and compare it with the owner id.
    • Marked as answer by Sleahy Friday, October 2, 2009 6:06 PM
    Thursday, October 1, 2009 9:24 PM