首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从visualforce页面上的父对象相关列表中保存子对象记录

如何从visualforce页面上的父对象相关列表中保存子对象记录
EN

Stack Overflow用户
提问于 2017-09-20 18:46:37
回答 1查看 2.7K关注 0票数 0

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

  • 我已经创建了一个VF页面,在其中我使用了页面块表。
  • 当我单击add行时,将在表上创建一个新行,但当我保存机会时,AccountName字段将在表(相关列表)上显示空白,而该机会记录不会在特定帐户中保存。

我没有得到实际的问题,因为当我进行系统调试时,Account和opplist没有在saveopp()上显示任何内容。我认为Accountid没有进入机会搜索领域。

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

代码语言:javascript
复制
**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;        

}}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-22 08:55:51

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

  1. 您的帐户的默认保存方法是重定向到标准页。如果需要,我们需要重定向到visualforce页面。
  2. 构造函数没有很好地定义,因为如果帐户Id不存在,它将查询所有的机会。
  3. 在addRow()中,首先将一个记录添加到机会列表中,然后初始化一个新的机会记录,这是不正确的。此外,我们不能在插入记录之前显示帐户名。理想的解决方案是不要在机会列表中再次显示帐户名称,因为我们在页面顶部有信息。我们可以在节省机会的时候把机会联系起来。
  4. 在saveopp()中,您必须插入帐户。
  5. 同时,关闭日期也是保存机会的强制字段。因此,我们需要从UI中获取信息。

我更新了你的密码。如果您有任何疑问,请仔细阅读并通知我。

VF页面:

代码语言:javascript
复制
    <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>

顶点延伸:

代码语言:javascript
复制
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;
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46329628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档