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
@anilbathula@@anilbathula@ 

creating duplicate records

hi guys ,

 

i am using a page block section for creating new and edit when creating new it works fine.

when editing the record its creating another record .Iam using upsert on the save button .

-------------Controller-------------------------

Public Class lComments{
public lead l {get;set;}
public list<Comments__c> Com{get; set;}
public Comments__c c{get;set;}
public Boolean property {get; set;}
public Boolean showblock{get;set;}
public Boolean hidebutton{get;set;}
public String SelId { get; set; }
public String delid{get;set;}
id str{get;set;}
public map<id,comments__c> mpid{get;set;}

public LComments(ApexPages.StandardController controller) {
l=(Lead)controller.getRecord();
str=Apexpages.currentpage().getparameters().get('id');
property=false;
hidebutton=true;
c=new Comments__c();
loaddata();
}
public void loaddata(){
com=[select id,name,lead__c,Contact__c,Opportunity__c,Finance__c,Comments_text__c,lead__r.Name ,Role__c ,CreatedDate from comments__c  where lead__c=:str order by CreatedDate desc limit 1000];
mpid=new map<id,Comments__c>([Select id, name from comments__c where id =:selid ]);
for(comments__c d:com){
mpid.put(d.id,d);

}
}

public PageReference save1(){
Comments__c c=new Comments__c(Name=c.Name,Comments_text__c=c.Comments_text__c,Lead__c=str);
upsert c;
PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
pageRef.setRedirect(true);
return pageRef;
}

public Comments__c getc(){
property=true;
hidebutton=false;
c=new comments__c();
return null;
}

Public void edit1(){
Property=true;
c=New Comments__c();
if(mpid.containsKey(SelId)){
c.Name=mpid.get(SelId).Name;
c.Comments_text__c=mpid.get(SelId ).Comments_text__c;
}

}

public Comments__c del1(){
Com=[select id from comments__c where id=:delid];
if(com.size()>0){
delete com;
}
loaddata();
return null;

}
}

----------------------Vf page----------------------------------------

--------
<apex:page standardController="Lead" extensions="lComments" >
<apex:form id="main" >
<table border="5">
<tr><td>
<div style="overflow: auto; height: 250px; width:500px;">
<apex:outputText value="{!Lead.Name}" style="font-size:12px;"/>
</div>
</td><td>
<div style="overflow: auto; height: 250px; width:500px;" >

<div id="parentdiv" style="text-align:center;width:30px;margin:auto;">
<apex:commandButton value="New" action="{!getc}" reRender="main,ref" rendered="{!hidebutton}" />
</div>

<apex:pageBlock rendered="{!Property}" id="ref" >
<apex:pageBlockSection columns="1" >
<apex:inputField value="{!c.Name}" required="true" />
<apex:inputField value="{!c.Comments_text__c}" style="width:380px;height:100px" />
</apex:pageBlockSection>
<apex:pageBlockButtons location="Bottom">
<apex:commandbutton action="{!Save1}" title="Save" value="Save" />
<apex:commandbutton action="{!cancel}" title="cancel" value="Cancel" onclick="window.history.previous()" />
</apex:pageBlockButtons>
</apex:pageBlock>

<apex:pageBlock >
<apex:pageBlockTable value="{!Com}" var="p" style="table-layout:fixed;" >

<apex:column width="100px" >
<apex:commandLink value="Edit" action="{!edit1}" reRender="main,ref" >
<apex:param name="cid" value="{!p.id}" assignTo="{!selid}"/>
</apex:commandlink>
&nbsp;&nbsp;&nbsp;&nbsp;
<apex:commandLink value="Del" action="{!del1}" reRender="main">
<apex:param name="cid" value="{!p.id}" assignTo="{!delid}"/>
</apex:commandLink>
</apex:column>

<apex:column style="overflow: hidden;">
<apex:facet name="header">Name</apex:facet>
{!If(LEN(p.Name)<25,p.Name,LEFT(p.Name,25))}
</apex:column>

<apex:column style="overflow: hidden;height:20px; ">
<apex:facet name="header">Comments</apex:facet>
{!If(LEN(p.Comments_text__c)<100,p.Comments_text__c,LEFT(p.Comments_text__c,100))}
</apex:column>
<apex:column value="{!p.Role__c}" />
<apex:column value="{!p.Lead__c}" />
<apex:column value="{!p.CreatedDate}" />
</apex:pageBlockTable>
</apex:pageBlock>
</div>
</td></tr>
</table>
</apex:form>
</apex:page>

------------------

Thanks

Anil.B

 

 

cloudForceDevcloudForceDev

In Method save1 you are not mapping the id, hence the duplicate record is created.

@anilbathula@@anilbathula@

Hi cloudForceDev,

 

Thanks a lot for ur reply .

I figured out the solution .

 

Thanks

Anil.B