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
gregusagregusa 

RecordType dropdown on a VisualForce page via Custom Controller

I have a custom object called "Overrides".  I've been developing a VisualForce page and custom controller that would allow users to data enter as many rows that they want of Overrides objects, at one time -- basically set them all up before clicking "Save".  On save each Overrides object row is saved.  This all works just as I want.

Now I need to add RecordTypes into the mix.  Ideally I want to have a dropdown at the top of the page where they will choose the record type of all of the Overrides objects they are creating for this particular "Save" operation.  I'm new to Apex and am having a hard time figuring this out.

Can someone show some sample code how to:

1) setup the controller to populate the RecordType dropdown box
2) set the dropdown box RecordType selection to each Overrides object when saving

Here is my controller:

Code:
public class multiOverrideInsertController{ 
      
    public List<Override__c> over{get; set;}   
    
    public multiOverrideInsertController(){        
        over= new List<Override__c>();        
        over.add(new Override__c());    
    }        

    public void addrow(){        
        over.add(new Override__c());    
    }     
    
    public void removerow(){  
        Integer i = over.size();      
        over.remove(i-1);    
    }        

    public PageReference save(){       
        insert over;        
        PageReference home = new PageReference('/a00/o');        
        home.setRedirect(true);        
        return home;    
    }
}

Here's my VisualForce page:

Code:
<apex:page controller="multiOverrideInsertController">    
    <apex:form >        
    <apex:pageBlock title="Insert Multiple Overrides">                    
        <apex:pageBlockButtons >                
            <apex:commandButton value="Save" action="{!save}" rerender="error,table"/>            
        </apex:pageBlockButtons>                    
        <apex:pageMessages ></apex:pageMessages>
    <apex:pageBlockTable value="{!over}" var="a" id="table">      
        <apex:column headerValue="Opportunity">                    
            <apex:inputField value="{!a.Opportunity__c}" required="true" />                         
        </apex:column>     
        <apex:column headerValue="Institution">                    
            <apex:inputField value="{!a.Institution__c}" required="true" />                         
        </apex:column> 
        <apex:column headerValue="Advisor (Rep)">                    
            <apex:inputField value="{!a.Advisor_Rep__c}" required="true" />                         
        </apex:column>                              
        <apex:column headerValue="Override Date">                      
            <apex:inputField value="{!a.Override_Date__c}" required="true" />                          
        </apex:column>
        <apex:column headerValue="Override Amount">                    
            <apex:inputField value="{!a.Override_Amount__c}" required="true" />                           
        </apex:column>                                          
     </apex:pageBlockTable>   
     <apex:pageblockButtons location="bottom">   
         <div style="text-align:right;margin-right:30px;font-weight:bold;">             
            <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error" immediate="true" /> &nbsp;|&nbsp;&nbsp;  
            <apex:commandLink id="RemoveRow" value="Remove Row" action="{!removeRow}" rerender="table,error" immediate="true" />
         </div>      
     </apex:pageblockButtons>                      
    </apex:pageBlock>    
    </apex:form>
</apex:page>




gregusagregusa
I've made some progress on this.  All I need to do now is find a way to bind the selected Record Type (from the dropdown) to each record as it is added via the controller.  If anyone could shine a light on how to do this, I'd appreciate it.

Here's my current VisualForce Page:

