function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NetmasterNetmaster 

URL redirection after saving

Hello,

 

I want to redirect my page after saving to the details page just created, but I have an error :

 

public class insererEnfants{


public List<Enfants__c> accts {get; set;}

public insererEnfants(ApexPages.StandardController controller){
    accts = new List<Enfants__c>();
    accts.add(new Enfants__c());
}

public void addrow(){
    accts.add(new Enfants__c());
}

public PageReference save(){
    insert accts;
    PageReference acctPage = new ApexPages.StandardController(accts).view();
    acctPage.setRedirect(true);
    return acctPage;
}
}

 Here is the error :


Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<Enfants__c> at line 19 column 45

 Can you help me, please ?



Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

In the example below, I've assumed the parent sobject is 'Parent__c', and the master/detail relationship in Enfants__c is called 'Parent__c' and is populated.  You'll need to change the object/field names as appropriate:

 

public PageReference save()
{
    insert accts;
    Parent__c theParent=new Parent__c(id=accts[0].Parent__c];
    PageReference acctPage = new ApexPages.StandardController(theParent).view();
    acctPage.setRedirect(true);
    return acctPage;
}

 essentially you create a record of the appropriate type, with the id set to the parent record id and then use that to construct the standardcontroller instance.

All Answers

bob_buzzardbob_buzzard

You are trying to create a standard controller with a list of Enfants__c, but the standard controller needs a single record.

NetmasterNetmaster

thaks for you answer, I want to redirect my page after saving to a custom object record witch is the parent (master relationship) of Enfants__c

 

How can I do this, please ?

bob_buzzardbob_buzzard

In the example below, I've assumed the parent sobject is 'Parent__c', and the master/detail relationship in Enfants__c is called 'Parent__c' and is populated.  You'll need to change the object/field names as appropriate:

 

public PageReference save()
{
    insert accts;
    Parent__c theParent=new Parent__c(id=accts[0].Parent__c];
    PageReference acctPage = new ApexPages.StandardController(theParent).view();
    acctPage.setRedirect(true);
    return acctPage;
}

 essentially you create a record of the appropriate type, with the id set to the parent record id and then use that to construct the standardcontroller instance.

This was selected as the best answer
NetmasterNetmaster

That's fine, thanks a lot, it works !!

NetmasterNetmaster

I have an other question if you accept, I want to insert the value of the master relationship field automaticlly, and if possible I hide it.

 

This is my VF page :

<apex:page standardController="Enfants__c" extensions="insererEnfants" standardStylesheets="true">
    <apex:sectionHeader title="Ajouter un enfant"
        subtitle="{!$User.FirstName}" help="/help/doc/user_ed.jsp?loc=help"></apex:sectionHeader>
    <apex:form >
        <apex:pageBlock title="Nouveau enfant" id="thePageBlock" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Enregistrer"></apex:commandButton>
                <apex:commandButton action="{!cancel}" value="   Annuler   "></apex:commandButton>
            </apex:pageBlockButtons>
            
            <apex:pageBlockTable value="{!accts}" var="a" id="table">
                <apex:facet name="footer">
                    <apex:commandLink value="Ajouter" action="{!addRow}" rerender="table,error"/>
                </apex:facet>
                
                <apex:column headerValue="Parent">
                    <apex:inputField value="{!a.Parent__c}"/>
                </apex:column>
                <apex:column headerValue="Nom">
                    <apex:inputField value="{!a.Name}"/>
                </apex:column>
                <apex:column headerValue="Prénom">
                    <apex:inputField value="{!a.Prenom__c}"/>
                </apex:column> 
                                <apex:column headerValue="Né le">
                    <apex:inputField value="{!a.Date_de_naissance__c}"/>
                </apex:column>   
                                <apex:column headerValue="Lieu de naissance">
                    <apex:inputField value="{!a.Lieu_de_naissance__c}"/>
                </apex:column>   
                                <apex:column headerValue="Situation">
                    <apex:inputField value="{!a.Situation__c }"/>
                </apex:column>                          
            </apex:pageBlockTable>



            </apex:pageblock>
    </apex:form>
</apex:page>

 


bob_buzzardbob_buzzard

Do you know the parent record id in the controller?  If you do, I'd just iterate the accts before saving and set the field at that point.  You can then simply remove the inputfield.

NetmasterNetmaster

I removed the inputField "Parent__c" and I obtained this error

 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Parent]: [Parent] 
Class.insererEnfants.save: line 17, column 1

 

bob_buzzardbob_buzzard

Hence my question do you know the parent id server side - if you don't, you'll have to get it from the user.  If you do, you'll have to iterate the accts in the save method and fill in that field for each one of them.

NetmasterNetmaster

Yes, the situation is that from parent I clic a buttom "new" witch take me to the child object (the custom page above), I this page I insert the parent manually (inputField), I want to insert it automaticlly because it is the same parent all time. I hope that is more clear and thaks for your help.

bob_buzzardbob_buzzard

You should be able to pass the id of the parent on the URL if this is a custom button.

NetmasterNetmaster

Yes, it is a custom buttom, I want to pass the parent name to insert it in this field

bob_buzzardbob_buzzard

So you should be able to change that custom button to pass the id of the record that is currently being displayed on the URL.  Your controller can then pick this up.