I have a controller and visualforce page that I'm using to create child records off of a list button on the parent record in SFDC. I now need to set a value of a custom field on the child object: Classroom__c . The Classroom__c field will contain a specific value that I'd like to hardcode (different buttons will distribute different hard coded values).
How can I modify my existing code to set the Student.Classroom__c to "KZ2020"?
Controller:
public class AddStudents {
Id parentId; String classroom;
**String classroom;**
public List<Student__c> childList {get;set;}
public Integer rowNum{get;set;}
public AddStudents(){
Id childId = ApexPages.currentPage().getParameters().get('childId');
ParentId=ApexPages.currentPage().getParameters().get('ParentId');
childList = new List<Student__c>();
childList.add(new Student__c(Teacher__c=parentId));
childList.add(new Student__c(Teacher__c=parentId));
childList.add(new Student__c(Teacher__c=parentId));
childList.add(new Student__c(Teacher__c=parentId));
childList.add(new Student__c(Teacher__c=parentId));
}
public pageReference insertChild(){
insert childList;
Pagereference page=new pagereference('/'+parentId);
Return page;
}
public void insertRow(){
Id parentId = ApexPages.currentPage().getParameters().get('parentId');
childList.add(new Student__c(Teacher__c=parentId));
}
}
Visualforce Page:
<apex:page controller="AddStudents" tabstyle="Student__c" >
<apex:form >
<apex:variable value="{!0}" var="index" />
<apex:pageBlock title="Add Students">
<apex:PageBlockTable value="{!childList}" var="stu">
<apex:facet name="footer">
<apex:commandLink value="Add" action="{!insertRow}"/>
</apex:facet>
<apex:column headerValue="Student Number">
<apex:outputText style="font-weight:bold;">
<apex:variable value="{!index + 1}" var="index"/>
Tier {!index} </apex:outputText>
</apex:column>
<apex:column headerValue="Name>
<apex:inputField value="{!stu.Name__c}"/>
</apex:column>
<apex:column headerValue="Subject">
<apex:inputField value="{!stu.Subject__c}"/>
</apex:column>
</apex:PageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!insertChild}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
Button URL (on Student__c Object):
/apex/AddStudents?parentId={!Teacher__c.Id}&{!Classroom__c}=KZ2020
var classroom;
at the top of my controller belowId parentId;
? Then I can set the variable in the button URL?var
is not valid apex for declaring a type; you probably want either Id or String