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
anuj huriaanuj huria 

save multiple records at same time

i want to same multiple records at one click im have following problems

1 dublicate records are creating
2 all records is save with same value


apex code

<apex:page controller="sample1">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!task}" var="p">
                <apex:column headerValue="values">
                    <apex:outputText value="{!p}">
                    </apex:outputText>
                </apex:column>
                
                <apex:column headerValue="15%">
                    <apex:inputText value="{!a}" />
                </apex:column>
                
                 <apex:column headerValue="20%">
                     <apex:inputtext value="{!b}"/>
                </apex:column>
                
                 <apex:column headerValue="30%">
                     <apex:inputtext value="{!c}"/>
                </apex:column>
                
            </apex:pageBlockTable>
            
             <apex:pageBlockButtons >
                 <apex:commandButton value="save" action="{!save}"/>
             </apex:pageBlockButtons>
           
          
        </apex:pageBlock>
    </apex:form>
</apex:page>


controller


public class sample1 
{

   
    
    List<String> s=new List<String>();
    public String x{get;set;}
    public Integer a{get;set;}
    public Integer b{get;set;}
    public Integer c{get;set;}
    
        public List<String> getTask()
        {
            List<SelectOption> options = new List<SelectOption>();
            Schema.DescribeFieldResult fieldResult = Account.task__c.getDescribe();
            List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
            
            for( Schema.PicklistEntry f : ple)
               {
                  //options.add(new SelectOption(f.getLabel(), f.getValue()));
                  //System.debug(f.getLabel());
                  s.add(f.getValue());
               }
             
            return s;
        }
    
     public void save() 
     {
          for(String x:s)
              {
                  task__c t = new task__c(Name=x,X15__c=a,X20__c=b,X30__c=c);
                  
                try
                   {
                       insert t;
                   }
              
               catch(Exception e)
                  {
                      ApexPages.addMessages(e);
                  }
              }
          
                       
     }
    
    
 
}
Shaijan ThomasShaijan Thomas
s.add(f.getValue()); - this line is getting all the values same.
task__c t = new task__c(Name=x,X15__c=a,X20__c=b,X30__c=c); - a, b and c are not having any value
Thanks
 
Harpreet On CloudHarpreet On Cloud
As a Best Practice, never use any DML within a for loop. I see 
insert t;
within for loop
sandeep reddy 37sandeep reddy 37
you will not do like that kind of scnarios will  to delete ,update but insert is need to define every field value also do dml for every operation other wise 1)douplicate values u will get     other wise run as a dynamic dml staements
public class insertdynamic{
public integer i {set;get;}
public list<account>acc{set;get;}
public string  name{set;get;}
public string  phone {set;get;}
public integer reveune{set;get;}
public void dynamic(){
 acc=new list<account>();
for( i=1;i<5;i++){
account a=new account();
a.name=name+i:
a.phone=phone;
a.AnnualRevenue=reveune;
acc.add(a)
}
if(i==1){
insert acc;
}else{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.error, 'this is duplicate values ');
}
public pagereferance next(){
pagereferance p=new pagereferance ('apex/nextpage');
return p;
}
use this page for enter next person details  vf for main
<apex:page controller="insertdynamic">
<apex:form>
<apex:inputtext value="{!name}"/>
<apex:inputtext value="{!phone}"/>
<apex:inputtext value="{!reveune}"/>
<apex:commadbutton value="save" action="{!dynamic}"/>
<apex:commandbutton value="next" action="{!next}"/>
</apex:form>
</apex:page>
vf page  name=========nextpage 
<apex:inputtext value="{!name}"/>
<apex:inputtext value="{!phone}"/>
<apex:inputtext value="{!reveune}"/>
<apex:commadbutton value="save" action="{!dynamic}"/>
<apex:commandbutton value="next" action="{!next}"/>

i hope this is use full to u tell me