如果声明 - Javascript / CRM 2011,有没有其他方法可以做到这一点

时间:2014-03-04 17:34:17

标签: javascript if-statement dynamics-crm-2011 crm

你会怎么做这个编码?

我在CRM 2011表格上有2个输入字段 - 公司规模/文件数量。 文件有一个固定的价格 - 即如果你的公司规模是0-100 - 你购买1-10文件,如果你购买11-50它的400如果你购买51-200 = 650如果你购买200 + 1000 < / p>

每个公司规模都有不同的定价结构

示例

公司规模|| 1-10文件|| 11-50文件|| 51-200份文件|| 200+文件

我目前有以下 - 这是超级大规模

if (EMP < 101) // Company Size 1 to 100
{
    if (DOC < 11) 
            {
                CV3 = 260.39;
            }
        else if (DOC < 51)
            {
                CV3 = 433.99;
            }
        else if (DOC < 201) 
            {
                CV3 = 694.38;
            }
       else if (DOC >  201)
            {
                CV3 = 1041.57;
            }
}

else if (EMP < 201) // Company Size 101 to 200
{
    if (DOC < 11) 
            {
                CV3 = 328.12;
            }
        else if (DOC < 51)
            {
                CV3 = 546.86;
            }
        else if (DOC < 201) 
            {
                CV3 = 874.98;
            }
       else if (DOC >  201)
            {
                CV3 = 1312.47;
            }
}

3 个答案:

答案 0 :(得分:0)

您可以将公司规模输入更改为选择列表吗?这样可以减少你需要做的分支数量。

这个怎么样:

//100000001, etc. is the default value scheme for CRM picklists
var optionValues = {
    100000001: "company_0_100", 
    100000002: "company_101_200"
};

var costMap = {
    company_0_100: function (documents) {
        if (documents < 1) return -1;

        if (documents < 11) {
            return 260.39;
        } else if (documents < 51) {
            return 433.99;
        } else if (documents < 201) {
            return 694.38;
        } else {
            return 1041.57;
        }
    },
    company_101_200: function (documents) {
        if (documents < 1) return -1;

        if (documents < 11) {
            return 328.12;
        } else if (documents < 51) {
            return 546.86;
        } else if (documents < 201) {
            return 874.98;
        } else {
            return 1312.47;
        }
    }
};

使用选项列表和文档计数值检索刚到达costMap对象的成本。

var cost = costMap[picklistValue](documentCount);

以下是一个示例小提琴:http://jsfiddle.net/R9gKZ/

如果您无法使用选项列表,则可以实施选择列表Web资源并更新文本框字段。 example

答案 1 :(得分:0)

首先了解Pointy对表格的看法,以下jsfiddle首先设置一个表格,然后用于查找成本。 (底部代码)

给定的价格范围被分成10个组,重复的值给出组范围,即组的成本(11-20),(21-30),(31-40)和(41-50) )都设置为(11-50)

的价格范围

从文档数量中取1,除以10并取整数部分,将您放入表格中正确的文档数量部分。

该表适用于1-100和101-200

的公司规模

如果公司规模达到100年,那么就可以直接增加更多公司。要从公司的部分表中获取1,从大小除以100得到整数部分。如果公司规模的范围大小不同,则需要将设置表代码修改为与文档数量相同的分组。

希望可以轻松维护成本数据。

var DOC; //number of documents;
var COMP; //size of company;

numComps=2; //Number of companies in table


/*price range for each company falls into four bands
for company size
priceRange[size][0] is for 1-10 docs
priceRange[size][1] is for 11-50 docs
priceRange[size][2] is for 51-200 docs
price range[size][4] is for 200+ docs
priceRange */

priceRange=[]
//company size 1-100
priceRange[0]=[260.39,433.99,694.38,1041.57];

//company size 101-200
priceRange[1]=[382.12,546.86,874.98,1312.47];

//expand through different company sizes as needed

/*split price ranges into groups of ten and equate to form groups for document sizes and form table for companies and document sizes*/

//set up table
companies=[];
for(var size=0; size<numComps; size++) {
    companies[size]=[];
    companies[size][0]=priceRange[size][0]; //docs 1-10
    for(var docms=1; docms<5; docms++) {  //docs 11-50
        companies[size][docms]=priceRange[size][1];        
    }
    for(var docms=5; docms<20; docms++) {  //docs 51-200
        companies[size][docms]=priceRange[size][2];        
    }
    companies[size][20]=priceRange[size][3];//docs 200+   
}

function getCost(docs,comps) {
    docs=Math.floor((docs-1)/10);
    if (docs>20) {docs=20};
    comps=Math.floor((comps-1)/100);
    return(companies[comps][docs]);
}

//example

DOC=8;
COMP=43;
console.log(DOC,COMP,getCost(DOC,COMP));

答案 2 :(得分:-1)

为实现这一目标,我在这里使用Short-hand

这很可怕但也有其他方式

 CV3 =  (DOC < 11?260.39:(DOC < 51?433.99:(DOC < 51?433.99:(DOC < 201?694.38:(DOC < 201?694.38:(DOC > 201?1041.57:''))))))

完全正常工作if / else if

CV3 = (EMP < 101)?(DOC < 11?260.39:(DOC < 51?433.99:(DOC < 51?433.99:(DOC < 201?694.38:(DOC < 201?694.38:(DOC > 201?1041.57:'')))))):(EMP < 201? (DOC < 11?328.12:(DOC < 51?546.86:(DOC < 201?874.98:(DOC >  201?1312.47:'')))):'')

FULL WORKING DEMO