While this is a scenario that could potentially be achieved using real time workflows, if you want things to happen really real-time, you have to go back to using JScript.

For this example we have an entity called Rule, with a reference to an Organization (lookup to Account). We also have an entity called Rule Condition, which relates to a Rule. On the Rule Condition, once we populate the Rule, we need to also populate the Organization based on the Organization selected on the Rule related entity.

The process is relatively simple.

1. Retrieve the value populated in the Rule field on Rule Condition.

2. Make a call to retrieve the values from the Rule, specifically the Organization.

3. Create and populate the Organization on Rule Condition based on the Organization retrieved from Rule.

And here’s the code. The first function retrieves the Rule ID, and passes it to the second function, which, in turn, makes the call and retrieves the Organization from the Rule, and populates the Organization on the Rule Condition.

Attach this first function to the onChange event of the Rule field on the Rule Condition form:

function PopulateAuthorityFromRuleAuthority() {
    var _lookupObject = Xrm.Page.getAttribute("new_rule_id");

    if (_lookupObject != null) {
        // debugger;

        var _lookupObjectValue = _lookupObject.getValue();

        if (_lookupObjectValue != null) {
            var _ruleName = _lookupObjectValue[0].name;
            var _ruleId = _lookupObjectValue[0].id;
            var _ruleType = _lookupObjectValue[0].entityType;

            /* Retrieve the Rule values, including the Authority */
            GetRuleAuthority(_ruleId);

        }
    }
}

And place this next function in the same web resource:

function GetRuleAuthority(ruleId) {

    var oDataURI = Xrm.Page.context.getClientUrl()
        + "/XRMServices/2011/OrganizationData.svc/"
        + "new_ruleSet(guid’" + ruleId + "’)"
        + "?$select="
        + "new_organization";

    var req = new XMLHttpRequest();
    req.open("GET", encodeURI(oDataURI), true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/html; charset=utf-8");
    req.onreadystatechange = function () {

        if (this.readyState == 4) {
            req.onreadystatechange = null; 
            if (this.status == 200) {
                
                var _ruleOrganization = new Array();
                _ruleOrganization [0] = new Object();
                var _organization= JSON.parse(this.responseText).d.new_organization;
                _ruleOrganization [0].id = _organization.Id;
                _ruleOrganization [0].name = _organization.Name;
                _ruleOrganization [0].entityType = _organization.LogicalName;
                
                Xrm.Page.getAttribute("new_organization").setValue(_ruleOrganization);
                Xrm.Page.data.save();
                
            }
            else {
                /* errorCallback(accountId);
                   an error occured, handle it as you wish. */
            }
        }
    };
    req.send();

}

If you find working with lookups a little more daunting then working with other fields, this should help.

Enjoy!

Advertisements