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
Ethan WestEthan West 

Mass Create Button; Possible?

Just posting a query; I'm looking to create a Custom Button / Link that relates to a VisualForce page for assignment records.

I already have pre-existing Mass-Edit buttons made via VisualForce which were relatively straight forward and was curious whether the functionality to create a "Mass Create" exists as I can't find much about it online.

Many thanks.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Ethan,

Greetings to you!

I haven't tried it but you can refer to the below link which might help you.

https://salesforce.stackexchange.com/questions/57297/what-is-the-best-approach-to-mass-create-records-from-a-button-click

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 the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ethan WestEthan West

Hi Khan, 

I actually think I may have found what I'm looking for a little after posting this on this site;

https://www.salesforcetutorial.com/visualforce-page-add-multiple-records/

I'll run between the two choices I have now and see which product resolves my query.

I'll make sure to come back and mark whichever option seems best for anyone else's later reference.
 

Many thanks,

Ethan

Khan AnasKhan Anas (Salesforce Developers) 
Ethan, please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page standardController="Contact" extensions="AddDeleteRowSaveS" sidebar="false">
    <apex:form >
        <apex:pageBlock title="Contacts" id="pb">
            <apex:pageMessages />
            <apex:variable var="rowNumber" value="{!0}"/>
            <apex:pageblockSection columns="1">
                <apex:pageBlockTable title="Contacts" var="cont" value="{!contactList}"> 
                    
                    <apex:column headerValue="No." style="width:20px; text-align:center;" headerClass="centertext">
                        <apex:outputText value="{0}" style="text-align:center;"> 
                            <apex:param value="{!rowNumber+1}" /> 
                        </apex:outputText>
                    </apex:column> 
                    <apex:column headerValue="First Name" >
                        <apex:inputField value="{!cont.FirstName}"/>
                    </apex:column> 
                    <apex:column headerValue="Last Name" >
                        <apex:inputField value="{!cont.LastName}"/>
                    </apex:column> 
                    <apex:column headerValue="Phone" >
                        <apex:inputField value="{!cont.Phone}"/>
                    </apex:column> 
                    <apex:column headerValue="Email" >
                        <apex:inputField value="{!cont.Email}"/>
                    </apex:column> 
                    <apex:column headerValue="Action" >
                        <apex:commandButton value="Delete Row" action="{!deleteRow}" reRender="pb">
                            <apex:param name="rowIndex" value="{!rowNumber}"/>
                        </apex:commandButton>
                        <apex:variable var="rowNumber" value="{!rowNumber+1}"/>
                    </apex:column> 
                </apex:pageBlockTable>
                <apex:commandButton action="{!addRow}" value="Add Contact" reRender="pb"/>
            </apex:pageblockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!ave}" />
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form> 
</apex:page>

Controller:
public class AddDeleteRowSaveS {
    
    public Contact del;
    public List<Contact> con {get; set;}
    public List<Contact> delConList {get;set;}
    public List<Contact> contactList {get;set;}
    public Integer rowIndex {get;set;}
    
    public AddDeleteRowSaveS(ApexPages.StandardController controller) {
        Contact c = new Contact();
        contactList = new List<Contact>();
        contactList.add(c);
        
        delConList = new List<Contact>();
        con = new List<Contact>();
    }
    
    public void addRow(){
        Contact c = new Contact();
        contactList.add(c);
    }
    
    public PageReference ave(){        
        for(Contact s2 : contactList){
            con.add(s2);
        }
        insert con;
        contactList.clear();
        addRow();
        return null;
    } 
    
    public void deleteRow(){        
        rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
        del = contactList.remove(rowIndex);
        delConList.add(del);        
    }
}

Regards,
Khan Anas
Ethan WestEthan West

Hi Khan,
 

I tried quickly making my own before I try and get deeper with yours just so I can get a better base understanding - this is also made a tad difficult by the fact I'm using a Custom Object. 

I'm having an issue as I work through the errors on my Apex Class with Line 5 Column 12 which is:

