locked
CRM 2011 script converted from CRM 4.0 RRS feed

  • Question

  • I have a script that run in CRM 4.0 that work great.

    This script runs on the email form onload event.  If the email regarding field is blank then if the current signed in user is not the "from" or in the "to" list then hid the message field.

    This work just fine in CRM 4.0, after running the script through the CRM2011 JavaScript Converter and installing it on CRM 2011, it gve me the error "Object Expected".  The full script is below.  If anyone is familiar with CRM 2011 scripts can you see anything that needs to change?  Thanks for the help.

    var xml = "" +
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<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\">" +
    Xrm.Page.context.getAuthenticationHeader() +
    " <soap:Body>" +
    " <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
    " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +
    " <q1:EntityName>systemuser</q1:EntityName>" +
    " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
    " <q1:Attributes>" +
    " <q1:Attribute>businessunitid</q1:Attribute>" +
    " <q1:Attribute>firstname</q1:Attribute>" +
    " <q1:Attribute>fullname</q1:Attribute>" +
    " <q1:Attribute>lastname</q1:Attribute>" +
    " <q1:Attribute>organizationid</q1:Attribute>" +
    " <q1:Attribute>systemuserid</q1:Attribute>" +
    " </q1:Attributes>" +
    " </q1:ColumnSet>" +
    " <q1:Distinct>false</q1:Distinct>" +
    " <q1:Criteria>" +
    " <q1:FilterOperator>And</q1:FilterOperator>" +
    " <q1:Conditions>" +
    " <q1:Condition>" +
    " <q1:AttributeName>systemuserid</q1:AttributeName>" +
    " <q1:Operator>EqualUserId</q1:Operator>" +
    " </q1:Condition>" +
    " </q1:Conditions>" +
    " </q1:Criteria>" +
    " </query>" +
    " </RetrieveMultiple>" +
    " </soap:Body>" +
    "</soap:Envelope>" +
    "";

    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);

    var resultXml = xmlHttpRequest.responseXML;
    var entityNode = resultXml.selectSingleNode("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");

    var firstNameNode = entityNode.selectSingleNode("q1:firstname");
    var lastNameNode = entityNode.selectSingleNode("q1:lastname");
    var fullNameNode = entityNode.selectSingleNode("q1:fullname");
    var systemUserIdNode = entityNode.selectSingleNode("q1:systemuserid");
    var businessUnitIdNode = entityNode.selectSingleNode("q1:businessunitid");
    var organizationIdNode = entityNode.selectSingleNode("q1:organizationid");


    // function to hide the email message field

    gHideField = function (name){
         var field = document.getElementById(name+'_d');
         var fieldcapt = document.getElementById(name+'_c');
         if(field != null){field.style.display='none';}
         if(fieldcapt != null){fieldcapt.style.display='none';}
     };

    // Get the Regarding field
    var TheRegarding = new Array;
    TheRegarding = null;

    TheRegarding= Xrm.Page.getAttribute("regardingobjectid").getValue();

    // If the regarding field IS blank then check to see if this user should be able to see the message

    if (TheRegarding == null) {

       // Get the email From user id, if there is one.
        var lookUpFrom = new Array();
        lookUpFrom = Xrm.Page.getAttribute("from").getValue();
        var pFromId = lookUpFrom[0].id;

        sFlag = "Hide"   // Default to hide the field, unless we prove this user may see the Message field

      // If the "From" is also the Current User then allow them to see the Message field
       if(systemUserIdNode.text == pFromId) {

         sFlag = "Visible";

       }     // if(systemUserIdNode.text == pFromId) {

       // Ok the "From" is not the current user so lets loop through the "To" user id's to see if the Current User is one of the "To" people
       if (sFlag == "Hide") {

           var lookUpTo = new Array();
           lookUpTo = Xrm.Page.getAttribute("to").getValue();

           iMax = lookUpTo.length - 1;     // How many "To" addresses are there?
          for (i = 0; i<=iMax;i++) {

              var pToId = lookUpTo[i].id;         // Get "To" user id

              if(systemUserIdNode.text == pToId) {        // Compare "To" user Is to Current System User Id
                    sFlag = "Visible";                                     // This Current User Id matchs this "To" user is, let them see the Message field.
                    i=iMax;

              }    // if(systemUserIdNode.text == pToId) {

          }  // For Next i loop

       }    //if (sFlag == "Hide") {

       if (sFlag == "Hide") {         // There was NO regarding field and the Current User Id is NOT the From or any of the To people, so hide the Message field

            gHideField ('Description');

       }   // if (sFlag == "Hide") {

    }   //if (TheRegarding == null) {

    Monday, March 14, 2011 1:50 PM

Answers

