Salesforce 在VisualForce页面中将父id传递给子记录
我正在尝试创建我的第一个VF页面。这是一个行项目输入表单,允许用户在单击“保存”之前输入多个子记录(儿童记录)。现在,我正在从父级(Assure_c)上的自定义按钮打开VF页面。但是,当页面打开时,父项的查找字段未填充-因此用户必须单击查找以从Assure_u_;c中选择父项。是否有方法将上一页的父id传递给VF页上的新子记录Salesforce 在VisualForce页面中将父id传递给子记录,salesforce,apex-code,visualforce,force.com,Salesforce,Apex Code,Visualforce,Force.com,我正在尝试创建我的第一个VF页面。这是一个行项目输入表单,允许用户在单击“保存”之前输入多个子记录(儿童记录)。现在,我正在从父级(Assure_c)上的自定义按钮打开VF页面。但是,当页面打开时,父项的查找字段未填充-因此用户必须单击查找以从Assure_u_;c中选择父项。是否有方法将上一页的父id传递给VF页上的新子记录 //page <apex:page standardController="Enfants__c" extensions="insererEnfants"
//page
<apex:page standardController="Enfants__c" extensions="insererEnfants" standardStylesheets="true">
<apex:sectionHeader title="Ajouter un enfant"
subtitle="{!$User.FirstName}" help="/help/doc/user_ed.jsp?loc=help"></apex:sectionHeader>
<apex:form >
<apex:pageBlock title="Nouveau enfant" id="thePageBlock" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Enregistrer"></apex:commandButton>
<apex:commandButton action="{!cancel}" value=" Annuler "></apex:commandButton>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!accts}" var="a" id="table">
<apex:facet name="footer">
<apex:commandLink value="Ajouter" action="{!addRow}" rerender="table,error"/>
</apex:facet>
<apex:facet name="header">
<apex:commandLink value="Supprimer" action="{!deleteRow}" rerender="table,error"/>
</apex:facet>
<apex:column headerValue="Nom">
<apex:inputField/> value="{!a.Parent__c}"/>
</apex:column>
<apex:column headerValue="Nom">
<apex:inputField value="{!a.Name}"/>
</apex:column>
<apex:column headerValue="Prénom">
<apex:inputField value="{!a.Prenom__c}"/>
</apex:column>
<apex:column headerValue="Né le">
<apex:inputField value="{!a.Date_de_naissance__c}"/>
</apex:column>
<apex:column headerValue="Lieu de naissance">
<apex:inputField value="{!a.Lieu_de_naissance__c}"/>
</apex:column>
<apex:column headerValue="Situation">
<apex:inputField value="{!a.Situation__c }"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageblock>
</apex:form>
</apex:page>
//Controller
public class insererEnfants{
public List<Enfants__c> accts {get; set;}
public insererEnfants(ApexPages.StandardController controller){
accts = new List<Enfants__c>();
accts.add(new Enfants__c();
}
public void addrow(){
accts.add(new Enfants__c());
}
public PageReference deleteRow(){
if (accts.size()>1)
{
accts.remove(accts.size()-1);
}
return null;
}
public PageReference save()
{
insert accts;
Assure__c theParent = new Assure__c(id=accts[0].Parent__c);
PageReference acctPage = new ApexPages.StandardController(theParent).view();
acctPage.setRedirect(true);
return acctPage;
}
}
//第页
value=“{!a.Parent\uuu c}”/>
//控制器
公营班学生{
公共列表帐户{get;set;}
公共插入儿童(ApexPages.StandardController){
accts=新列表();
账户添加(新儿童);
}
public void addrow(){
账户添加(新儿童);
}
公共页面引用deleteRow(){
如果(帐户大小()>1)
{
账户移除(账户大小()-1);
}
返回null;
}
公共页面引用保存()
{
插入账户;
Assure\uuu c家长=新家长(id=accts[0]。家长\uuu c);
PageReference acctPage=新建ApexPages.StandardController(theParent.view();
acctPage.setRedirect(真);
返回账户;
}
}
首先,您没有对父对象的引用。可以用两种方法解决此问题:
解决方案1:
使用URL按钮传递父级的ID,URL如下所示:
/apex/EnfantsPage?assureid={!Assure_c.Id}
将“EnfantsPage”替换为VF页面的名称。然后,在控制器内:
String assureid = ApexPages.currentPage().getParameters().get('assureid');
现在您知道了父记录的ID。创建新的子记录时,请将父记录ID的值设置为已传递的ID
public insererEnfants(ApexPages.StandardController controller){
accts = new List<Enfants__c>();
if (assureid <> null) {
for (Assure__c assure: [ SELECT Id FROM Assure__c
WHERE Id = :assureid] ) {
accts.add(new Enfants__c( Parent__c = assure.Id ));
} else {
accts.add(new Enfants__c());
}
}
public void addrow(){
if (assure.Id <> null) {
accts.add(new Enfants__c( Parent__c = assure.Id ));
} else {
accts.add(new Enfants__c());
}
}
公共插件(ApexPages.StandardController){
accts=新列表();
if(assureid null){
for(Assure\uuu\c Assure:[从Assure\uu\c中选择Id
其中Id=:assureid]){
账户添加(新子女(父母=assure.Id));
}否则{
账户添加(新儿童);
}
}
public void addrow(){
if(assure.Id null){
账户添加(新子女(父母=assure.Id));
}否则{
账户添加(新儿童);
}
}
解决方案2:
扩展父控制器而不是扩展子控制器:
public Assure__c parent {get;}
public List<Enfants__c> accts {get; set;}
public insererEnfants(ApexPages.StandardController stdController) {
this.parent = (Assure__c)stdcontroller.getRecord();
accts.add(new Enfants__c( Parent__c = parent.id );
}
public void addrow(){
accts.add(new Enfants__c( Parent__c = parent.id );
}
public-Assure\uuu-c-parent{get;}
公共列表帐户{get;set;}
公共插件(ApexPages.StandardController stdController){
this.parent=(Assure__c)stdcontroller.getRecord();
accts.add(新子女(父母=Parent.id);
}
public void addrow(){
accts.add(新子女(父母=Parent.id);
}
谢谢您的帮助,但我的问题是在基中插入ID,我应该修改控制器以插入父记录的ID。问题是您在创建子记录时没有定义父记录,也没有在其他任何地方进行设置。请尝试以下操作:accts.add(new Enfants_uc(parent_uc=assureid));我得到了这个错误错误:编译错误:变量不存在:在第9行第44列的assureid
这就是我的问题,直到我发现了这个问题,我才找到一个嘴的解决方案,我不能对这个问题做同样的处理再次查看,我现在看到您没有检索标准控制器的记录。我将更改我的ans我现在明白了根本问题所在。