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
    Feb 5 '20 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? Feb 5 '20 at 18:26
  • yep - except var is not valid apex for declaring a type; you probably want either Id or String
    – cropredy
    Feb 5 '20 at 18:29
  • not working when i pass the string through the button URL.. any other tips? Feb 6 '20 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
    Feb 6 '20 at 17:40
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));

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.