You need to sign in to do that
Don't have an account?

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:
Here's my VisualForce page:
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" /> | <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 current VisualForce Page:
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:
Here's the VisualForce page:
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...
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'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?
Hi:
How did you write the testclass for the recordtype section? May I see please.
Thanks