代码之家  ›  专栏  ›  技术社区  ›  Lavi__c

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

  •  0
  • Lavi__c  · 技术社区  · 7 年前

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

    • 我创建了一个VF页面,在其中我使用了页面块表。

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

    如果你们能帮我解决这个问题,我将不胜感激 问题感谢您的善意帮助:)

    **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;        
    
    }}
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Pang Mohammad Imran    7 年前

    1. 构造函数没有很好地定义,因为如果帐户Id不存在,它将查询所有机会。

    2. 在saveopp()中,必须向上插入帐户。

    3. 此外,关闭日期是保存opportunity的必填字段。因此,我们需要从UI获取信息。

    我已经更新了你的代码。请过目一下,如果你有任何疑问,请告诉我。

        <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;
            }
        }
    }