Code:
<apex:page controller="multiOverrideInsertController" title="Insert Multiple Overrides">    
    <apex:form >        
    <apex:pageBlock title="Insert Multiple Overrides">                    
        <apex:pageBlockButtons >                
            <apex:commandButton value="Save" action="{!save}" rerender="error,table"/>  
            <apex:commandButton value="Cancel" action="{!cancel}" rerender="error,table" immediate="true"/>          
        </apex:pageBlockButtons>                    
        <apex:pageMessages ></apex:pageMessages>
    <apex:pageBlockSection id="selectedRecordType" columns="1">
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Choose Record Type for batch of Overrides" for="rt" />         
            <apex:panelGrid columns="2">
                <apex:outputText value="RecordTypeID.ID" rendered="false" />
                <apex:selectList id="rt" value="{!RecordTypeID}" size="1" required="true">
                    <apex:selectOption itemValue="" itemLabel=""/>
                    <apex:selectOptions value="{!RecordTypeOptions}"/>
                </apex:selectList>              
            </apex:panelGrid>       
        </apex:pageBlockSectionItem>

    </apex:pageBlockSection>

    <apex:pageBlockTable value="{!over}" var="a" id="table">      
        <apex:column headerValue="Opportunity">                    
            <apex:inputField value="{!a.Opportunity__c}" required="true" />                         
        </apex:column>     
        <apex:column headerValue="Institution">                    
            <apex:inputField value="{!a.Institution__c}" required="true" />                         
        </apex:column> 
        <apex:column headerValue="Advisor (Rep)">                    
            <apex:inputField value="{!a.Advisor_Rep__c}" required="true" />                         
        </apex:column>                              
        <apex:column headerValue="Override Date">                      
            <apex:inputField value="{!a.Override_Date__c}" required="true" />                          
        </apex:column>
        <apex:column headerValue="Override Amount">                    
            <apex:inputField value="{!a.Override_Amount__c}" required="true" />                           
        </apex:column>                                          
     </apex:pageBlockTable>   
     <apex:pageblockButtons location="bottom">   
         <div style="text-align:right;margin-right:30px;font-weight:bold;">             
            <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error" immediate="true" /> &nbsp;|&nbsp;&nbsp;  
            <apex:commandLink id="RemoveRow" value="Remove Row" action="{!removeRow}" rerender="table,error" immediate="true" />
         </div>      
     </apex:pageblockButtons>                      
    </apex:pageBlock>    
    </apex:form>
</apex:page>

 
Here's my controller:
Code:
public class multiOverrideInsertController{ 
      
    public List<Override__c> over{get; set;}   
      
    public string RecordTypeID {get ; set ;}
         
    public List<selectOption> RecordTypeOptions {get {
        List<selectOption> myRecordTypes = new List<selectOption>();
        for (RecordType rt : [select Name from RecordType where RecordType.SobjectType = 'Override__c'])
            myRecordTypes.add(new selectOption(rt.id, rt.name));
        return myRecordTypes;
        }
        private set;
    }
    
    //new override, this code not working: RecordTypeID = RecordTypeID
    public multiOverrideInsertController(){             
        over= new List<Override__c>();        
        over.add(new Override__c());    
    }        

    public void addrow(){        
        over.add(new Override__c());    
    }     
    
    public void removerow(){  
        Integer i = over.size();      
        over.remove(i-1);    
    }        

    public PageReference save(){       
        insert over;        
        PageReference home = new PageReference('/a00/o');        
        home.setRedirect(true);        
        return home;    
    }
    
    public PageReference cancel(){            
        PageReference home = new PageReference('/a00/o');        
        home.setRedirect(true);        
        return home;    
    }    
}

 

fifedogfifedog
Have you gotten anywhere else for this?  I have the same issue where I want to only display those values of a drop down for the related record type.
gregusagregusa
Yes I was finally able to make it work.  The key to making it work was looping through the collection of mass objects in the PageReference Save() method, before inserting them, and setting the record type to the dropdown.

The only thing I couldn't figure out was how to return a message to the user if they didn't make a selection with the dropdown.  Right now if they don't it will throw a SalesForce error.  If you can figure that out, please let me know.

Here's the controller:
Code:
public class multiOverrideInsertController{ 
      
    public List<Override__c> over{get; set;}         
      
    String recType;
    
    public List<SelectOption> getrectypes() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('','--Select Record Type --'));
        for(RecordType rt:[select id,name from RecordType where sobjecttype='Override__c']){
            options.add(new SelectOption(rt.id,rt.name));    
    }
        return options;
    }
    
    public String getRecordType() {
        return recType;
    }
    
    public void setRecordType(String recType) {
        this.recType= recType;
    }
        
    public multiOverrideInsertController(){             
        over= new List<Override__c>();        
        over.add(new Override__c());    
    }       

    public void addrow(){        
        over.add(new Override__c());    
    }     
    
    public void removerow(){  
        Integer i = over.size();      
        over.remove(i-1);    
    }        
      
    public PageReference save(){ 
        
        Integer count = over.size();
        Integer i = 0;
                    
            do {
                over[i].recordtypeid=recType;
                i++;
                } while (i < count);
                         
            insert over;                         
                                
            PageReference home = new PageReference('/a00/o');        
            home.setRedirect(true);        
            return home;                    
            }  
        
        
    public PageReference cancel(){            
        PageReference home = new PageReference('/a00/o');        
        home.setRedirect(true);        
        return home;    
    }    
}

