【问题标题】:how can I save child object record from related list of parent object on visualforce page如何从visualforce页面上的父对象相关列表中保存子对象记录
【发布时间】:2017-09-25 18:21:33
【问题描述】:

我想从 visualforce 页面上的 Account 对象的相关列表中动态保存机会记录。为此:

  • 我创建了一个使用页块表的 VF 页。
  • 当我点击添加行时,会在表格上创建一个新行,但是当我保存机会时,帐户名称字段在表格(相关列表)上显示为空白,并且该机会记录不会保存在特定帐户中。

我没有遇到实际问题,因为当我进行系统调试时,帐户和 opplist 没有在 saveopp() 上显示任何内容。我认为 Accountid 不会进入机会的查找字段。

如果你们能帮我解决这个问题,我将非常感激 问题。谢谢你的帮助:)

**VF Page:-**
<apex:page standardController="Account" extensions="taskDemo">
<apex:form >
<apex:pageBlock title="" id="pb1" >
<apex:pageBlockSection title="Assign" columns="2">
<apex:inputField value="{!Account.Name}"/>
<apex:inputField value="{!Account.AccountNumber}"/>
<apex:inputField value="{!Account.Phone}"/>
<apex:inputField value="{!Account.Website}"/>
<apex:commandButton value="updateRecord" action="{!save}"/>
</apex:pageBlockSection>
<apex:pageBlockTable value="{!oppList}" var="op">
        <apex:column headerValue="OpportunityName"> 
        <apex:inputField value="{!op.Name}">
        </apex:inputField>
        </apex:column>
        <apex:column headerValue="AccountName">
        <apex:inputField value="{!op.Account.Name}"></apex:inputField>
        </apex:column>
        <apex:column headerValue="Amount">
        <apex:inputField value="{!op.Amount}">
        </apex:inputField>
        </apex:column>
        <apex:column headerValue="StageName">
        <apex:inputField value="{!op.StageName}">
        </apex:inputField>
        </apex:column>
     </apex:pageBlockTable>
<apex:commandButton value="saveopp" action="{!saveopp}"/>
<apex:commandButton value="AddRow" action="{!addRow}" rerender="pb1"/>
</apex:pageBlock>
</apex:form>
</apex:page>

**Controller:-**
public class taskDemo {
 public ApexPages.StandardController controller;
 public List<Opportunity> oppList{get; set;}
 public Account a{get; set;}
 public String accId{get;set;}

public taskDemo(ApexPages.StandardController controller) {
    a = new Account();
    accId = controller.getId();
    System.debug('accid is::'+accId);
    oppList = [Select id,Name,Account.Name,Amount,StageName,CloseDate from 
                 Opportunity where AccountId =: accId];
}

public void addRow(){
    oppList.add(new Opportunity());
    Opportunity ts=new Opportunity();
    ts.AccountId = accId;
    System.debug('addrow::'+ ts.AccountId);
}
public PageReference saveopp(){
    if(a.Name != null){
        insert a;
        system.debug('a record is='+a);
        List<Opportunity> con = new List<Opportunity>();

        for(Opportunity os : oppList)
        {
            os.AccountId = accId;
            con.add(os);
            system.debug('os record is='+os);
        }
        if(con != null){
            upsert oppList;
            system.debug('opp record is='+oppList);
        }
    }
    return null;        

}}

