function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Akash Choudhary 17Akash Choudhary 17 

Creating records in child object if a particular Parent record is selected

Hi All,

I have a scenario where I need to create a VF page with these details:
whenever a parent record from (Auctions__c) object is selected, automaically option for creating a child record in 
(SellerAuctionItem__c) object opens up in which Auction field(read only) (master detial relationship) is autopopulated with the parent record selected. And rest can be filled and saved
GauravGargGauravGarg
Hi Akash,

Please follow these:
  • On Selecting Parent object, call vf page method createRelatedChildRecords(). 
  • In createRelatedChildRecords(), create a new record SellerAuctionItem__c sai = new SellerAuctionItem__c(parentId = Auctions__c.Id)
  • Put this into a list, if need to create more child record or make SellerAuctionItem__c sai{get;set} at the top of page. 
  • Refresh the defined section of the page, or initiate the popup. As per your requirement. 
The remaining fields can be filled by Customer. 

Hope this will solve your problem. Do let me know if need more details on it. 

Thanks,
Gaurav
Skype: gaurav62990
Email: gauravgarg.nmims@gmail.com
Akash Choudhary 17Akash Choudhary 17
Hi Gaurav,

The thing is cutomer have to select the parent object record from the vf page, only then they can fill in the details with auction field auto populated with the parent record here is what I have tried but it is not working pls help:

vf page- 
<apex:page controller="RegisterMultipleAuction" showHeader="false" sidebar="false" standardStylesheets="true">
    <apex:form >
        <apex:pageMessages />
        <apex:pageblock title="Available auction">
            <apex:pageblocktable value="{!Allauction}" var="a"> 
                <apex:column headervalue="Select one">                    
                    <apex:actionsupport action="{!Allauction}" event="onclick" >  
                        <input type="radio" />                    
                        <apex:param name="auctionid" value="{!a.Id}">
                        </apex:param></apex:actionsupport>                            
                </apex:column>    
                <apex:column headervalue="Name">
                    <apex:outputfield value="{!a.Name}">
                    </apex:outputfield></apex:column> 
                
                <apex:column headervalue="Auction Code">
                    <apex:outputfield value="{!a.Auction_Code__c}">
                    </apex:outputfield></apex:column>  
                <apex:column headervalue="Auction Start Date">
                    <apex:outputfield value="{!a.Auction_Start_Date__c}">
                    </apex:outputfield></apex:column>  
                <apex:column headervalue="Auction End Date">
                    <apex:outputField value="{!a.Auction_End_Date__c}">
                    </apex:outputField>
                </apex:column> 
                <apex:column headervalue="Auction Status">
                    <apex:outputField value="{!a.Auction_Status__c}">
                    </apex:outputField>
                </apex:column> 
            </apex:pageblocktable>
            
        </apex:pageblock>
        <apex:pageblock id="registerauction" title="Register for this auction">
            <apex:pageblocktable id="registerauction" value="{!registerauction}" var="b">                       
                <apex:column headervalue="Name">
                    <apex:inputField value="{!b.Name}">
                    </apex:inputField>
                </apex:column>  
                
                
                <apex:column headervalue="Category">
                    <apex:inputField value="{!b.Category__c}">
                    </apex:inputField>
                </apex:column> 
                <apex:column headervalue="Minimum Price">
                    <apex:inputField value="{!b.Minimum_Price__c}">
                    </apex:inputField>
                </apex:column> 
                <apex:column headervalue=" Auction">
                    <apex:outputField value="{!b.Auction__c}">
                    </apex:outputField>
                </apex:column> 
                <apex:column headervalue="Description">
                    <apex:inputField value="{!b.Description__c}">
                    </apex:inputField>
                </apex:column>  
            
                <apex:column headervalue="Item Code">
                    <apex:inputField value="{!b.Item_Code__c}">
                    </apex:inputField>
                    </apex:column>
                 <apex:column headervalue="Status">
                    <apex:inputField value="{!b.Status__c}">
                    </apex:inputField>
                </apex:column> 
 

 
            </apex:pageblocktable>
            
          <apex:commandButton value="Save" action="{!saveauction}"/> 
        </apex:pageblock>               
    </apex:form>
</apex:page>


Controller- 
public class RegisterMultipleAuction {
 list<Auctions__c>   a = new list<Auctions__c> ();
Public List<Auctions__c> getAllauction(){
List <Auctions__c> allauctions = [Select Id,Name,Auction_Code__c,Auction_End_Date__c, Auction_Start_Date__c, Auction_Status__c From Auctions__c where Auction_Status__c ='new'];
 
    a.addAll(allauctions);
    return a;
}
    
