简体   繁体   English

Visualforce 页面未呈现来自 controller 的值

[英]Visualforce page not rendering values from controller

Need your inputs in a scenario I am currently stuck in. Here are the details.在我目前陷入的场景中需要您的输入。这是详细信息。 Appreciate your time and all your inputs.感谢您的时间和所有投入。

  • currently I am able to see the values retrieved in controller but they are not being displayed on visualforce page.目前我能够看到在 controller 中检索到的值,但它们没有显示在 visualforce 页面上。

Requirement: I need to email bulk of selected contacts.要求:我需要 email 批量选择的联系人。 When there is no email to selected contacts, we are required to populate the name of contacts on UI who do not have the email.当所选联系人没有 email 时,我们需要在 UI 上填充没有 email 的联系人姓名。 I am able to accomplish first part of requirement but stuck on displaying contact names on visual force page.我能够完成要求的第一部分,但坚持在视觉力页面上显示联系人姓名。

  1. List button: BulkEmailTest which calls firstVF visual force page.列表按钮:调用firstVF视觉力页面的BulkEmailTest。

  2. firstVF code:第一个VF代码:

     <apex:page standardController="Contact" extensions="FirstController" recordSetVar="listRecs" action="{.send}"> Emails are being sent. <script> window;history:back(); </script> </apex:page>
  3. FirstController code: for simplified code, I have edited snippet for contacts with email as our priority is only related to contacts with no email. FirstController 代码:对于简化代码,我编辑了带有 email 的联系人的片段,因为我们的优先级仅与没有 email 的联系人有关。

     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 visual force page code 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>

It's bit messy.有点乱I think I know why it doesn't work but I'm also going to give you few tips how to clean it up.我想我知道它为什么不起作用,但我也会给你一些如何清理它的提示。

I don't think you need 2 separate pages.我认为您不需要 2 个单独的页面。 You could do it on 1 page.您可以在 1 页上完成。 I'm not even sure what were you trying to accomplish.我什至不确定你想要完成什么。 Should the user be moved to previous page (some listview button I guess? Wherever history.back() takes them).用户是否应该移到上一页(我猜是某个列表视图按钮?无论history.back()带它们到哪里)。 Or to Page.NoEmailVF .Page.NoEmailVF (and there's even Page.pdfTest thrown into the mix;)) (甚至还有Page.pdfTest混入其中;))

If you're sure you need multiple pages - here's how you can transfer "state" of the controller across pages.如果您确定需要多个页面 - 以下是如何跨页面传输 controller 的“状态”。 It should work automatically as long as they share same controller (or extension), no need to pass anything via url: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_wizard.htm It should work automatically as long as they share same controller (or extension), no need to pass anything via url: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_wizard. htm

Your constructors don't do much.你的构造函数做的不多。 They save the references to standard(set)controller but they don't run any queries.他们保存对标准(设置)控制器的引用,但他们不运行任何查询。 Contacts aren't queried until the send() method.send()方法之前不会查询联系人。

You're hoping to pass the noEmail parameter with String.valueOf(List<Contact>) .您希望使用String.valueOf(List<Contact>)传递noEmail参数。 It's... uh.. it probably does something but after redirect you don't read anything.它......呃......它可能会做一些事情,但在重定向之后你什么都没有读到。 The NoEmailVF page has just <apex:page controller=... (not an extension) so the FirstController() (the one without any parameters) is called. NoEmailVF页面只有<apex:page controller=... (不是扩展名),因此调用了FirstController() (没有任何参数的那个)。 And it has empty body.它有空的身体。 It completely ignores what was passed via url.它完全忽略了通过 url 传递的内容。 It probably could read what you passed using ApexPages.currentPage().getParameters().get('Name') but then honestly no idea what you can do to create real list of contacts out of such string.它可能可以读取您使用ApexPages.currentPage().getParameters().get('Name')传递的内容,但老实说,您不知道如何从此类字符串中创建真正的联系人列表。 Messy.凌乱。 You probably could do some JSON.serialize and then deserialize but I don't like the whole idea.您可能可以做一些JSON.serialize然后deserialize化,但我不喜欢整个想法。

And last but not least - calling page action is evil, unexpected for the user and against salesforce security best practices.最后但并非最不重要的一点 - 调用页面action是邪恶的,对用户来说是出乎意料的,并且违反了 salesforce 安全最佳实践。 Pls check my old answer https://salesforce.stackexchange.com/a/28853/799请检查我的旧答案https://salesforce.stackexchange.com/a/28853/799

So...所以...

What a random internet stranger thinks you need:一个随机的互联网陌生人认为你需要什么:

  1. 1 VF page. 1 VF 页。 With only 1 constructor, the one that takes StandardSetController.只有 1 个构造函数,即采用 StandardSetController 的构造函数。
  2. In the constructor inspect the ssc.getSelected() and query SELECT Id, Name FROM Contact WHERE Id IN:ssc.getSelected() AND Email = null .在构造函数中检查ssc.getSelected()并查询SELECT Id, Name FROM Contact WHERE Id IN:ssc.getSelected() AND Email = null Save the results of the query into public contactsWithoutEmail {get; private set;}将查询结果保存到public contactsWithoutEmail {get; private set;} public contactsWithoutEmail {get; private set;}
  3. Do NOT have the action={!send} unless you absolutely need it automated.除非您绝对需要自动化,否则不要使用action={!send} it should be conscious user's decision to click some final "Do it."它应该是有意识的用户决定点击一些最终的“做它”。 button.按钮。
  4. In the send() method query only these SELECT Id, Name FROM Contact WHERE Id IN:ssc.getSelected() AND Email != null and process them.send()方法中仅查询这些SELECT Id, Name FROM Contact WHERE Id IN:ssc.getSelected() AND Email != null并处理它们。
  5. In visualforce - use <apex:pageBlockTable> , <apex:dataTable> or similar to display contactsWithoutEmail .在 visualforce 中 - 使用<apex:pageBlockTable><apex:dataTable>或类似内容来显示contactsWithoutEmail No need to hand-craft the html.无需手工制作 html。
  6. And I recommend making send a normal apex:commandButton , not action我建议send一个普通的apex:commandButton ,而不是action

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM