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
MeerMeer 

How to insert record with VF Page

Hi, I have the following class and page, I have added the Page in the  detail pagelayout of custom object 'Journal'. Now can anybody tell me how can I auto fill the Journal__c field in the VfPage  with the current Journal detail page. Plus I want to save this New Line for thr current Journal. Please help me

 

********** Class **********

 

public class Fin_LineManager
{

public Line__c newLine{get; set;}


public Fin_LineManager(ApexPages.standardController stdn)
{
newLine = new Line__c();

}

public PageReference save()
{
insert newLine;
return null;
}
}

 

 

********** Page **********

 

<apex:page standardController="Fin_Journal__c" extensions="Fin_LineManager">
<apex:form >

<apex:pageBlock >

 

<apex:panelGrid columns="4" cellspacing="2" cellpadding="1">

<apex:outputLabel >Journal ID: </apex:outputLabel>
<apex:outputLabel value="{!newline.Journal__c}"/>

<apex:outputLabel >Credit: </apex:outputLabel>
<apex:inputField value="{!newline.Credit__c}"/>
<apex:outputLabel >Debit: </apex:outputLabel>
<apex:inputField value="{!newline.Debit__c}"/>
<apex:outputLabel >Description: </apex:outputLabel>
<apex:inputField value="{!newline.Description__c}"/>
<apex:commandButton action="Fin_LineManager.save()" value="Save"/>
</apex:panelGrid>

 

</apex:pageBlock>

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

 

 

NOTE: I have a related list of 'Line' in the Journal Page, I want to Update and refresh that realted list as well so the user can see that New Line has been added successfully.

 

Thanks

 

Meer

Best Answer chosen by Admin (Salesforce Developers) 
MeerMeer

Sorry there was a mistake in calling save function it should be action="{!save}"  and the constructor can go like this:

 

public Fin_LineManager(ApexPages.standardController stdn)

{
       newLine  = new Line__c(Journal__c = ApexPages.currentPage().getParameters().get('id'));
}

All Answers

Alok_NagarroAlok_Nagarro

Hi,

 

Try the following code ----

 

********** Class **********

 

public class Fin_LineManager
{

public Line__c newLine{get; set;}

public string id{get;set;}


public Fin_LineManager(ApexPages.standardController stdn)
{
newLine = new Line__c();

id=ApexPages.CurrentPage.getParameters().get('id);

newLine.Journal__c=id;

}

public PageReference save()
{
insert newLine;
return null;
}
}

 



********** Page **********

 

<apex:page standardController="Fin_Journal__c" extensions="Fin_LineManager">

<script>

 function goto()

{

 parent.location.href='/{!id}';

}

</script>
<apex:form >

<apex:pageBlock >

 

<apex:panelGrid columns="4" cellspacing="2" cellpadding="1">

<apex:outputLabel >Journal ID: </apex:outputLabel>
<apex:outputLabel value="{!newline.Journal__c}"/>

<apex:outputLabel >Credit: </apex:outputLabel>
<apex:inputField value="{!newline.Credit__c}"/>
<apex:outputLabel >Debit: </apex:outputLabel>
<apex:inputField value="{!newline.Debit__c}"/>
<apex:outputLabel >Description: </apex:outputLabel>
<apex:inputField value="{!newline.Description__c}"/>
<apex:commandButton action="{!save}" value="Save" onComplete="goto();"/>
</apex:panelGrid>

 

</apex:pageBlock>

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

MeerMeer

Sorry there was a mistake in calling save function it should be action="{!save}"  and the constructor can go like this:

 

public Fin_LineManager(ApexPages.standardController stdn)

{
       newLine  = new Line__c(Journal__c = ApexPages.currentPage().getParameters().get('id'));
}

This was selected as the best answer