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 

List of New Records

Hi.. I am having a problem in saving the last record.. The feilds for the last record are not saving.. I have modified a code of Bob_bazard from his blog 

http://bobbuzzard.blogspot.co.uk/2011/07/managing-list-of-new-records-in.html

 

If someone can help me in this please

 

*********Controller*********

 

public class Line_Manager
{
public Fin_Journal__c journal {get;set;}
public Line__c newLine {get; set;}
public List<LineWrapper> wrappers {get; set;}
public static Integer toDelIdent {get; set;}
public static Integer addCount {get; set;}
public Boolean refreshPage {get; set;}
private Integer nextIdent=0;

public Line_Manager ()
{
journal = new Fin_Journal__c();
wrappers=new List<LineWrapper>();
wrappers.add(new LineWrapper(nextIdent++));
}

public Line_Manager (ApexPages.StandardController controller)
{
journal = new Fin_Journal__c();
wrappers=new List<LineWrapper>();
wrappers.add(new LineWrapper(nextIdent++));
}


public Fin_Journal__c GetJournal()
{
Fin_Journal__c journal = [SELECT Name, Description__c FROM Fin_Journal__c WHERE id=: ApexPages.currentPage().getParameters().get('id')];
return journal;
}

public void delWrapper()
{
Integer toDelPos=-1;
for (Integer idx=0; idx<wrappers.size(); idx++)
{
if (wrappers[idx].ident==toDelIdent)
{
toDelPos=idx;
}
}

if (-1!=toDelPos)
{
wrappers.remove(toDelPos);
}
}

public void addRows()
{
for (Integer idx=0; idx<addCount; idx++)
{
wrappers.add(new LineWrapper(nextIdent++));
}
}

public PageReference save()
{
insert journal;

List<Line__c> lines = new List<Line__c>();
for (LineWrapper wrap : wrappers)
{
wrap.line.Journal__c = journal.id;
lines.add(wrap.line);
}
insert lines;


PageReference page = new PageReference ('https://ap1.salesforce.com/a0I/o');
return page;
}

public class LineWrapper
{
public Line__c line {get; set;}
public Integer ident {get; set;}

public LineWrapper(Integer inIdent)
{
ident=inIdent;
line =new Line__c();
}
}

}

 

 

 

*********VF Page*********

 

 

<apex:page standardController="Fin_Journal__c" extensions="Line_Manager">
<script src="../../soap/ajax/24.0/connection.js" type="text/javascript"></script>
<script type="text/javascript">
function GetDescription()
{
var queryresult = sforce.connection.query("SELECT Description__c FROM Code_Combination__c WHERE Name = '" + document.getElementById('{!$Component.MyForm:acc}').value + "'", queryresult);
var records = queryresult.getArray('records');
document.getElementById('{!$Component.MyForm:desc}').value = records [0].Description__c;
}
</script>

<apex:form id="MyForm">

<table border="0" cellspacing = "0" cellpadding= "2">
<tr><td></td></tr><tr><td></td></tr><tr>
<td><apex:image url="https://c.ap1.content.force.com/servlet/servlet.FileDownload?file=01590000000O4Wx" width="35" height="40"/></td>
<td>
<apex:outputText ><font size="1" color="#606060" face="Arial"><b>New Journal</b></font></apex:outputText><br/>
<apex:outputText ><font size="4" color="Black" face="Arial">Journal Edit</font></apex:outputText>
</td>
</tr><tr><td></td></tr><tr><td></td></tr>
</table>

<center>
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Save & New"/>
<apex:commandButton value="Cancel"/>
</center>

<table><tr><td>&nbsp;</td></tr></table>

<apex:pageBlock mode="edit">
<apex:pageblockSection columns="1" Title="Journal Details">
<apex:inputfield value="{!journal.Period__c}"/>
<apex:inputfield value="{!journal.Journal_Date__c}"/>
<apex:inputfield value="{!journal.Card__c}"/>
<apex:inputfield value="{!journal.Description__c}" style="width:270px"/>
<apex:outputLabel ></apex:outputLabel>
<apex:outputLabel ></apex:outputLabel>
</apex:pageblockSection>
</apex:pageBlock>

