1

I have a use case where I have to display some fields on visualforce page and then whatever data user provides on the page, grab the data and create a record out if it. Now the record needs to be created as a related list to a master object hence I need to supply a master-detail field id named "student_form__c" but when I do that, it gives me an error that the master-detail field cannot be written. Also, I need to provide user a "Add new" button on visualforce page. Upon clicking the button, the same set of fields appear again on visualforce and a record gets created again upon saving it. So this way user can create n number of records.

Visualforce page:

<apex:page controller="Dummy_Controller">
  <Apex:form>
     <apex:pageBlock>
         <apex:pageBlockButtons>
            <apex:commandButton action="{!Save}" value="Save" />
            <!-- Has to be immediate - otherwise the form tries to "submit" and triggers built in validation --> 
            <apex:commandButton action="{!AddNew}" value="Add New" immediate="true" />
        </apex:pageBlockButtons>

        <!-- Repeat for each record, create a "row" where we can put inputs --> 
        <apex:repeat var="record" value="{!Records}">
            <!-- wrap each record with an actionRegion - ensures the fields are submitted when adding new rows --> 
            <apex:actionRegion>
                <apex:pageBlockSection>
                        <apex:inputField value="{!Record.Name}"  />
                        <apex:inputField value="{!Record.Status}"  />
                </apex:pageBlockSection>
            </apex:actionRegion>
        </apex:repeat>
    </apex:pageBlock>
</Apex:form>

Here is my controller:

public class Dummy_Controller {

public List<Application__c> Records { get; set; } // our variable 

public Dummy_Controller() {
    Records = new List<Application__c>(); // initial state 
    Records.add(new Application__c()); // initial record 
}

public PageReference Save() {

    /*I have already fetched the studentformid from url in apex class 
      constructor like this Id StudentFormId = 
      ApexPages.currentPage().getParameters().get('id'); Now I want to 
      assign this fetched id to student_form__c field*/

     for(Application__c record:records) 

     **// Here is the problem**

    { 
       record.student_form__c = StudentFormId; 

     }

    insert Records; // save all records 

    return new PageReference('/' + Records[0].Id); // redirect to saved record 
}

public PageReference AddNew() {
    Records.add(new Application__c()); // add another record 

    return null; 
  }

}

1

There are 2 issues I can see, because in the save method you are looping records which are type of Application__c.

You have created List<Account>,it should be List<Application__c>

public List<Application__c> Records { get; set; } // our variable 

public Dummy_Controller() {
    Records = new List<Application__c>(); // initial state 
    Records.add(new Application__c()); // initial record 
}

Same way, in the Add new method

public PageReference AddNew() {
    Records.add(new Application__c()); // add another record 

    return null; 
  }

In the above solution, I have presumed that a Custom object cannot be a Master of Standard object (Account).

Update

When you are creating an instance of record, assign the MasterId at that moment and no need to update that in save method.

public PageReference AddNew() {
    //retrieve StudentFormId in the constructor and use it here.
    Records.add(new Application__c(student_form__c = StudentFormId)); // add another record 

    return null; 
  }

Otherwise, in the configuration choose allow reparenting on the master-detail relationship so that the id will be writeable.

  • Thanks for replying. I am sorry I had some typos in my above code, mixed two codes. I have updated the code above. I am dealing entirely with Application and Student form object. Studentform is the master of application object and both are custom objects. I am having issue in specifying the master-detail field id in order to create records at the appropriate place. – Student Aug 9 '18 at 19:29
  • check my updated answer – Santanu Boral Aug 9 '18 at 19:47
  • I tried that. It gave me this error on visualforce upon submission. Error: Insert failed. First exception on row 0 with id a1Rr0000000eI6hEAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] I don't know why, it is not the actual record's id, it is just a master-detail field id. – Student Aug 9 '18 at 19:57
  • 1
  • 1
    There were actually two issues. First one was providing master-detail id. After I solved that by specifying the id right where I am defining the list, it lead to second issue that I mentioned above. Thanks for all the help. – Student Aug 10 '18 at 17:18

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.