0

I am pretty new to Apex and VF (started about 10 hours ago), have close to never coded before, and I have this problem I need to solve: The ultimate goal is to be able to select multiple Account records and then bulk create records in a custom object from the Account list view that would be associated with the selected Accounts selected. For now, I felt like the best way to go was to have 2 VF pages. The first would create the custom object records based on some predefined values and then call the second page. The second page should then update the fields of the initially created object using values that would be selected by the user. Here's my code:

Page 1:

<apex:page standardController="Account"
       lightningStyleSheets="true"
recordSetVar="Account"
extensions="CreatePipeline"
showHeader="false"
id="mcp">
<script>

</script>

<apex:form id="mcpform">
    <apex:pageMessage summary="Selected Collection Size: {!mySelectedSize}" 
        severity="info"
        id="mcppm1"
    />
    <apex:pageMessage summary="Record Set Size: {!myRecordsSize}"
        severity="info"
        id="mcppm2"
    />
    
    
    <apex:pageBlock title="Mass create Pipeline" mode="edit" id="mc1">
        <apex:pageBlockButtons location="bottom" id="mubut">
            <apex:commandButton value="update" action="{!MassCreatePipelines}" id="butupd"/>
            <apex:commandButton value="Leave" action="{!save}" id="butlea"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>
</apex:page>

Page 2

<apex:page 
    standardController="Pipeline__c"
    recordSetVar="pipeline"
    lightningStyleSheets="true"
    extensions="CreatePipeline"
    showHeader="false"
    id="muopp"
           >
     <apex:form id="muform">
        <apex:pageMessage summary="Selected Collection Size: {!mySelectedSize}" 
            severity="info"
            id="mupms"
        />
        <apex:pageMessage summary="Record Set Size: {!myRecordsSize}"
            severity="info"
            id="mupmr"
        />
        <apex:pageBlock title="Pipeline Mass-Update" mode="edit" id="mub1">
            <apex:pageMessages />
            <apex:pageBlockSection id="mus1">
                <apex:inputField value="{!Pipeline__c.Stage__c}" id="stagename">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputField>
            </apex:pageBlockSection>
            <apex:pageBlockSection id="mus2">
                <apex:inputField value="{!Pipeline__c.Deal_Status__c}" id="statusname">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputField>
            </apex:pageBlockSection>
            <apex:pageBlockSection id="mus3">
                <apex:inputField value="{!Pipeline__c.Cohort_Name__c}" id="cohortname">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputField>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom" id="mubut">
                <apex:commandButton value="Save" action="{!save}" id="butsav"/>
                <apex:commandButton value="Cancel" action="{!cancel}" id="butcan"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:pageBlock title="Selected Pipelines" id="muselectedlist">
            <apex:pageBlockTable value="{!selected}" var="pipl" id="mutab">
                <apex:column value="{!pipl.name}" id="piplname"/>
                <apex:column value="{!pipl.Stage__c}" id="piplstage"/>
                <apex:column value="{!pipl.Deal_Status__c}" id="piplstatus"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
           
    
</apex:page>

APEX

    public class CreatePipeline {
ApexPages.StandardSetController setCon;
    
    Public List<Pipeline__c> myList = new List<Pipeline__c>();
    
    public CreatePipeline(ApexPages.StandardSetController controller){
        setCon = controller;
    }
    
    
    Public PageReference MassCreatePipelines(){
        List<Pipeline__c> myList = new List<Pipeline__c>();
        PageReference pageRef = ApexPages.currentPage();
        for(Integer i=0;i<setCon.getSelected().size();i++) {

            Pipeline__c a = new Pipeline__c(Name='testing button' + i,
                                                            Company_Name__c=setCon.getSelected().get(i).id,
                                                            Deal_Leaders__c='Joe Bob',
                                                            Fund__c='Moneyman',
                                                            Log_In_Date__c=Date.today(),
                                                            Momentum_Stage__c='Potential',
                                                            Momentum_Deal_Status__c='Under Review',
                                                             Cohort_Name__c='aaaaaaa00000' 
                                                            );
            myList.add(a);
            insert a;

        }
        PageReference newocp = new PageReference('/apex/MassCreatePipeline2');
        return newocp;
        }
    Public PageReference MassUpdatePipeline(){
        for(Integer i=0;i<setCon.getSelected().size();i++) {
            myList.get(i).Cohort_Name__c = Pipeline__c.Cohort_Name__c; /*this is where shit fuck**************************************************************************/

            }
        Return Null;
    }
    public integer getMySelectedSize() {
        return setCon.getSelected().size();
    }
    public integer getMyRecordsSize() {
 

           return setCon.getRecords().size();
        }
        
    }

Basically my problem now is that I have no clue how to send the value that were selected on page 2 to the MassUpdatePipeline() class. Me playing with 2 different objects and predefined picklists only makes it harder for me to figure it out.

Would really apreciate any help

1
  • 4
    If you are learning Salesforce, selecting Visualforce as your UI technology of choice is a huge mistake. Learn lwc instead, it is significantly more valuable.
    – Adrian Larson
    Mar 26 at 4:25

1 Answer 1

0

Communicating between controller objects can be done using several techniques

Option 1 Use a single controller aka "wizard"

That is, don't use two controllers. This is described in the VF documentation Creating a Wizard

Option 2 Pass the ids of the selected recs in URL params to the final controller

See PageReference method getParameters

Option 3 Store the Ids of the selected records in some semi-persistent place

One choice would be browser cookies. See setCookies PageReference methods

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.