0

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
7
  • can't you pass in the data through a second URL param for the custom button?
    – cropredy
    Commented Feb 5, 2020 at 18:20
  • so add a line: var classroom; at the top of my controller below Id parentId;? Then I can set the variable in the button URL? Commented Feb 5, 2020 at 18:26
  • yep - except var is not valid apex for declaring a type; you probably want either Id or String
    – cropredy
    Commented Feb 5, 2020 at 18:29
  • not working when i pass the string through the button URL.. any other tips? Commented Feb 6, 2020 at 17:39
  • You'll need to edit your post, include the button URL and update your code to current state. See How to Ask
    – cropredy
    Commented Feb 6, 2020 at 17:40

1 Answer 1

0

Per the chat conversation, some things that you'll need to do:

Classroom__c is a picklist

The URL should be:

/apex/AddStudents?parentId={!Teacher__c.Id}&classroom=KZ2020

The URL param should be retrieved in the control by:

String classroom = ApexPages.currentPage().getParameters().get('classroom');

The children should be built as

childList.add(new Student__c(Teacher__c=parentId, Classroom__c = classroom));

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .