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
goutham.tatineni1.3893010493585044E12goutham.tatineni1.3893010493585044E12 

Issue with Apex Save method while creating parent and Child record at same time

HI ,

I have a Parent Object (Client Site) and Child Object (Client Supplier ). What i am trying to acheive is i am trying to create 1 parent and multiple child records at the same time .Everything is working perfectly except I have one issue while i cretae a parent 1 Child record is created by deafult even if i do not want to . 
public class AddClientSupplier {

    ApexPages.StandardController sc;      
    public Client_Site__c acct{get;set;}
    public Integer marker=2;
    public Integer selectedClientSupplier{get;set;}
    public List<WrapperClass> lClientSuppliers{get;set;}
    public AddClientSupplier(ApexPages.StandardController controller) {
        this.acct = (Client_Site__c)controller.getRecord();
        sc=controller;
        lClientSuppliers=new List<WrapperClass>();
        Client_Supplier__c c=new Client_Supplier__c();
        lClientSuppliers.add(new WrapperClass(c,1));
    }
    public PageReference deleteClientSupplier(){
        Integer x=-1;
        for(WrapperClass wc:lClientSuppliers){
            x++;
            if(wc.counter==selectedClientSupplier){
                System.debug('-->selected ClientSupplier:'+selectedClientSupplier+'  position:'+x);
                break;
            }
        }
       lClientSuppliers.remove(x);
        return null;
    }
    public PageReference saveClientSite(){
        Database.SaveResult sr = Database.insert(acct, false);
        Id idey=sr.getId();
        List<Client_Supplier__c> ClientSupplierList=new List<Client_Supplier__c>();

        for(WrapperClass wc:lClientSuppliers){

            Client_Supplier__c c=new Client_Supplier__c();
         c.Is_This_a_New_or_Incumbent_Supplier__c=wc.c.Is_This_a_New_or_Incumbe           nt_Supplier__c;
        c.Skill_Type__c=wc.c.Skill_Type__c;
        c.Supplier_Name__c=wc.c.Supplier_Name__c;
        c.Will_Manpower_Manage_This_Supplier__c=wc.c.Will_Manpower_Manage_Thi        s_Supplier__c;     
        c.Client_Site__c=idey;
        ClientSupplierList.add(c);
        }
        insert ClientSupplierList;
        
         return(new PageReference('/'+sr.id).setRedirect(True));       
 

    }
    public PageReference addAClientSupplier(){
        
        Client_Supplier__c c=new Client_Supplier__c();
        lClientSuppliers.add(new WrapperClass(c,marker));        
        marker=marker+1;
        return null;
    }
    public class WrapperClass{
        public Integer counter{get;set;}
        public Client_Supplier__c c{get;set;}
        public WrapperClass(Client_Supplier__c cntc,Integer i){
            this.c=cntc;
            this.counter=i;
        }
    }
}
 
<apex:page standardController="Client_Site__c" extensions="AddClientSupplier,ClientSiteExt" tabStyle="Client_Site__c">
<apex:form id="myForm" >

<apex:sectionHeader title="New Client Site" />
<apex:pageBlock title=" Client Site Edit" mode="edit">

<apex:pageBlockButtons location="top" >
                        <apex:commandButton value="Save" action="{!saveClientSite}" />
                        <apex:commandButton value="Cancel" action="{!Cancel}"/>
</apex:pageBlockButtons>

<apex:pageBlockSection title="Information" columns="2">
  
    <apex:inputField value="{!Client_Site__c.Client_Site_Name__c}" taborderhint="1"/>
    <apex:inputField value="{!Client_Site__c.Client_Discovery__c}" taborderhint="6"/>
    <apex:inputField value="{!Client_Site__c.City__c}" taborderhint="2"/>
    <apex:inputField value="{!Client_Site__c.Number_of_Shifts__c}" taborderhint="7"/>
    <apex:inputField value="{!Client_Site__c.State__c}" taborderhint="3"/>
    <apex:inputField value="{!Client_Site__c.Number_of_Team_Leads__c}" taborderhint="8"/>
    <apex:inputField value="{!Client_Site__c.Head_count__c}" taborderhint="4"/>
    <apex:inputField value="{!Client_Site__c.Number_of_On_Site_Managers__c}" taborderhint="9"/>
    <apex:inputField value="{!Client_Site__c.Job_Titles__c}" taborderhint="5"/>
    <apex:inputField value="{!Client_Site__c.Union_or_Non_Union__c}" taborderhint="10"/>
  
  </apex:pageBlockSection>