        public void registerauction(){
            if(!a.isEmpty()){  
            List<SellerAuctionItem__c> submitItem = [SELECT Id, Name, Category__c,Minimum_Price__c, Auction__c, Description__c,Item_Code__c,Status__c FROM SellerAuctionItem__c ];
            for(SellerAuctionItem__c item :submitItem ){
                item.Auction__c = a.get(0).Name;
            }}
        }
    }

this is not working. PLEASE HELP!
Thanks
 
GauravGargGauravGarg
Are you able to get Parameter value in Apex class?
  • Call RegisterAuction event from action:support or action:function. Use "saiList" as a looping variable. Below extension class is updated to accommodate the new values. 
 
Controller- 
public class RegisterMultipleAuction {
	list<Auctions__c>   a = new list<Auctions__c> ();
	public list <SellerAuctionItem__c> saiList {get;set;}

	Public List<Auctions__c> getAllauction(){
		List <Auctions__c> allauctions = [Select Id,Name,Auction_Code__c,Auction_End_Date__c, Auction_Start_Date__c, Auction_Status__c From Auctions__c where Auction_Status__c ='new'];

		a.addAll(allauctions);
		return a;
	}

	public void registerauction(){
		if(!a.isEmpty()){  
			saiList  = new List<SellerAuctionItem__c >();
			SellerAuctionItem__c submitItem = new SellerAuctionItem__c(Auction__c = a.get(0).Name);
			saiList  add(submitItem );
		}}
	}
}


Thanks,

Gaurav

Akash Choudhary 17Akash Choudhary 17
It is throwing an error - Unknown property 'RegisterMultipleAuction.registerauction'
GauravGargGauravGarg
can you please show the VF page code
Akash Choudhary 17Akash Choudhary 17
I have shown it above it has both vf and controller class but here is VF again

vf page- 
<apex:page controller="RegisterMultipleAuction" showHeader="false" sidebar="false" standardStylesheets="true">
    <apex:form >
        <apex:pageMessages />
        <apex:pageblock title="Available auction">
            <apex:pageblocktable value="{!Allauction}" var="a"> 
                <apex:column headervalue="Select one">                    
                    <apex:actionsupport action="{!Allauction}" event="onclick" >  
                        <input type="radio" />                    
                        <apex:param name="auctionid" value="{!a.Id}">
                        </apex:param></apex:actionsupport>                            
                </apex:column>    
                <apex:column headervalue="Name">
                    <apex:outputfield value="{!a.Name}">
                    </apex:outputfield></apex:column> 
                
                <apex:column headervalue="Auction Code">
                    <apex:outputfield value="{!a.Auction_Code__c}">
                    </apex:outputfield></apex:column>  
                <apex:column headervalue="Auction Start Date">
                    <apex:outputfield value="{!a.Auction_Start_Date__c}">
                    </apex:outputfield></apex:column>  
                <apex:column headervalue="Auction End Date">
                    <apex:outputField value="{!a.Auction_End_Date__c}">
                    </apex:outputField>
                </apex:column> 
                <apex:column headervalue="Auction Status">
                    <apex:outputField value="{!a.Auction_Status__c}">
                    </apex:outputField>
                </apex:column> 
            </apex:pageblocktable>
            
        </apex:pageblock>
        <apex:pageblock id="registerauction" title="Register for this auction">
            <apex:pageblocktable id="registerauction" value="{!registerauction}" var="b">                       
                <apex:column headervalue="Name">
                    <apex:inputField value="{!b.Name}">
                    </apex:inputField>
                </apex:column>  
                
                
                <apex:column headervalue="Category">
                    <apex:inputField value="{!b.Category__c}">
                    </apex:inputField>
                </apex:column> 
                <apex:column headervalue="Minimum Price">
                    <apex:inputField value="{!b.Minimum_Price__c}">
                    </apex:inputField>
                </apex:column> 
                <apex:column headervalue=" Auction">
                    <apex:outputField value="{!b.Auction__c}">
                    </apex:outputField>
                </apex:column> 
                <apex:column headervalue="Description">
                    <apex:inputField value="{!b.Description__c}">
                    </apex:inputField>
                </apex:column>  
            
                <apex:column headervalue="Item Code">
                    <apex:inputField value="{!b.Item_Code__c}">
                    </apex:inputField>
                    </apex:column>
                 <apex:column headervalue="Status">
                    <apex:inputField value="{!b.Status__c}">
                    </apex:inputField>
                </apex:column> 
 

 
            </apex:pageblocktable>
            
          <apex:commandButton value="Save" action="{!saveauction}"/> 
        </apex:pageblock>               
    </apex:form>
</apex:page>

Thanks.
 
GauravGargGauravGarg
instead of "registerauction" you have to use List "saiList "