<apex:pageBlock mode="edit">
<apex:pageblockSection Title="Journal Settings">
<apex:inputfield value="{!journal.Journal_Name__c}"/>
<apex:outputLabel ></apex:outputLabel>
<apex:inputfield value="{!journal.Batch__c}"/>
<apex:inputfield value="{!journal.Source__c}"/>
<apex:inputfield value="{!journal.Currency__c}"/>
<apex:inputfield value="{!journal.Status__c}"/>
<apex:inputfield value="{!journal.Category__c}"/>
<apex:inputfield value="{!journal.Currency__c}"/>
<apex:outputLabel ></apex:outputLabel>
<apex:inputfield value="{!journal.Include_Tax__c}"/>
<apex:outputLabel ></apex:outputLabel>
<apex:outputLabel ></apex:outputLabel>
</apex:pageblockSection>
</apex:pageBlock>

</apex:form>

<apex:form >
<apex:pageBlock mode="edit">
<apex:pageBlockSection title="Lines" columns="1">
<apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
<apex:column headerValue="Sr. #">
<apex:outputText value="{!wrapper.ident}"/>
</apex:column>
<apex:column headerValue="Account Code" style="width:30%">
<apex:inputField value="{!wrapper.line.Code_Combination__c}" style="width:90%"/>
</apex:column>
<apex:column headerValue="Description" style="width:30%">
<apex:inputField value="{!wrapper.line.Description__c}" style="width:99%"/>
</apex:column>
<apex:column headerValue="Job" style="width:20%">
<apex:inputField value="{!wrapper.line.Job__c}" style="width:87%"/>
</apex:column>
<apex:column headerValue="Debit" style="width:6%">
<apex:inputField value="{!wrapper.line.Debit__c}" style="width:99%"/>
</apex:column>
<apex:column headerValue="Credit" style="width:6%">
<apex:inputField value="{!wrapper.line.Credit__c}" style="width:99%"/>
</apex:column>
<apex:column headerValue="Action">
<apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">
<apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/>
</apex:commandButton>
</apex:column>
</apex:pageBlockTable>
<apex:commandButton value="New Line" action="{!addRows}" rerender="wtable">
<apex:param name="addCount" value="1" assignTo="{!addCount}"/>
</apex:commandButton>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

</apex:page>


Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

That's strange too.  However, you could just default these values when you add them to the list,  e.g.

 

public class LineWrapper
{
   public Line__c line {get; set;}
   public Integer ident {get; set;}
   public LineWrapper(Integer inIdent)
   {
      ident=inIdent;  
      line =new Line__c(Debit__c=0, Credit__c=0);
   }
}

 

Its not ideal, but should suffice as a workaround.

All Answers

bob_buzzardbob_buzzard

I can't see anything obviously wrong.  If you only have a single record do you see the same problem?

 

MeerMeer

Yes I am having the same problem. If I save only one record  the details (feilds values) are not saved and If I save 5 or more records than the first four records are properly saved but the last record's details are not saved. However number of records I want to insert are saved accoridingly but the only issue is with details of the records.

 

Beside this there is one more issue I am facing, there are two fields Debit__c and Credit__c they both have their default value '0' but when I create another record (i.e. 2nd Record) than their default values are not shown in the text box, where as the first record do have.

 

Thanks a lot

 

Regards,

Meer

bob_buzzardbob_buzzard

That's very strange.  I can't see any reason why things would behave differently for individual elements in the list - its the exact same code that is being used to create the elements and add them to the list.  It almost suggests that the save isn't causing a postback, though I can't see why that would be the case,  The only thing that looks a trifle odd is the fact that you have two different forms.  Try moving it all into a single form and see if that helps.

MeerMeer

:D It worked when I put in the same form :) but the default value issue is still there. For new Record I can't see the default values (except the first record)

 

Regards,

Meer

bob_buzzardbob_buzzard

That's strange too.  However, you could just default these values when you add them to the list,  e.g.

 

public class LineWrapper
{
   public Line__c line {get; set;}
   public Integer ident {get; set;}
   public LineWrapper(Integer inIdent)
   {
      ident=inIdent;  
      line =new Line__c(Debit__c=0, Credit__c=0);
   }
}

 

Its not ideal, but should suffice as a workaround.

This was selected as the best answer
MeerMeer

Thanks Bob

 

Regards,

Meer