1

我在 CRM 表单中创建了 3 个整数字段:var1 var2 和 result

我需要从 var1 中减去 var 2 并在结果字段中显示结果

在表单事件中添加了 Jscript 代码,在 var1 和 var2 字段中添加了 onchange 事件。

我收到错误消息:
无法获取属性“执行”的值:对象为空或未定义

这是我的 JScript:

function calculates( )
{

    var val1 = Xrm.Page.entity.attributes.get(safe_val1).getValue();
    var val2 = Xrm.Page.entity.attributes.get(safe_val2).getValue();

    if(val1=null) return;
    if(val2=null) return;

    var result = val1 - val2;

    Xrm.Page.entity.attributes.get(safe_result).setValue(result);
}

提前感谢任何回答我问题的人!

4

2 回答 2

1

的基本语法 Xrm.Page.data.entity.attributes.get要求您传递字段的名称。

例如,假设我在Contact表格上并且我想抢占该firstname字段,我会使用

 Xrm.Page.data.entity.attributes.get('firstname');

在上面的示例中,您将传递给variables被调用的safe_val1and safe_val2。没有建议这些在任何地方初始化,所以这意味着你正在通过null这将使 CRM 哭泣。

您需要查看要传入的字段的名称并改为使用它们。

编辑:刚刚注意到你错过了这个data对象

edit2:或者,您可以使用速记/快捷方式

Xrm.Page.getAttribute('new_fieldname');
于 2013-02-21T14:39:57.930 回答
0

I'll give it a whack, but I'm not in front of the computer so it's just a general suggestion (that might resolve your issue).

There are some issues with the code. I'd rewrite it as follows and get back to tell what's happening then.

function calculate() {
  // Make sure that you refer the right fields.
  var val1 = Xrm.Page.entity.attributes.get("var1").getValue();
  var val2 = Xrm.Page.entity.attributes.get("var2").getValue();

  // Yes! Triple equality sign. I'm not BS'ing. I'm JS'ing.
  if(val1 === null || val2 === null) return;

  // Make sure that the computation's been carried out properly.
  var result = val1 - val2;
  alert(result);

  Xrm.Page.entity.attributes.get(safe_result).setValue(result);
}

If you want to tear out your eyes from the sockets watching the ugly and weird

if(val1 === null || val2 === null) return;

you might want to exchange it for

if(!val1 || !val2) return;

which is also ugly and weird but less. And if this fails, comment everything out and do exactly the following. Just to make sure that we eliminate other issues you might have.

function calculate(){
  alert("Konrad Viltersten is godlike but humble.");
}

If it works, you go shotgun and add one line at a time to see when the weird stuff starts happening. (JS - where the joy of programming goes to die.)

于 2013-02-21T21:48:16.933 回答