<apex:pageblocktable id="registerauction" value="{!saiList }" var="b">  
Akash Choudhary 17Akash Choudhary 17
It is throwing an unknown method error - Unknown method 'RegisterMultipleAuction.saiList()'
Khan AnasKhan Anas (Salesforce Developers) 
Hi Akash,

I trust you are doing very well.

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Parent Object : Account
Child
Object : Contact


Visualforce:
<apex:page controller="AutoParentFieldPopoupC">
    <apex:includescript value="//code.jquery.com/jquery-1.11.1.min.js" />
    <apex:includescript value="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js" />
    <apex:stylesheet value="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css" /> 
    
    <script>
    <!--j$ = jQuery.noConflict();-->
        $(document).ready( function () {
        var accounttable = $('[id$="accounttable"]').DataTable({
            
        });
        
        $("#formid").hide();
        $('#a1').click(function(){
            $("#formid").show();
            
        });
        
        $('#closeButton').click(function(){
            $('#formid').hide();
        });
        
    });
    function myFunc(id){
        console.log(id);
        hold(id);
        $("#formid").show();
    }
    
    </script> 
    
    
    
    <style type="text/css">
        .custPopup{
        background-color: white;
        border-width: 2px;
        border-style: solid;
        z-index: 9999;
        left: 50%;
        padding:10px;
        position: absolute;            
        width: 600px;
        margin-left: -350px;
        top:120px;
        }
        .popupBackground{
        background-color:black;
        opacity: 0.20;
        filter: alpha(opacity = 20);
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        z-index: 9998;
        }
    </style>
    <apex:form id="fd">
        <apex:actionFunction action="{!call}" name="hold"  rerender="dd">
            <apex:param id="aname" name="Idr" value="" />
        </apex:actionFunction>
        
    </apex:form>
    <body>
        <apex:form id="aa">
            <table id="accounttable" class="display">
                <thead>
                    <tr>
                        <th>Account</th>
                        <th>Phone</th>
                    </tr>
                </thead>
                <tbody>
                    <apex:repeat value="{!accountList}" var="acc">
                        <tr>
                            <td onclick="myFunc('{!acc.Id}'); return false;">{!acc.Name}</td>
                            <td>{!acc.Phone}</td>
                        </tr>                                  
                    </apex:repeat>
                </tbody>          
            </table> 
        </apex:form>
        <div id="formid">
            <apex:outputPanel id="tstpopup"  >
                <apex:outputPanel styleClass="popupBackground" layout="block" />
                <apex:outputPanel styleClass="custPopup" layout="block" >
                    <apex:form id="dd">
                        <apex:pageBlock title="My Content" mode="edit">
                            
                            <apex:pageBlockSection title="My Content Section" columns="2">
                                <apex:outputField value="{!con.Name}"/>
                                <apex:inputField value="{!contact.firstName}"/>
                                <apex:inputField value="{!contact.lastName}"/>                             
                            </apex:pageBlockSection>
                            <apex:pageBlockButtons >
                                <apex:commandButton value="Save" action="{!updateContact}" >
                                    <apex:param name="con1" assignTo="{!con.Id}" value="con1" />
                                </apex:commandButton>
                            </apex:pageBlockButtons>
                        </apex:pageBlock>
                    </apex:form>
                    
                    <button type="button" id='closeButton'>Close</button>
                </apex:outputPanel>
            </apex:outputPanel>
        </div>                      
    </body>
</apex:page>

Controller:
public class AutoParentFieldPopoupC {
    
    public boolean displayPopup {get; set;}
    public Account con {get;set;} 
    public String newcon{get;set;}
    public String con1{get;set;}
    public Id recId {get; set;}
    public Contact contact {get;set;}
    public String idr {get;set;}
    public List<Contact> acnt;
    
    public AutoParentFieldPopoupC(){
        contact = new Contact();
        con = new Account();
        acnt = new List<Contact>();
    }
    
    public List<Account> accountList {
        get {
            if (accountList == null) {
                accountList = [SELECT Name, Phone FROM Account LIMIT 1000];
            }
            return accountList;
        }
        set;
    }
    
    
    public void call(){
        string passedParam1 = Apexpages.currentPage().getParameters().get('Idr');
        con = [SELECT Id, Name, (SELECT firstName, lastName FROM Contacts) FROM Account WHERE Id =: passedParam1];       
    }
    
    public PageReference updateContact(){
        contact.AccountId = con.Id;
        acnt.add(contact);
        System.debug('acnt-> ' + acnt);
        upsert acnt;  
        return null;
    }
}

Screenshot:
User-added image

User-added image




I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
GauravGargGauravGarg
please debug in debug log, where it is failing. Also, please switch on "View state" checkbox on user record to show line number.