Here's the VisualForce page:
Code:
<apex:page controller="multiOverrideInsertController" title="Insert Multiple Overrides">    
    <apex:form >        
    <apex:pageBlock title="Insert Multiple Overrides">                    
        <apex:pageBlockButtons >                
            <apex:commandButton value="Save" action="{!save}" rerender="error,table"/>  
            <apex:commandButton value="Cancel" action="{!cancel}" rerender="error,table" immediate="true"/>          
        </apex:pageBlockButtons>                    
        <apex:pageMessages ></apex:pageMessages>
        <apex:messages />        
    <apex:pageBlockSection id="selectedRecordType" columns="1">
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Choose Record Type for batch of Overrides" for="rt" />         
            <apex:panelGrid columns="2">
            <apex:selectList value="{!RecordType}" multiselect="false"  size="1">
                <apex:selectOptions value="{!rectypes}"/>
            </apex:selectList>              
            </apex:panelGrid>       
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
    <apex:pageBlockTable value="{!over}" var="a" id="table">            
        <apex:column headerValue="Opportunity">                    
            <apex:inputField value="{!a.Opportunity__c}" required="true" />                         
        </apex:column>     
        <apex:column headerValue="Institution">                    
            <apex:inputField value="{!a.Institution__c}" required="true" />                         
        </apex:column> 
        <apex:column headerValue="Advisor (Rep)">                    
            <apex:inputField value="{!a.Advisor_Rep__c}" required="true" />                         
        </apex:column>                              
        <apex:column headerValue="Override Date">                      
            <apex:inputField value="{!a.Override_Date__c}" required="true" />                          
        </apex:column>
        <apex:column headerValue="Override Amount">                    
            <apex:inputField value="{!a.Override_Amount__c}" required="true" />                           
        </apex:column>                                          
     </apex:pageBlockTable>   
     <apex:pageblockButtons location="bottom">   
         <div style="text-align:right;margin-right:30px;font-weight:bold;">             
            <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error" immediate="true" /> &nbsp;|&nbsp;&nbsp;  
            <apex:commandLink id="RemoveRow" value="Remove Row" action="{!removeRow}" rerender="table,error" immediate="true" />
         </div>      
     </apex:pageblockButtons>                      
    </apex:pageBlock>    
    </apex:form>
</apex:page>

 


SGrabo_NutriciaSGrabo_Nutricia

Bear with me: I'm only a week into Apex and controllers, so it's still a bit bumpy.

 

I have a very similar need: associating a dropdown's value with a particular record, where the dropdown is being populated by a data source beyond the current object. However, instead of the dropdown value being a single instance, I need it to be repeated for each new addrow() I execute. The user will be selecting a different value from the dropdown for each record they're creating.

 

I largely understand the get/set you're executing to hold the value in a property (rectype), and in how you're looping through the instances to assign that value to each. What I'm unclear on is how to identify which instance of each dropdown is associated with each record, and then to grab the associated value.

 

In other words, if another instance of your recType dropdown was associated with each new Over instance, how would you identify which value the user has selected for each dropdown?

 

Any thoughts would be greatly appreciated. You're example has already moved me several steps further down the road...

gregusagregusa

I feel your pain. Unfortunately I'm not much further along than you.  As far as I can tell, you would have to call the set and get methods from within the SAVE loop, catching the row's select list value, setting the controllers variable, then retrieving that:

 

 

do { CALL SET RECORDTYPE METHOD HERE, SET = SELECT LIST VALUE over[i].recordtypeid= CALL GET RECORD TYPE HERE; i++; } while (i < count); insert over;

 

 I would think there would be something easier, but it'll take someone further along than me to help.  Can't wait to see if someone chimes in here!

 

SGrabo_NutriciaSGrabo_Nutricia

I'm not sure I understand how you're assigning recType. I would expect to see a SetRecType method, but there's not one in your controller. In your code, you have the following:

 

public String getRecordType() { return recType; } public void setRecordType(String recType) { this.recType= recType; }

 I assume your Save method is interacting with this somehow:

 

do { over[i].recordtypeid=recType; i++; } while (i < count);

 but I don't see how the value of recType is ever being established. I'd like to do the same thing, essentially doing something like:

 

public PageReference save(){ Integer count = over.size(); Integer i = 0; do { over[i].recordtypeid=recType[i]; i++; } while (i < count); insert over;

 so that it knows which recType selectList value to use for each over instance, but I can't see how to reference that. Any thoughts?

 

 

 

mmaxtrammaxtra

Hi:

   How did you write the testclass for the recordtype section? May I see please.

Thanks