locked
CRM 4.0 - Lock a single field onLoad event, only if field is not blank RRS feed

  • Question

  • I am trying to lock a single field by using the onLoad event to trigger disabling the field if the user is not a system admin.  This works, however, I also need to be able to specify that if the field is blank, the field is enabled for all users (or possibly "if the field has specific values, then check user role").

    Any help would be appreciated.

    Thank you

    Erin Clements

    Monday, July 8, 2013 10:59 PM

All replies

  • For all users, the resolution is simple: Use JavaScript to check for a value in the field, and set the state. A la:

    if (Xrm.Page.getAttribute('some_field').getValue() != null)
    {
      Xrm.Page.getControl('some_field').setDisabled(true);
    } else {
      Xrm.Page.getControl('somefield').setDisabled(false);
    }

    Beyond that, when looking for user roles, you can use the Xrm.Page.context.getUserRoles() method to return an array of Role GUIDs to compare with.  The advantage here, is that if you need to transport roles between systems, using a Solution will keep the GUIDs the same (for all custom roles; I don't think the "out of box" roles have GUID fidelity, and probably for good reason).


    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.

    Monday, July 8, 2013 11:07 PM
    Moderator
  • Dave,

    I appreciate the quick response. Since I am using CRM 4.0, that would mean I can't use the Xrm.page identifier, correct?  See below for the code I am currently using.

    if(crmForm.FormType != 1)
    {
      if(UserHasRole("Benefits Broker System Administrator") == false)
        {
            crmForm.all.bb_groupsize.Disabled = true;
        }
    }
    
    function UserHasRole(roleName)  
    {  
     //get Current User Roles, oXml is an object  
     var oXml = GetCurrentUserRoles();  
     if(oXml != null)  
     {  
      //select the node text  
      var roles = oXml.selectNodes("//BusinessEntity/q1:name");  
      if(roles != null)  
      {  
       for( i = 0; i < roles.length; i++)  
       {  
        if(roles[i].text == roleName)  
        {  
         //return true if user has this role  
         return true;  
        }  
       }  
      }  
     }  
     //otherwise return false  
     return false;  
    }  
      
    function GetCurrentUserRoles()  
    {  
     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>role</q1:EntityName>" +  
     " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +  
     " <q1:Attributes>" +  
     " <q1:Attribute>name</q1:Attribute>" +  
     " </q1:Attributes>" +  
     " </q1:ColumnSet>" +  
     " <q1:Distinct>false</q1:Distinct>" +  
     " <q1:LinkEntities>" +  
     " <q1:LinkEntity>" +  
     " <q1:LinkFromAttributeName>roleid</q1:LinkFromAttributeName>" +  
     " <q1:LinkFromEntityName>role</q1:LinkFromEntityName>" +  
     " <q1:LinkToEntityName>systemuserroles</q1:LinkToEntityName>" +  
     " <q1:LinkToAttributeName>roleid</q1:LinkToAttributeName>" +  
     " <q1:JoinOperator>Inner</q1:JoinOperator>" +  
    
    
     " <q1:LinkEntities>" +  
     " <q1:LinkEntity>" +  
     " <q1:LinkFromAttributeName>systemuserid</q1:LinkFromAttributeName>" +  
     " <q1:LinkFromEntityName>systemuserroles</q1:LinkFromEntityName>" +  
     " <q1:LinkToEntityName>systemuser</q1:LinkToEntityName>" +  
     " <q1:LinkToAttributeName>systemuserid</q1:LinkToAttributeName>" +  
     " <q1:JoinOperator>Inner</q1:JoinOperator>" +  
     " <q1:LinkCriteria>" +  
     " <q1:FilterOperator>And</q1:FilterOperator>" +  
     " <q1:Conditions>" +  
     " <q1:Condition>" +  
     " <q1:AttributeName>systemuserid</q1:AttributeName>" +  
     " <q1:Operator>EqualUserId</q1:Operator>" +  
     " </q1:Condition>" +  
     " </q1:Conditions>" +  
     " </q1:LinkCriteria>" +  
     " </q1:LinkEntity>" +  
     " </q1:LinkEntities>" +  
     " </q1:LinkEntity>" +  
     " </q1:LinkEntities>" +  
     " </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;  
     return(resultXml);  
    }
    

    Sorry for the long code. So what I tried was to add in the code you provided at the front end, but now there is an error when loading the form. 

    if (crmForm.getAttribute('bb_groupsize').getValue() != null)
    {
      crmForm.getControl('bb_groupsize').setDisabled(true);
    } else {
      crmForm.getControl('bb_groupsize').setDisabled(false);
    }
    
    if(crmForm.FormType != 1)
    {
        if(UserHasRole("Benefits Broker System Administrator") == false)
        {
            crmForm.all.bb_groupsize.Disabled = true;
        }
    }
    
    function UserHasRole(roleName)  

    I am reasonably sure that I am really misunderstanding this - any clarification would be fantastic.

    Also, is there any way to verify that the onLoad code will work without publishing it, testing it, and if it errors removing the code and re-publishing?\

    Thanks again for all your help.

    Erin Clements

    Tuesday, July 9, 2013 2:33 PM
  • Sorry, I wanted to credit Frederico Stockchneider since he was the one who provided the bulk of the first code snippet.

    Erin

    Avatar of Frederico Stockchneider

    Frederico Stockchneider

    Partner

    270 Points700
    Recent Achievements
    First Helpful VoteForums Answerer IICode Answerer I
    Tuesday, July 9, 2013 2:55 PM
  • I'm so sorry for not seeing "CRM 4.0" in the thread header.  I'll reply back with CRM 4.0 guidance as soon as possible.

    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.

    Tuesday, July 9, 2013 7:12 PM
    Moderator
  • Erin,

    have you checked this :

    if (crmForm.all.bb_groupsize.DataValue != null)
    {
    	crmForm.all.bb_groupsize.disabled = true;
    } else {
    	crmForm.all.bb_groupsize.disabled = false;
    }
    
    if(crmForm.FormType != 1)
    {
        if(UserHasRole("Benefits Broker System Administrator") == false)
        {
    	crmForm.all.bb_groupsize.disabled = true;
        }
    }
    
    function UserHasRole(roleName)  



    Best regards,
    Vishal Swami

    This posting is provided "AS IS" with no warranties, and confers no rights.


    Tuesday, July 9, 2013 7:47 PM
  • Vishal,

    Thank you.  Unfortunately, when I input that code, it locks the field regardless of whether is it blank or not.  Does it matter if it is a picklist?  In otherwords, do you think I would need to put something else where the code specifies a null value?

    When I look at the actual attribute for that field, the default value is "Unassigned Value".

    if (crmForm.all.bb_groupsize.DataValue != null)
    {
    	crmForm.all.bb_groupsize.disabled = true;
    } else {
    	crmForm.all.bb_groupsize.disabled = false;
    }
    

    Sould I make it read (somethig along the lines of:

    if (crmForm.all.bb_groupsize.DataValue != Unassigned)
    {
    	crmForm.all.bb_groupsize.disabled = true;
    } else {
    	crmForm.all.bb_groupsize.disabled = false;
    }
    

    Please advise.  Thank you!!

    Erin Clements

    Tuesday, July 9, 2013 8:01 PM
  • If you're still doing the User Role validation, are you certain that the role is assigned to the account you're using? "null" is equivalent to "Unassigned Value" for a picklist.

    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.

    Friday, July 12, 2013 7:16 PM
    Moderator
  • Dave,

    My account is a system admin, but I have a coworker who checks the result of the change who is not. 

    Unfortunately, it looks like maybe there is an error in how I am entering the code.

    Thanks for your help!

    Erin

    Friday, July 12, 2013 7:39 PM