All replies

  • You might have other issues with your code that I haven't noticed, but there is one issue that stands out.

    In 2011 you need to package the code into a function within a .js file when you send it up.

    See the 2011 example on this blog post:

    http://mileyja.blogspot.com/2010/12/map-custom-attributes-from-opportunity.html


    Jamie Miley
    http://mileyja.blogspot.com
    LinkedIn Profile
    Monday, March 14, 2011 2:07 PM
    Moderator
  • The script ran successfully in CRM 4.0 but after upgrading to CRM 2011 the script does not seem to work at all.  Below is the upgraded script. 

    Has some routines changed or are not available anylonger?  Can "Debugger;" be added to go to debug mode?

    It is flagged as enabled.   Is there some other settings needing to be set?

    Thanks

    function Form_onload()
    {
    var xml = "" +
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<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\">" +
    GenerateAuthenticationHeader() +
    " <soap:Body>" +
    " <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
    " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +
    " <q1:EntityName>systemuser</q1:EntityName>" +
    " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
    " <q1:Attributes>" +
    " <q1:Attribute>businessunitid</q1:Attribute>" +
    " <q1:Attribute>firstname</q1:Attribute>" +
    " <q1:Attribute>fullname</q1:Attribute>" +
    " <q1:Attribute>lastname</q1:Attribute>" +
    " <q1:Attribute>organizationid</q1:Attribute>" +
    " <q1:Attribute>systemuserid</q1:Attribute>" +
    " </q1:Attributes>" +
    " </q1:ColumnSet>" +
    " <q1:Distinct>false</q1:Distinct>" +
    " <q1:Criteria>" +
    " <q1:FilterOperator>And</q1:FilterOperator>" +
    " <q1:Conditions>" +
    " <q1:Condition>" +
    " <q1:AttributeName>systemuserid</q1:AttributeName>" +
    " <q1:Operator>EqualUserId</q1:Operator>" +
    " </q1:Condition>" +
    " </q1:Conditions>" +
    " </q1:Criteria>" +
    " </query>" +
    " </RetrieveMultiple>" +
    " </soap:Body>" +
    "</soap:Envelope>" +
    "";

    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);

    var resultXml = xmlHttpRequest.responseXML;
    var entityNode = resultXml.selectSingleNode("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");

    var firstNameNode = entityNode.selectSingleNode("q1:firstname");
    var lastNameNode = entityNode.selectSingleNode("q1:lastname");
    var fullNameNode = entityNode.selectSingleNode("q1:fullname");
    var systemUserIdNode = entityNode.selectSingleNode("q1:systemuserid");
    var businessUnitIdNode = entityNode.selectSingleNode("q1:businessunitid");
    var organizationIdNode = entityNode.selectSingleNode("q1:organizationid");


    // function to hide the email message field
    debugger;
    gHideField = function (name){
         var field = document.getElementById(name+'_d');
         var fieldcapt = document.getElementById(name+'_c');
         if(field != null){field.style.display='none';}
         if(fieldcapt != null){fieldcapt.style.display='none';}
     };

     

    // Get the Regarding field
    var TheRegarding = new Array;
    TheRegarding = null;
    debugger;
    TheRegarding= crmForm.all.regardingobjectid.DataValue;

    // If the regarding field IS blank then check to see if this user should be able to see the message

    if (TheRegarding == null) {

       // Get the email From user id, if there is one.
        var lookUpFrom = new Array();
        lookUpFrom = crmForm.all.from.DataValue;
        var pFromId = lookUpFrom[0].id;

        sFlag = "Hide"   // Default to hide the field, unless we prove this user may see the Message field

      // If the "From" is also the Current User then allow them to see the Message field
       if(systemUserIdNode.text == pFromId) {

         sFlag = "Visible";

       }     // if(systemUserIdNode.text == pFromId) {

       // Ok the "From" is not the current user so lets loop through the "To" user id's to see if the Current User is one of the "To" people
       if (sFlag == "Hide") {

           var lookUpTo = new Array();
           lookUpTo = crmForm.all.to.DataValue;

           iMax = lookUpTo.length - 1;     // How many "To" addresses are there?
          for (i = 0; i<=iMax;i++) {

              var pToId = lookUpTo[i].id;         // Get "To" user id

              if(systemUserIdNode.text == pToId) {        // Compare "To" user Is to Current System User Id
                    sFlag = "Visible";                                     // This Current User Id matchs this "To" user is, let them see the Message field.
                    i=iMax;

              }    // if(systemUserIdNode.text == pToId) {

          }  // For Next i loop

       }    //if (sFlag == "Hide") {

       if (sFlag == "Hide") {         // There was NO regarding field and the Current User Id is NOT the From or any of the To people, so hide the Message field

            gHideField ('Description');

       }   // if (sFlag == "Hide") {

    }   //if (TheRegarding == null) {
    }

    Monday, April 11, 2011 5:20 PM