Dynamics CRM JavaScript查询实体信息

    /*
    方法描述: 单条结果的查询。根据实体主键值(entityGuid)查询指定实体(entityName)的记录
    */
    function RetrieveSingleEntity(entityName, entityGuid) {
        var retrieveReq = new XMLHttpRequest();
        retrieveReq.open("GET", GetServerUrl() + "/" + entityName + "Set(guid'" + entityGuid + "')", false);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        retrieveReq.send();
        var retrieved = JSON.parse(retrieveReq.responseText).d;
        return retrieved;
    }

    /*
    方法描述: 多条结果的查询。url为OData拼接地址
    */
    function RetrieveMultipleEntity(url) {
       var retrieveReq = new XMLHttpRequest();
        retrieveReq.open("GET", frensworkz.GetServerUrl() + "/" + url, false);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        retrieveReq.send();
        var retrieved = JSON.parse(retrieveReq.responseText).d;
        return retrieved;
    }

    function GetServerUrl() {
        var serverUrl = Xrm.Page.context.getClientUrl();
        if (serverUrl.match(/\/$/)) {
            serverUrl = serverUrl.substring(0, serverUrl.length - 1);
        }
        return serverUrl + "/XRMServices/2011/OrganizationData.svc";

    }

 

//demo 多条结果的查询
       var odata = "new_personinfoSet?$select=new_personinfoId,new_name&"
+ "$filter=new_position/Value eq 1 and statecode/Value eq 0 and new_plant/Id eq (guid'" + Xrm.Page.getAttribute("new_internalresourceid").getValue()[0].id
+ "')";
        var Entcoll = RetrieveMultipleEntity(odata);
        if (Entcoll.results.length > 0) {
            Xrm.Page.getAttribute("new_salesregionmanager").setValue([{ id: Entcoll.results[0].new_personinfoId, name: Entcoll.results[0].new_name, entityType: "new_personinfo" }]);
            Xrm.Page.getAttribute("new_salesregionmanager").setSubmitMode("always");
        }

//demo 单条结果的查询
  var account = Xrm.Page.getAttribute("new_accountid").getValue();
    if (account != null && account[0].id != null && account[0].id != undefined)
    {
        var entity = RetrieveSingleEntity("Account", account[0].id);
        if (entity != null)
        {
            Xrm.Page.getAttribute("new_customername").setValue(entity.new_companyname);
            Xrm.Page.getAttribute("new_customername").setSubmitMode("always");
            Xrm.Page.getAttribute("new_custmoerenglishname").setValue(entity.new_compnayenglishname);
            Xrm.Page.getAttribute("new_custmoerenglishname").setSubmitMode("always");

        }
    }

 

你可能感兴趣的:(Dynamics,CRM,JavaScript)