I have a visualforce page, embedded in standard pagelayout of Account, that displays a list in peblocktable and can be inline edited. while hitting the Save button, it updates records and rerenders the pageblocktable with updated value. it works fine for the 1st time but next time onwards redirects to some invalid URL / or throws error System.ListException: Before Insert or Upsert list must not have two identically equal elements Error is in expression '{!save}' in component in page inline1: Class.inline1.Save: line 47, column 1 here is the code:
<apex:pageBlockTable value="{!records}" var="k" id="thePageBlockTable" rendered="{!records.size>0}">
<apex:column width="25" style="background:pink;">
<apex:inputField value="{!k.Competitor_Product__c}" id="AccountTypeDOM"/>
<apex:facet name="header">CompetitorProduct</apex:facet>
</apex:column>
<apex:column width="100" style="background: lightblue;" >
<apex:outputField value="{!k.January__c}" id="January" >
<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
/>
</apex:outputfield>
<apex:facet name="header">January</apex:facet>
</apex:column>
<apex:column width="100" style="background:lightgreen;" >
<apex:outputField value="{!k.February__c}" id="February" >
<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
/>
</apex:outputfield>
<apex:facet name="header">February</apex:facet>
</apex:column>
<apex:column width="100" style="background:lightblue;" >
<apex:outputField value="{!k.March__c}" id="March" >
<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
/>
</apex:outputfield>
<apex:facet name="header">March</apex:facet>
</apex:column>
<apex:column width="100" style="background:lightgreen;" >
<apex:outputField value="{!k.April__c}" id="April" >
<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
/>
</apex:outputfield>
<apex:facet name="header">April</apex:facet>
</apex:column>
<apex:column width="100" style="background:lightblue;" >
<apex:outputField value="{!k.May__c}" id="May" >
<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
/>
</apex:outputfield>
<apex:facet name="header">May</apex:facet>
</apex:column>
<apex:column width="100" style="background:lightgreen;" >
<apex:outputField value="{!k.June__c}" id="June" >
<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
/>
</apex:outputfield>
<apex:facet name="header">June</apex:facet>
</apex:column>
<apex:column width="100" style="background:lightblue;" >
<apex:outputField value="{!k.July__c}" id="July" >
<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
/>
</apex:outputfield>
<apex:facet name="header">July</apex:facet>
</apex:column>
<apex:column width="100" style="background:lightgreen;" >
<apex:outputField value="{!k.August__c}" id="August" >
<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
/>
</apex:outputfield>
<apex:facet name="header">August</apex:facet>
</apex:column>
<apex:column width="100" style="background:lightblue;" >
<apex:outputField value="{!k.September__c}" id="September" >
<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
/>
</apex:outputfield>
<apex:facet name="header">September</apex:facet>
</apex:column>
<apex:column width="100" style="background:lightgreen;" >
<apex:outputField value="{!k.October__c}" id="October" >
<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
/>
</apex:outputfield>
<apex:facet name="header">October</apex:facet>
</apex:column>
<apex:column width="100" style="background:lightblue;" >
<apex:outputField value="{!k.November__c}" id="November" >
<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
/>
</apex:outputfield>
<apex:facet name="header">November</apex:facet>
</apex:column>
<apex:column width="100" style="background:lightgreen;" >
<apex:outputField value="{!k.December__c}" id="December" >
<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton" rendered=""
/>
</apex:outputfield>
<apex:facet name="header">December</apex:facet>
</apex:column>
<apex:column >
<apex:commandButton value="X" action="{!Del}" rerender="thePageBlock">
<apex:param name="rowToBeDeleted" value="{!k.Id}" assignTo="{!del_Id}"></apex:param>
</apex:commandButton>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton action="{!cancel}" value="cancel" id="cancelButton" reRender="thePageBlock" style="display: none;vertical-align:bottom;" rendered="{!records.size>0}"/>
<!--apex:commandLink value="Save" action="{!save}" target="_parent" styleClass="btn" style="text-decoration:none;padding:4px;" id="saveButton" /-->
<!--apex:commandButton action="{!calledit}" value="edit" id="editButton" rendered="{!records.size>0}"/-->
<apex:commandButton value="Save" action="{!save}" id="saveButton" reRender="thePageBlock" style="display: none;vertical-align:bottom;" rendered="{!records.size>0}"/>
<apex:commandButton value="Add New" action="{!AddMore}" id="AddMoreButton" reRender="thePageBlock" style="vertical-align:bottom;" />
<!--apex:commandButton value="Add New row" action="{!AddMore}" id="AddNewRowButton" reRender="thePageBlock" style="vertical-align:bottom;" rendered="{!records.size>0}" /-->
<!-- <apex:commandLink value="Save" action="{!save}" target="_parent" styleClass="btn" style="text-decoration:none;padding:4px;" id="NewRowsaveButton"/> -->
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
controller code:
public class inline1{
public List<Competitor_Mapping__c> records=new List<Competitor_Mapping__c>();
//public Map<ID, List<Competitor_Mapping__c>> records = new Map<ID, List<Competitor_Mapping__c>>();
//public List<Competitor_Mapping__c> records= [select id,Competitor_Product__c,January__c,February__c,March__c,April__c,May__c,June__c,July__c,August__c,September__c,October__c,November__c,December__c from Competitor_Mapping__c];
public Set<Competitor_Mapping__c>lstAcct = new Set<Competitor_Mapping__c>();
public List<Competitor_Mapping__c>lstAcct1 = new List<Competitor_Mapping__c>();
public Boolean b{get;set;}
public Id Acc_Id {get;set;}
public Id Del_Id {get;set;}
public List<Competitor_Mapping__c>del_lst= new List<Competitor_Mapping__c>();
Public Account a{get;set;}
//ApexPages.StandardController stdCtrl;
public List<Competitor_Mapping__c> getrecords()
{
return records;
}
/*Constructor*/
public void inline1()
{
}/*End Constructor*/
/*Constructor*/
public inline1(ApexPages.StandardController controller)
{
// stdCtrl=controller;
Acc_Id =ApexPages.currentPage().getParameters().get('id');
records= [select id,Competitor_Product__c,January__c,February__c,March__c,April__c,May__c,June__c,July__c,August__c,September__c,October__c,November__c,December__c from Competitor_Mapping__c where Account__c=:ApexPages.currentPage().getParameters().get('id') order by Competitor_Product__c desc];
}
/*End Constructor*/
public void Save()
{
//PageReference pr = new PageReference('/'+Acc_Id);
//PageReference pr = new ApexPages.StandardController(a).view();
for(Integer j = 0;j<records.size();j++)
{
lstAcct.add(records[j]);
}
for (Competitor_Mapping__c a:lstAcct)
{
lstAcct1.add(a);
}
Upsert lstAcct1;
// pr.setRedirect(True);
// return pr;
// stdCtrl.save();
//records= [select id,Competitor_Product__c,January__c,February__c,March__c,April__c,May__c,June__c,July__c,August__c,September__c,October__c,November__c,December__c from Competitor_Mapping__c where Account__c=:ApexPages.currentPage().getParameters().get('id') order by Competitor_Product__c desc];
}
public void AddMore()
{
// PageReference pr = new PageReference('/apex/inline1');
Competitor_Mapping__c newRec=new Competitor_Mapping__c (Account__c=Acc_Id);
Insert newRec;
// pr.setRedirect(True);
// return pr;
records= [select id,Competitor_Product__c,January__c,February__c,March__c,April__c,May__c,June__c,July__c,August__c,September__c,October__c,November__c,December__c from Competitor_Mapping__c where Account__c=:ApexPages.currentPage().getParameters().get('id') order by Competitor_Product__c desc];
}
public void cancel()
{
records= [select id,Competitor_Product__c,January__c,February__c,March__c,April__c,May__c,June__c,July__c,August__c,September__c,October__c,November__c,December__c from Competitor_Mapping__c where Account__c=:ApexPages.currentPage().getParameters().get('id') order by Competitor_Product__c desc];
}
public PageReference calledit()
{
PageReference pr = new PageReference('/apex/inline1');
b=true;
pr.setRedirect(True);
return pr;
}
public void Del()
{
del_lst=[select id from Competitor_Mapping__c where Id =:del_id];
Delete del_lst;
records= [select id,Competitor_Product__c,January__c,February__c,March__c,April__c,May__c,June__c,July__c,August__c,September__c,October__c,November__c,December__c from Competitor_Mapping__c where Account__c=:ApexPages.currentPage().getParameters().get('id') order by Competitor_Product__c desc];
}
}