【问题标题】:Apex getter returning wrong valueApex getter 返回错误值
【发布时间】:2012-04-20 13:28:33
【问题描述】:

我的 salesforce 页面中有 javascript 函数来验证其中一位联系人是否有未结案件。此函数调用顶点 getter 来获取值。我面临的问题是顶点吸气剂总是返回错误的布尔值。我尝试调试它,似乎一切正常,但由于某种原因返回的布尔值错误。

顶点函数:

 public Boolean openCase{
   get{

        if (Contacts.size() > 0){
            for(cContact wContact: dicContacts.values()){
                 if(wContact.selected){                     
                     if(wContact.con.account.Number_of_open_Financial_Review_Cases__c > 1){

                        return true;
                    } 
                 }           
            }                     
    return false;
   }
   set{}

}

js函数:

 function validateOpenCases(sendEmail){

    doIt = true;
    oc = {!openCase};   // <<== problem here
    alert(oc);
      if (oc)
      {
       doIt=confirm('blabla?');
      }
      if(doIt){
            // do stuff
      }
      else{
        // do nothing
      }
  }

【问题讨论】:

  • 它应该是一个布尔值吗?
  • 我相信”var oc = openCase(); 更有意义,imo。
  • 我试过了,没有任何区别。

标签: javascript salesforce apex-code


【解决方案1】:

您不应该直接在 JavaScript 中绑定 Apex 对象/变量(就像您有 {!openCase}; 一样)。我以前遇到过很多问题。请改用JavaScript RemotingAjax Toolkit


更新

另一个选项是使用隐藏的 Visualforce 输入来存储绑定的 Visualforce 值。然后你可以在你的 JavaScript 中获得这个值。

这是一个例子:

<apex:page controller="myController">
    <script>
        function getInputEndingWith(endsWith)
        {
            // put together a new Regular Expression to match the 
            // end of the ID because Salesforce prepends parent IDs to
            // all elements with IDs
            var r = new RegExp("(.*)"+endsWith+"$"); 

            // get all of the input elements
            var inputs = document.getElementsByTagName('input');

            // initialize a target
            var target;

            // for all of the inputs
            for (var i = 0; i < inputs.length; ++i)
            {
                // if the ID of the input matches the
                // Regular Expression (ends with)
                if (r.test(inputs[i].id))
                {
                    // set the target
                    target = inputs[i];
                    // break out of the loop because target 
                    // was found
                    break; 
                }
            }

            // return the target element
            return target;
        }

        function validateOpenCases(sendEmail)
        {
            doIt = true;
            oc = getInputEndingWith("OpenCase").value;

            alert(oc);

            if (oc === "true") {
                doIt = confirm('Are you sure?'); 
            }
            if (doIt) {
                // do stuff
            }
            else {
                // do nothing
            }
        }
    </script>

    <apex:form>
        <apex:outputpanel>
            <apex:inputhidden id="OpenCase" value="{!openCase}" />
        </apex:outputpanel>
        <input type="button" class="btn" onclick="validateOpenCases('Send');" value="Validate" />
    </apex:form>
</apex:page>

【讨论】:

  • 我该如何使用它?我还是 Salesforce 的新手,我没有找到很多 JavaScript 远程处理或 Ajax 工具包的示例。你能告诉我如何应用它吗?
  • 我刚刚意识到我提供的一个链接不能正常工作,这里有一个更好的,它有一个例子:salesforce.com/us/developer/docs/pages/…
  • 这也是一个 Ajax 工具包示例:salesforce.com/us/developer/docs/ajax/Content/…
  • JavaScript 远程处理对我来说效果不佳,因为我必须将大部分变量设置为静态,这对我的应用程序不起作用。
  • 谢谢。不过我有一个问题,myHiddenInput 是什么,你为什么不使用 getInputEndingWith(endsWith) 中传递的参数以及 if (/(.*)WidgetName$/g.exec(inputs[i].id)) 使用的参数。
猜你喜欢
  • 1970-01-01
  • 2019-09-17
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-23
  • 2019-02-03
相关资源
最近更新 更多