Customer Engagement & Dynamics CRM Forum

Connections quetsion...

  • 1.  Connections quetsion...

    Posted 9 days ago
    ​Hello,

    I am currently on CRM 2016 on premise and I am wondering if there is a way to grab the account name and populate that name into a field on the connection record called "Related Account".

    Example 1 would be:  I am on the Account record, from here I go to the connections and create a new connection.  I have a Related account field on the connection record that is a lookup to the account entity.  I want this field to be prefilled in from the client name field on the Account entity so that the user doesn't have to look up the account manually.

    I have a lot of different entities that has the account name on them, so no matter what entity I am starting from, if I go to connections and create a new connection, I want the related account field to pre-populated with the account name of the previous record that I just came from.

    Can this be done?  I see that you can't create business rules on connections. Below is what I have so far.  When I am on the account or on the accumulator record and go to connections, the related account field on the connection record does get filled in, so that works great.  However, when I am on the eligibility record and go to the connections, the related account field stays blank, and not sure why.  Any help is appreciated.  Maybe there is an easier way to do this as well?

    function onloadform(){
    debugger;
      if (Xrm.Page.data.entity.attributes.get("record1id").getValue() != null) {
          var connectionFrom = Xrm.Page.data.entity.attributes.get("record1id").getValue();
          var fromId = Xrm.Page.data.entity.attributes.get("record1id").getValue()[0].id;
          fromId= fromId.slice(1,-1);
          var fromtype = Xrm.Page.data.entity.attributes.get("record1id").getValue()[0].entityType;
          if (fromtype == "account")
       {
              Xrm.Page.getAttribute("nhs_relatedaccount").setValue(connectionFrom);
          }
          else if (fromtype == "new_accumulator")
       {
              PopulateAccountName(fromId);
          }
       else if (fromtype == "new_eligibility")
       {
        PopulateEligibilityAccountName(fromId);
      }
    }


    function PopulateAccountName(fromId){
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/new_accumulators(" + fromId + ")?$select=_new_client_value", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var result = JSON.parse(this.response);

    var retrivedvalue= result["_new_client_value"];
     var retrivedformatedvalue= result["_new_client_value@OData.Community.Display.V1.FormattedValue"];
     if (retrivedvalue!= null) {
     var value = new Array();
     value[0] = new Object();
     value[0].id = retrivedvalue;
     value[0].name = retrivedformatedvalue;
     value[0].entityType = "account";

    Xrm.Page.getAttribute("nhs_relatedaccount").setValue(value);
    }
    }
    else{
                Xrm.Utility.alertDialog(this.statusText);
        }
        }
    };
    req.send();
    }

    function PopulateEligibilityAccountName(fromId)
    {
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/new_eligibilities(" + fromId + ")?$select=_new_client_value", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var result = JSON.parse(this.response);

    var retrivedvalue= result["_new_client_value"];
     var retrivedformatedvalue= result["_new_client_value@OData.Community.Display.V1.FormattedValue"];
     if (retrivedvalue!= null) {
     var value = new Array();
     value[0] = new Object();
     value[0].id = retrivedvalue;
     value[0].name = retrivedformatedvalue;
     value[0].entityType = "account";

    Xrm.Page.getAttribute("nhs_relatedaccount").setValue(value);

            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    }
    }
    }

    ------------------------------
    Kevin Jicha
    Application System Administrator
    Navitus Health Solutions
    Appleton WI
    ------------------------------