【问题标题】:Visualforce page not rendering values from controllerVisualforce 页面未从控制器呈现值
【发布时间】:2020-12-05 18:16:23
【问题描述】:

在我目前陷入的场景中需要您的输入。以下是详细信息。感谢您的时间和所有投入。

  • 目前我可以看到在控制器中检索到的值,但它们没有显示在 visualforce 页面上。

要求:我需要通过电子邮件发送大量选定的联系人。当没有给选定联系人的电子邮件时,我们需要在 UI 上填充没有电子邮件的联系人的姓名。我能够完成要求的第一部分,但坚持在视觉力量页面上显示联系人姓名。

  1. 列表按钮:调用firstVF视觉力页面的BulkEmailTest。

  2. 第一个VF代码:

     <apex:page standardController="Contact" extensions="FirstController" recordSetVar="listRecs" 
         action="{!send}">
         Emails are being sent!
         <script> window.history.back();
         </script>
     </apex:page>
    
  3. FirstController 代码:为了简化代码,我为有电子邮件的联系人编辑了 sn-p,因为我们的优先级仅与没有电子邮件的联系人有关。

     public with sharing class FirstController
     {
     public List<Contact> noEmail {get;set;} 
     public Contact contact;
     public List<Contact> allcontact {get; set;} 
     Id test;
     public Contact getAllContact() {
             return contact;
     }
    
     ApexPages.StandardSetController setCon; 
     ApexPages.StandardController setCon1; 
     public static Boolean err{get;set;}
     public FirstController(ApexPages.StandardController controller) 
     {
    
              setCon1 = controller;  
     } 
     public FirstController(ApexPages.StandardSetController controller)
      {
    
              setCon = controller;  
     } 
      public PageReference cancel()
      {
             return null;
     }
     public FirstController() 
     {
    
     }
     public PageReference send()
      {
      noEmail = new List<Contact>();
       set<id> ids = new set<id>();
    
      for(Integer i=0;i<setCon.getSelected().size();i++){
                     ids.add(setCon.getSelected()[i].id); 
             }
              if(ids.size() == 0){
                     err = true;
                     return null;
             }
    
       List<Contact> allcontact = [select Email, Name, firstName , LastName from Contact where Id IN :ids];
       for(Contact current : allcontact)
             {
             system.debug(current);
                     if (current.Email!= null)
                     {
                     PageReference pdf =  Page.pdfTest;
                     pdf.getParameters().put('id',(String)current.id); 
                     system.debug('id is :'+current.id);
                               pdf.setRedirect(true);
                return pdf;
                     }
                     else //No email 
                     {
                     system.debug('in else current'+current );
               noEmail.add(current);
              // noEmail.add(current);
              system.debug('in else noemail'+noEmail );
                     }//e
             }        
    
     if(noEmail.size()>0 ) {
      PageReference pdf1 =  Page.NoEmailVF;
    
       pdf1.getParameters().put('Name', String.valueOf(noEmail)); 
               system.debug('pring noEmail' +noEmail); 
               pdf1.setRedirect(false);
                return pdf1;
    
       } 
             return null;
    
    
     }   
     }
    
  4. NoEmailVF 视觉力页面代码

     <apex:page controller="FirstController">
     <b> Emails are not sent to below contacts :  
    
     <table border="1">
     <tr>
     <th>Name</th>
      </tr>
    
     <apex:repeat var="cx" value="{!allcontact}" rendered="true">
     <tr>
             <td>{!cx.name}</td>
     </tr>   
     </apex:repeat> 
     </table> 
     <p> Please note that emails are not sent to selected Donors only when 
     they did not make any donation for that year or if they do not have email address listed. </p> 
     <p>If you still wish to retrieve donations made in this year, then you may use "Hard Copy" button listed on the Donor record to have the data printed. </p>
     </b>
     <apex:form >
     <apex:commandButton action="{!cancel}" value="Back" immediate="true"/>.        
     </apex:form>
     </apex:page>
    

【问题讨论】:

    标签: salesforce visualforce


    【解决方案1】:

    有点乱。我想我知道它为什么不起作用,但我也会给你一些如何清理它的提示。

    我认为您不需要 2 个单独的页面。您可以在 1 页上完成。我什至不确定你想要完成什么。用户是否应该被移动到上一页(我猜是某个列表视图按钮?无论history.back() 将它们带到哪里)。或Page.NoEmailVF。 (甚至还有Page.pdfTest 加入其中;))

    如果您确定需要多个页面 - 以下是跨页面传输控制器“状态”的方法。只要它们共享相同的控制器(或扩展),它就应该自动工作,无需通过 url 传递任何东西:https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_wizard.htm

    你的构造函数做的不多。他们保存对标准(设置)控制器的引用,但他们不运行任何查询。在send() 方法之前不会查询联系人。

    您希望将noEmail 参数与String.valueOf(List&lt;Contact&gt;) 一起传递。它......呃......它可能会做一些事情,但在重定向之后你什么都没有读到。 NoEmailVF 页面只有 &lt;apex:page controller=...(不是扩展名),因此调用了 FirstController()(没有任何参数的页面)。而且它的身体是空的!它完全忽略了通过 url 传递的内容。它可能可以读取您使用 ApexPages.currentPage().getParameters().get('Name') 传递的内容,但老实说,您不知道如何从此类字符串中创建真正的联系人列表。乱。你可能可以先做一些JSON.serialize 然后deserialize 但我不喜欢整个想法。

    最后但并非最不重要的一点 - 调用页面action 是邪恶的,对用户来说是出乎意料的,并且违反了 Salesforce 安全最佳实践。请检查我的旧答案https://salesforce.stackexchange.com/a/28853/799

    所以...

    一个随机的互联网陌生人认为你需要什么:

    1. 1 VF 页面。只有 1 个构造函数,即采用 StandardSetController 的构造函数。
    2. 在构造函数中检查ssc.getSelected() 并查询SELECT Id, Name FROM Contact WHERE Id IN :ssc.getSelected() AND Email = null。将查询结果保存到public contactsWithoutEmail {get; private set;}
    3. 除非您绝对需要自动化,否则不要使用action={!send}。它应该是有意识的用户决定点击一些最终的“做它!”按钮。
    4. send() 方法中仅查询这些SELECT Id, Name FROM Contact WHERE Id IN :ssc.getSelected() AND Email != null 并处理它们。
    5. 在 visualforce 中 - 使用 &lt;apex:pageBlockTable&gt;&lt;apex:dataTable&gt; 或类似的方式显示 contactsWithoutEmail。无需手工制作 html。
    6. 我建议将send 设为普通apex:commandButton,而不是action

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多