public AddmultipleAssignmentController(ApexPages.StandardController controller) {
this error was also occurring when I kept it as:
public AddmultipleAssignmentController() {


You wouldn't happen to know / see the issue here would you?

[Please see below for full code I'm working my way through]
 

public class AddmultipleAssigmentController {
    pse__Assignment__c assignment = new pse__Assignment__c();
    public list<pse__Assignment__c> listAssignment{ get; set; }
    
    public AddmultipleAssignmentController(ApexPages.StandardController controller) {
        listAssignment=new list<pse__Assignment__c>();
        listAssignment.add(assignment);
    }
    
    Public void addAssignment() {
        Assignment TC = new Assignment();
        listAssignment.add(TC);
    }
    public PageReference saveAssignment() {
        for(Integer i=0; i<listAssignment.size(); i++) {
            insert listAssignment;
        }
        return Page.Allassignmentsaved;
    }
}
 

Many thanks,
Ethan

Khan AnasKhan Anas (Salesforce Developers) 
Hi Ethan, there is a typo in the constructor name. You need to use AddmultipleAssigmentController instead of AddmultipleAssignmentController (check spelling).

Try below code:
public class AddmultipleAssigmentController {
    pse__Assignment__c assignment = new pse__Assignment__c();
    public list<pse__Assignment__c> listAssignment{ get; set; }
    
    public AddmultipleAssigmentController(ApexPages.StandardController controller) {
        listAssignment=new list<pse__Assignment__c>();
        listAssignment.add(assignment);
    }
    
    public void addAssignment() {
        pse__Assignment__c TC = new pse__Assignment__c();
        listAssignment.add(TC);
    }

    public PageReference saveAssignment() {
        for(Integer i=0; i<listAssignment.size(); i++) {
            insert listAssignment;
        }
        return Page.Allassignmentsaved;
    }
}

 
Ethan WestEthan West

Hi Khan,

Absolutely silly mistake, saw it just as you were commenting!

My only query now is adding list view potential to the VisualForce code for the custom button/link - I assumed it would be to change my standardController to "pse__Assignment__c" and add extension="AddmultipleAssignmentController", but this didn't seem to work. 

As requested please find below the VisualForce Page and the Apex Class;

VF:

<apex:page Controller="AddmultipleAssignmentController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!listAssignment}" var="TC">
                <apex:column headerValue="Assignment Name">
                    <apex:inputField value="{!TC.Name}"/>
                </apex:column>
                <apex:column headerValue="Resource">
                    <apex:inputField value="{!TC.pse__Resource__c}"/>
                </apex:column>
                <apex:column headerValue="Project">
                    <apex:inputField value="{!TC.pse__Project__c}"/>
                </apex:column>
                <apex:column headerValue="Candidate">
                    <apex:inputField value="{!TC.Candidate__c}"/>
                </apex:column>
                <apex:column headerValue="Start Date">
                    <apex:inputField value="{!TC.pse__Start_Date__c}"/>
                </apex:column>
                <apex:column headerValue="End Date">
                    <apex:inputField value="{!TC.pse__End_Date__c}"/>
                </apex:column>
                <apex:column headerValue="Monday Hours">
                    <apex:inputField value="{!TC.Monday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Tuesday Hours">
                    <apex:inputField value="{!TC.Tuesday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Wednesday Hours">
                    <apex:inputField value="{!TC.Wednesday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Thursday Hours">
                    <apex:inputField value="{!TC.Thursday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Friday Hours">
                    <apex:inputField value="{!TC.Friday_Hours__c}"/>
                </apex:column>                       
                <apex:column headerValue="Saturday Hours">
                    <apex:inputField value="{!TC.Saturday_Hours__c}"/>
                </apex:column>                             
                <apex:column headerValue="Sunday Hours">
                    <apex:inputField value="{!TC.Sunday_Hours__c}"/>
                </apex:column>
            </apex:pageBlockTable>
                <apex:pageBlockButtons >
                    <apex:commandButton value="Add Assignment Row" action="{!addAssignment}"/>
                    <apex:commandButton value="Save Assignments" action="{!saveAssignment}"/>
                </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Apex Class:
public class AddmultipleAssignmentController {
    pse__Assignment__c assignment = new pse__Assignment__c();
    public list<pse__Assignment__c> listAssignment{ get; set; }
    
    public AddmultipleAssignmentController() {
        listAssignment=new list<pse__Assignment__c>();
        listAssignment.add(assignment);
    }
    
    Public void addAssignment() {
        pse__Assignment__c TC = new pse__Assignment__c();
        listAssignment.add(TC);
    }
    public PageReference saveAssignment() {
        for(Integer i=0; i<listAssignment.size(); i++) {
            insert listAssignment;
        }
        return Page.Allassignmentsaved;
    }
}
Returning VF-Resolution Page:
<apex:page sidebar="false" showHeader="true">
<center><h3>Your assignments have been successfully saved.
</h3></center>
</apex:page>
Happen to have any tips at all?

Thank you very much,

Ethan!

Ethan WestEthan West

Hi Khan,
 

Just coming back to this; not sure if you can help.
 

I was trying your version just to see whether it operated the same way as my other version.

I've got the Apex Class set out as such:

public class AddmultipleAssignmentController {

    public pse__Assignment__c del;
    public List<pse__Assignment__c> TC {get; set;}
    public List<pse__Assignment__c> delTCList {get; set;}
    public List<pse__Assignment__c> AssignmentList {get; set;}
    public Integer rowIndex {get; set;}

    public AddmultipleAssignmentController(ApexPages.StandardSetController controller) {
        pse__Assignment__c a = new pse__Assignment__c();
        AssignmentList = new List<pse__Assignment__c>();
        AssignmentList.add(a);
        
        delTCList = new List<pse__Assignment__c>();
        TC = new List<pse__Assignment__c>();
        
    }
    
    public void addRow(){
        pse__Assignment__c a = new pse__Assignment__c();
        AssignmentList.add(a);
    }
    
    public PageReference save(){
        for(pse__Assignment__c s2 : AssignmentList){
            TC.add(s2);
            
             return Page.Allassignmentsaved;
            
        }
        insert TC;
        AssignmentList.clear();
        addRow();
        return null;
    }
    
    public void deleteRow(){
        rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
        del = AssignmentList.remove(rowIndex);
        delTCList.add(del);
        

    }
}
Following your protocol I attempted to recreate the same in my VF page, see below;
<apex:page standardController="pse__Assignment__c" recordSetVar="unused" extensions="AddmultipleAssignmentController" >
<apex:enhancedList type="pse__Assignment__c" height="300" rowsPerPage="10" id="ListViewID"/>
    <apex:form>
    
        <apex:pageBlock title="Assignments" id="pb">
            <apex:variable var="rowNumber" value="{!0}"/>
            <apex:pageblockSection columns="1">
            <apex:pageBlockTable title="Assignments" value="{!listAssignment}" var="TC">
            
                <apex:column headerValue="No." style="width:20px; text-align:center;" headerClass="centertext">
                        <apex:outputText value="{0}" style="text-align:center;">
                            <apex:param value="{!rowNumber+1}"/>
                        </apex:outputText>
                </apex:column>
                        
                <apex:column headerValue="Assignment Name">
                    <apex:inputField value="{!TC.Name}"/>
                </apex:column>
                
                <apex:column headerValue="Resource">
                    <apex:inputField value="{!TC.pse__Resource__c}"/>
                </apex:column>
                
                <apex:column headerValue="Project">
                    <apex:inputField value="{!TC.pse__Project__c}"/>
                </apex:column>
                
                <apex:column headerValue="Candidate">
                    <apex:inputField value="{!TC.Candidate__c}"/>
                </apex:column>
                
                <apex:column headerValue="Bill Rate">
                    <apex:inputField value="{!TC.pse__Bill_Rate__c}"/>
                </apex:column>    
                
                <apex:column headerValue="Start Date">
                    <apex:inputField value="{!TC.Start_Date__c}"/>
                </apex:column>
                
                <apex:column headerValue="End Date">
                    <apex:inputField value="{!TC.End_Date__c}"/>
                </apex:column>
                
                <apex:column headerValue="Monday Hours">
                    <apex:inputField value="{!TC.Monday_Hours__c}"/>
                </apex:column>
                
                <apex:column headerValue="Tuesday Hours">
                    <apex:inputField value="{!TC.Tuesday_Hours__c}"/>
                </apex:column>
                
                <apex:column headerValue="Wednesday Hours">
                    <apex:inputField value="{!TC.Wednesday_Hours__c}"/>
                </apex:column>
                
                <apex:column headerValue="Thursday Hours">
                    <apex:inputField value="{!TC.Thursday_Hours__c}"/>
                </apex:column>
                
                <apex:column headerValue="Friday Hours">
                    <apex:inputField value="{!TC.Friday_Hours__c}"/>
                </apex:column>                       
                
                <apex:column headerValue="Saturday Hours">
                    <apex:inputField value="{!TC.Saturday_Hours__c}"/>
                </apex:column>                             
                
                <apex:column headerValue="Sunday Hours">
                    <apex:inputField value="{!TC.Sunday_Hours__c}"/>
                </apex:column>
                
                <apex:column headerValue="Action">
                    <apex:commandButton value="Delete Row" action="{!deleteRow}" reRender="pb"/>
                        <apex:param name="rowIndex" value="{!rowNumber+1}"/>
                </apex:column>
                
            </apex:pageBlockTable>
            
                    <apex:commandButton action="{!addRow}" value="Add Assignment" rerender="pb"/>
            </apex:pageblockSection>
                <apex:pageBlockButtons>
                    <apex:commandButton value="Save" action="{!save}"/>
                    <apex:commandButton value="Cancel" action="{!cancel}"/>
                </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

However, now I'm met with the error:

Error: Unknown property 'pse__Assignment__cStandardController.listAssignment'

Any chance you could see where this error is coming from at all?

Kind regards,

Ethan