zoukankan      html  css  js  c++  java
  • Microsoft Dynamics CRM 2011 Javascript 根据一个lookup字段过滤另一个lookup字段

     一、需求:

    描述:过滤发货部品代码的view
    三种情况:
    1.部品更换单明细窗体上,
    发货部品代码等于退货部品代码
    2.部品更换单明细窗体上的发货部品代码等于退货部品代码的替换
    部品代码
    具体:a.如果部品更换单明细窗体上的退货部品代码等于部品替换表窗体上的部品代码,
    则部品更换单明细窗体上的发货部品代码等于部品替换表窗体上的替代部品的部品代码。
    b.如果部品更换单明细窗体上的退货部品代码等于部品替换表窗体上的替代部品的部品代码。
    ,则部品更换单明细窗体上的发货部品代码等于部品替换表窗体上的部品代码。

    二、分析:首选大家可以把用SQL语句写出来,然后在用OData查询工具处理

    SQL1:

    select
    a.new_fittings_replaceid as FittingsReplaceId
    ,b.new_material_code as MaterialCode
    ,b.new_material_name as MaterialName
    ,a.new_fittingsid as searchFittingsId
    from
    new_sys_fittings_replaceExtensionBase a
    left join new_sys_materialExtensionBase b
    on a.new_fittings_replaceid = b.new_sys_materialId
    where a.new_fittingsid='973BEBF6-0B45-E211-BA39-00155D002F0D'

    union all

    select
    a.new_fittingsid as FittingsId
    ,b.new_material_code as MaterialCode
    ,b.new_material_name as MaterialName
    ,a.new_fittings_replaceid as searchFittingsId
    from
    new_sys_fittings_replaceExtensionBase a
    inner join new_sys_materialExtensionBase b
    on a.new_fittingsid = b.new_sys_materialId
    where a.new_fittings_replaceid='973BEBF6-0B45-E211-BA39-00155D002F0D'

    union all

    select
    new_sys_materialId as MaterialId
    ,new_material_code as MaterialCode
    ,new_material_name as MaterialName
    ,new_sys_materialId as searchFittingsId
    from new_sys_materialExtensionBase
    where new_sys_materialId='973BEBF6-0B45-E211-BA39-00155D002F0D'

    SQL2:

    select * from(
    select
    a.new_fittings_replaceid as FittingsReplaceId
    ,b.new_material_code as MaterialCode
    ,b.new_material_name as MaterialName
    ,a.new_fittingsid as searchFittingsId
    from
    new_sys_fittings_replaceExtensionBase a
    left join new_sys_materialExtensionBase b
    on a.new_fittings_replaceid = b.new_sys_materialId

    union all

    select
    a.new_fittingsid as FittingsId
    ,b.new_material_code as MaterialCode
    ,b.new_material_name as MaterialName
    ,a.new_fittings_replaceid as searchFittingsId
    from
    new_sys_fittings_replaceExtensionBase a
    inner join new_sys_materialExtensionBase b
    on a.new_fittingsid = b.new_sys_materialId

    union all

    select
    new_sys_materialId as MaterialId
    ,new_material_code as MaterialCode
    ,new_material_name as MaterialName
    ,new_sys_materialId as searchFittingsId
    from new_sys_materialExtensionBase
    ) temp
    where searchFittingsId='973BEBF6-0B45-E211-BA39-00155D002F0D'

    觉得SQL1比SQL2语句效率快些,特别是上万条记录的时候。

    经过分析发现上面的SQL语句都是关联new_sys_materialExtensionBase这个表,SQL语句可以优化,如下:

    SQL3:

    select
    a.new_fittings_replaceid as FittingsReplaceId
    ,b.new_material_code as MaterialCode
    ,b.new_material_name as MaterialName
    ,a.new_fittingsid as searchFittingsId
    ,c.new_material_code as MaterialCode
    ,c.new_material_name as MaterialName
    from new_sys_fittings_replaceExtensionBase a
    left join new_sys_materialExtensionBase b on a.new_fittings_replaceid = b.new_sys_materialId
    left join new_sys_materialExtensionBase c on a.new_fittingsid = c.new_sys_materialId
    where a.new_fittingsid='973BEBF6-0B45-E211-BA39-00155D002F0D'
    or a.new_fittings_replaceid='973BEBF6-0B45-E211-BA39-00155D002F0D'
    or b.new_sys_materialId='973BEBF6-0B45-E211-BA39-00155D002F0D'
    or c.new_sys_materialId='973BEBF6-0B45-E211-BA39-00155D002F0D'

    发现查处理的数据少了一条,经过分析,原来有再一条部品替换表(new_sys_fittings_replaceExtensionBase)记录中的部品(new_fittingsid)和替代部品(new_fittings_replaceid)存在相互的关系,所以原本复杂的东西可以简单化了,主要是前面思路不清楚,考虑问题不全面。

    其实可以把Sql语句优化成如下:

    SQL4:

    select
    a.new_fittings_replaceid as FittingsReplaceId
    ,a.new_fittingsid as searchFittingsId
    from new_sys_fittings_replaceExtensionBase a
    where a.new_fittingsid='973BEBF6-0B45-E211-BA39-00155D002F0D'
    or a.new_fittings_replaceid='973BEBF6-0B45-E211-BA39-00155D002F0D'

    把FittingsReplaceId和searchFittingsId都查出来,统一为new_sys_materialExtensionBase的ID,但是必须去掉重复的ID就可以解决问题了。

    然后把SQL4转化成JS代码,为了正确性和提供工作效率,直接用Odata 查询工具(CRMToolsODataQueryDesigner)主要JS代码如下:

    三、主要代码实现:

    function addCustomShipSysMaterialView() {

    var newshippedsnnum = Xrm.Page.getAttribute("new_shipped_snnum");
    var returnssysmaterialid = Xrm.Page.getAttribute("new_returns_sys_materialid"); //退货部品代码
    var shipsysmaterial = Xrm.Page.getAttribute("new_ship_sys_material"); //发货部品代码
    var serverUrl = Xrm.Page.context.getServerUrl(); //获取路径
    var entityName = "new_sys_material";

    if (returnssysmaterialid != undefined && returnssysmaterialid.getValue() != null && returnssysmaterialid.getValue()[0].id != "" && returnssysmaterialid.getValue()[0].id != null) {
    var retrieveResult =
    retrieveMultipleEntity(serverUrl,
    "new_sys_fittings_replace",
    "new_fittingsid,new_fittings_replaceid",
    "new_fittingsid/Id eq (guid'" + returnssysmaterialid.getValue()[0].id + "') or new_fittings_replaceid/Id eq (guid'" + returnssysmaterialid.getValue()[0].id + "') and statecode/Value eq 0",
    "", "", "", "");
    }

    if (retrieveResult == null || retrieveResult.statusText != "OK") {
    alert("查询失败" + "(" + retrieveResult.response + ")");
    return;
    }

    var new_fittings_arr = "";
    new_fittings_arr = retrieveResult.response.results;


    //如果没有查到数据,那么直接把退货部品代码的值赋给发货部品代码
    if (new_fittings_arr.length == 0) {
    if (returnssysmaterialid != undefined && returnssysmaterialid != "") {
    var entityReference = getEntityReferenceObject(returnssysmaterialid.getValue()[0].id, entityName, returnssysmaterialid.getValue()[0].name);
    shipsysmaterial.setValue(entityReference);
    shipsysmaterial.fireOnChange();
    shipsysmaterial.setSubmitMode("always");
    }
    }
    else {
    if (returnssysmaterialid.getIsDirty()
    && shipsysmaterial != undefined
    && shipsysmaterial != null
    && shipsysmaterial.getValue() != null
    && shipsysmaterial.getValue()[0] != null &&
    (shipsysmaterial.getValue()[0].id != null || shipsysmaterial.getValue()[0].id != "")
    ) {
    shipsysmaterial.setValue(null);
    }
    }

    var materialsId = "";
    var aryMaterialIds = new Array();
    var in_conditions = "";
    for (var i = 0; i < new_fittings_arr.length; i++) {
    var new_fittingsid = new_fittings_arr[i].new_fittingsid.Id.toLowerCase();

    var new_fittings_replaceid = new_fittings_arr[i].new_fittings_replaceid.Id.toLowerCase();
    //全部转为小写,然后再使用indexof,这样子代码严谨些。也可以转换为/大写toUpperCase();

     

    if (materialsId.indexOf(new_fittingsid) < 0) materialsId = materialsId + new_fittingsid + ",";
    //判断 new_fittingsid 在变量字符串materialsId 中的索引值不存在,也就是去掉重复的GUID

    if (materialsId.indexOf(new_fittings_replaceid) < 0) materialsId = materialsId + new_fittings_replaceid + ",";

    }
    if (materialsId && materialsId.length > 0) {
    materialsId = materialsId.substring(0, materialsId.length - 1);
    aryMaterialIds = materialsId.split(",");
    }
    if (aryMaterialIds && aryMaterialIds.length > 0) {
    for (var j = 0; j < aryMaterialIds.length; j++) {
    in_conditions = in_conditions + "<value>" + aryMaterialIds[j] + "</value>";
    }
    } else {
    in_conditions = "<value>" + returnssysmaterialid.getValue()[0].id + "</value>"
    }

    var lookupControl = Xrm.Page.getControl("new_ship_sys_material");

    var viewId = "{00000000-0000-0000-0000-000000000001}";

    var viewDisplayName = "filter";

    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"
    + " <entity name='new_sys_material'>"
    + " <attribute name='new_material_code' />"
    + " <attribute name='new_material_type' />"
    + " <attribute name='new_material_name' />"
    + " <attribute name='new_sys_materialid' />"
    + " <order attribute='new_material_code' descending='false' />"
    + " <filter type='and'>"
    + " <condition attribute='statecode' operator='eq' value='0' />"
    + " <condition attribute='new_sys_materialid' operator='in'>"
    + in_conditions
    + " </condition>"
    + " </filter>"
    + " </entity>"
    + " </fetch>";

    //这里的fetchxml是用CRM的高级查找处理的。下载之后,用记事本打开fetchxml文件,Ctrl + H,Ctrl + F ,把"全部替换成': 如下:


     

    var layoutXml =
    "<grid name='resultset' " +
    "object='1' " +
    "jump='name' " +
    "select='1' " +
    "icon='1' " +
    "preview='1'>" +
    "<row name='result' " +
    "id='new_sys_materialid'>" +
    "<cell name='new_material_code' " +
    "width='100' />" +
    "<cell name='new_material_name' " +
    "width='100' />" +
    "</row>" +
    "</grid>";

    lookupControl.addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, false);
    lookupControl.setDefaultView(viewId);

    }

     

    最后,发现这个问题其实不难,主要是没有分析好,考虑问题不全面,思考不深,导致思路不对,思路很重要。感谢大家对我的帮助。加油吧。

  • 相关阅读:
    第六章:随机化(续1)
    第六章:随机化
    PAT甲组 1010 Radix (二分)
    关于我的2019年度总结
    Codeforces 567D:One-Dimensional Battle Ships(二分)
    Codeforces 567C:Geometric Progression(DP)
    Codeforces 567B:Berland National Library(模拟)
    HDU 4790:Just Random(容斥)
    Codeforces 450C:Jzzhu and Chocolate(贪心)
    Codeforces 450E:Jzzhu and Apples(构造,数学)
  • 原文地址:https://www.cnblogs.com/allenhua/p/3048309.html
Copyright © 2011-2022 走看看