【问题讨论】:

    标签: controller salesforce visualforce apex


    【解决方案1】:

    我想指出您的代码中需要的几个问题和更改:

    1. 您帐户的默认保存方法是重定向到标准页面。如果需要,我们需要再次重定向到 visualforce 页面。

    2. 构造函数定义不明确,因为如果帐户 ID 不存在,它会查询所有机会。

    3. 在 addRow() 中,您首先将记录添加到机会列表,然后初始化新的机会记录,这是不正确的。此外,我们不能在插入记录之前显示帐户名称。理想的解决方案是不再在机会列表中显示客户名称,因为我们在页面顶部有信息。我们可以在保存机会时将机会与帐户相关联。

    4. 在 saveopp() 中,您必须更新帐户。

    5. 关闭日期也是保存商机的必填字段。因此我们需要从 UI 中获取信息。

    我已更新您的代码。请仔细阅读,如果您有任何疑问,请告诉我。

    VF 页面:

        <apex:page standardController="Account" extensions="taskDemo">
            <apex:form >
            <apex:pageMessages />
            <apex:pageBlock title="" id="pb1" >
            <apex:pageBlockSection title="Assign" columns="2">
                <apex:inputField value="{!Account.Name}"/>
                <apex:inputField value="{!Account.AccountNumber}"/>
                <apex:inputField value="{!Account.Phone}"/>
                <apex:inputField value="{!Account.Website}"/>
                <apex:commandButton value="updateRecord" action="{!upsertAccount}"/>
            </apex:pageBlockSection>
            <apex:pageBlockTable value="{!oppList}" var="op">
                <apex:column headerValue="OpportunityName"> 
                <apex:inputField value="{!op.Name}">
                </apex:inputField>
                </apex:column>
                <!--<apex:column headerValue="AccountName">
                <apex:inputField value="{!op.Account.Name}"></apex:inputField>
                </apex:column>-->
                <apex:column headerValue="Close date">
                    <apex:inputField value="{!op.CloseDate}"/>
                </apex:column>
                <apex:column headerValue="Amount">
                <apex:inputField value="{!op.Amount}">
                </apex:inputField>
                </apex:column>
                <apex:column headerValue="StageName">
                <apex:inputField value="{!op.StageName}">
                </apex:inputField>
                </apex:column>
             </apex:pageBlockTable>
            <apex:commandButton value="saveopp" action="{!saveopp}"/>
            <apex:commandButton value="AddRow" action="{!addRow}" rerender="pb1"/>
            </apex:pageBlock>
            </apex:form>
        </apex:page>
    

    顶点扩展:

    public class taskDemo {
        public ApexPages.StandardController controller;
        public List<Opportunity> oppList{get; set;}
        public Account account{get; set;}
        public String accId{get;set;}
    
        public taskDemo(ApexPages.StandardController controller) {
            try{
                account = new Account();
                account = (Account)controller.getRecord();
                if(account.Id != null){
                    oppList = [Select id,Name,Account.Name,Amount,StageName,CloseDate from 
                             Opportunity where AccountId =: account.Id];
                    system.debug('Opportunities '+ oppList.size());    
                }
                else{
                    oppList = new List<Opportunity>();
                }    
            }
            catch(Exception e){
                ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,e.getMessage()));
            }
    
        }
    
        public void addRow(){
            oppList.add(new Opportunity());
        }
    
        public pageReference upsertAccount(){
            Pagereference pg = Page.taskDemoPage;//please update taskDemoPage with the name of your vf page.
            if(account.Name != null){
                upsert account;
                pg.getParameters().put('Id',account.Id);
                pg.setRedirect(true);
            }
            else{
                pg = null;
            }
            return pg;
        }
    
        public PageReference saveopp(){
            try{
                pageReference pg = Page.taskDemoPage;//please update taskDemoPage with the name of your vf page.
                if(account.Name != null){
                    upsert account;
                    pg.getParameters().put('Id',account.Id);
                    List<Opportunity> con = new List<Opportunity>();
    
                    for(Opportunity os : oppList)
                    {   
                        os.AccountId = account.Id;
                        con.add(os);
                        system.debug('os record is='+os);
                    }
                    if(con != null){
                        upsert oppList;
                        system.debug('opp record is='+oppList);
                    }
                }
                pg.setRedirect(true);
                return pg;            
            }
            catch(Exception e){
                ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,e.getMessage()));
                return null;
            }
        }
    }
    

    【讨论】:

    • 非常感谢,@Shyam Nair 现在代码运行良好..就像我真正想要的一样..但是你能解释一下你所做的改变吗..?
    • 谢谢@Pang,你编辑了一些东西……你能解释一下吗?
    • 嗨@Swarnika,我做了以下更改"
    • 嗨@Swarnika,如果你通过我的cmets回答,你应该能够关注它。在 VF 页面上进行了一些小的更改,例如添加关闭日期字段和从机会列表中删除帐户名称字段。我还编写了一个名为 upsertAccount() 的自定义方法来更新帐户记录并导航到同一页面。在控制器中,我修改了构造函数以避免任何异常。另外,我正在再次修改代码以处理异常并在 VF 页面上显示错误。请在 10 分钟后检查。如果有任何进一步的疑问,请告诉我。谢谢。
    • 非常感谢@Shyam Nair 现在我明白了.. :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    相关资源
    最近更新 更多