Search code examples
javascriptdynamics-crm

Dynamics CRM - Lock Contact Fields if Account is of type "potential_client"


I'm trying to create a javascript to run "onPageLoad". The entity i want to run it is Contacts and on page load(contact page) i want to check if the Account associated with the contact is of type "potential_client".

So if Account is of type "potential_client" i want to lock fields on Contact page like firstname, lastname, email, address

EDIT - MY ATTEMPT

function onPageLoad(){
    var accountid = Xrm.Page.getAttribute("parentcustomerid").getValue()[0].id;

    Xrm.WebApi.retrieveRecord("account", accountid, "?$select=customertypecode").then(
      function success(result) {
          if (result != null) {
              if (result.customertypecode == 1 || result.customertypecode == 3) {
                Xrm.Page.ui.controls.get("firstname").setDisabled(true);
                Xrm.Page.ui.controls.get("lastname").setDisabled(true);
                Xrm.Page.ui.controls.get("emailaddress1").setDisabled(true);
              }         
            }
        },
      function(error) {
        alert(error.message);
    )
}

ERROR: Cannot read property setDisabled of null

EDIT 2 So i found out firstname and lastname lines were breaking the script with that error and i thought it was because they were already lock in configs, so i took that out and now they can be disabled, when i added those lines again, script breaks with same error "Cannot read property setDisabled of null" in those to 2 lines. Any idea why?


Solution

  • function onPageLoad(){
        var accountid = Xrm.Page.getAttribute("parentcustomerid").getValue()[0].id;
    
        Xrm.WebApi.retrieveRecord("account", accountid, "?$select=customertypecode").then(
          function success(result) {
              if (result != null) {
                  if (result.customertypecode == 1 || result.customertypecode == 3) {
                    Xrm.Page.ui.controls.get("fullname").setDisabled(true);
                    Xrm.Page.ui.controls.get("emailaddress1").setDisabled(true);
                  }         
                }
            },
          function(error) {
            alert(error.message);
        )
    }
    

    So this is the script that solves my problem, i found out that i had to set disable the "fullname" instead of setting "firstname" and "lastname" separately.

    Thanks for all the help guys.