<apex:pageBlockSection title="Client Suppliers" columns="4">
</apex:pageBlockSection>
     <apex:repeat value="{!lClientSuppliers}" var="x">
     <apex:panelGrid columns="6">
     <apex:panelGrid >
     <apex:facet name="header">Client Supplier Name</apex:facet>
     <apex:inputField value="{!x.c.Supplier_Name__c}" style="width:200px" />
     </apex:panelGrid>
     <apex:panelGrid >
     <apex:facet name="header">Is This a New or Incumbent Supplier?y</apex:facet>
     <apex:inputField value="{!x.c.Is_This_a_New_or_Incumbent_Supplier__c}" style="width:200px" />  
     </apex:panelGrid>  
     <apex:panelGrid >
     <apex:facet name="header">Skill Type</apex:facet>
     <apex:inputField value="{!x.c.Skill_Type__c}" style="width:200px"/> 
     </apex:panelGrid>
     <apex:panelGrid >
     <apex:facet name="header"> Will Manpower manage this supplier?</apex:facet>
     <apex:inputField value="{!x.c.Will_Manpower_Manage_This_Supplier__c}" style="width:200px" />
     </apex:panelGrid>
     <apex:panelGrid >
     </apex:panelGrid>    
    <apex:commandButton action="{!deleteClientSupplier}" style="Button" value="Delete ClientSite" reRender="myForm" immediate="true">
    
<apex:param value="{!x.counter}" name="selected"
assignTo="{!selectedContact}"/>
</apex:commandButton>      
    </apex:panelGrid> 
    </apex:repeat>   
    
    <apex:pageBlockButtons location="bottom">
    
     <apex:panelGrid ></apex:panelGrid>
    <apex:commandButton value="Add Client Supplier" action="{!addAClientSupplier}" reRender="myForm" immediate="true" />    
    <apex:commandButton value="Save" action="{!saveClientSite}"  />
    <apex:commandButton value="Cancel" action="{!cancel}"/>
   </apex:pageBlockButtons>
  </apex:pageBlock>
  </apex:form>
</apex:page>

User-added image
Best Answer chosen by goutham.tatineni1.3893010493585044E12
Le NguyenLe Nguyen
Hi,

Because there is 1 item in the client Suppliers list which you initialied in contructor.

Client_Supplier__c c=new Client_Supplier__c();
ClientSuppliers.add(new WrapperClass(c,1));

Therefore,  It will also create an client supplier.  You should check the values of being added client supplier its values have been populated.
 
for(WrapperClass wc:lClientSuppliers){

// You can put a simple validation here

//EX:

if(!string.isblank(wc.c.Skill_Type__c) ){
            Client_Supplier__c c=new Client_Supplier__c();
         c.Is_This_a_New_or_Incumbent_Supplier__c=wc.c.Is_This_a_New_or_Incumbe           nt_Supplier__c;
        c.Skill_Type__c=wc.c.Skill_Type__c;
        c.Supplier_Name__c=wc.c.Supplier_Name__c;
        c.Will_Manpower_Manage_This_Supplier__c=wc.c.Will_Manpower_Manage_Thi        s_Supplier__c;     
        c.Client_Site__c=idey;
        ClientSupplierList.add(c);}
       }

Regards,
Le
 

All Answers

Le NguyenLe Nguyen
Hi,

Because there is 1 item in the client Suppliers list which you initialied in contructor.

Client_Supplier__c c=new Client_Supplier__c();
ClientSuppliers.add(new WrapperClass(c,1));

Therefore,  It will also create an client supplier.  You should check the values of being added client supplier its values have been populated.
 
for(WrapperClass wc:lClientSuppliers){

// You can put a simple validation here

//EX:

if(!string.isblank(wc.c.Skill_Type__c) ){
            Client_Supplier__c c=new Client_Supplier__c();
         c.Is_This_a_New_or_Incumbent_Supplier__c=wc.c.Is_This_a_New_or_Incumbe           nt_Supplier__c;
        c.Skill_Type__c=wc.c.Skill_Type__c;
        c.Supplier_Name__c=wc.c.Supplier_Name__c;
        c.Will_Manpower_Manage_This_Supplier__c=wc.c.Will_Manpower_Manage_Thi        s_Supplier__c;     
        c.Client_Site__c=idey;
        ClientSupplierList.add(c);}
       }

Regards,
Le
 
This was selected as the best answer
goutham.tatineni1.3893010493585044E12goutham.tatineni1.3893010493585044E12
Thanks Le Nguyen that worked.