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
dev.shiv1.3478599680068948E12dev.shiv1.3478599680068948E12 

Creating the same records again while adding the existing records to the related list.

Hi, I created a Visualforce page for a List button to the related list object to add the child records into parent related list.

The controller I've written for that is shown below. Its adding duplicate records and as well as creating the same records again and again when ever i add the records. Will you please tell me the logic how to add the records and does not allow duplicates or create new records for  the child object....

 

public with sharing class ProgramController {
public Program__c Program ;
public List<Wrapper> wraplist{get;set;}
public ProgramController(ApexPages.StandardController controller) {
program = (Program__c) Controller.getRecord();
wraplist = new List<Wrapper>();
List<Title__c> t = [SELECT id,name,Program__c FROM Title__c LIMIT 20];

for(title__c t1:t){
wrapList.add( new wrapper(t1));
}
}
public class Wrapper{
public Boolean Selected{get;set;}
public title__c title{get;set;}
public wrapper(Title__c ti){
title = ti;
}
}
public PageReference addTitle(){
List<Title__c> newTitle = new List<Title__c>();
for(Wrapper wrap :WrapList){

if(Wrap.Selected== true)
{

title__c newtitle1 = new Title__c(Name=wrap.title.name,Program__c=program.id);

newTitle.add(newtitle1);
}
upsert newtitle;
}
return (new ApexPages.StandardController(Program)).view();
}
}

yvk431yvk431

Can you explain it in detail, i mean what happens when you click the list buttton. Also why you are searching for only 20 titles at line 7?

 

 

--yvk

dev.shiv1.3478599680068948E12dev.shiv1.3478599680068948E12

I need to add selected checkbox records(child) to the related list for particular parent record. For that I created a Listbutton(url button: apex/PageName&id={!ParentObject.Id}) which directs to a visualforce page to select records and adding them to the related list. In this process when ever I add new records to the related list my code allowing  me the older records also as well as creating the new records with the same name which i add to the related list earlier which is resulting duplicate records in child object.

 

 I need to stop creating duplicates in the child object and does not allow the older records to add them again into related list.

yvk431yvk431

If you dont want to add already added titles, just avoid them in the query itself.

Just create a set of strings with all the existing child Title names and exclude those using the soql

 

before line 7

Set<string> titlestr = new set<string>();

List<Title__c> PTLst = [SELECT id,name,Program__c FROM Title__c  where Program__c = :program.id];

 

for(Title__c t : PTLst)

{

    titlestr.add(t.Name);

}

 

List<Title__c>  t = [SELECT id,name,Program__c FROM Title__c  where name not in :titlestr limit 20];

 

 

--yvk

 

 

dev.shiv1.3478599680068948E12dev.shiv1.3478599680068948E12

The logic you gave is correct. but how would you call the wrapper class into the Set method titlestr ? If you call wraper list then only we can retrieve the title__c records there. How can we assign wraplist to the titlestr?

yvk431yvk431

its not a method, you need to replace the line 7 with the code I gave.

 

--yvk

 

 

visuvisu
hi Shiv,

I have a similar requirement on a related list, can you please let me know if this is possible? Also please share the VF page code if possible for reference... Thanks. Please reply at the earliest.
Shashikant SharmaShashikant Sharma
Hi,

Not sure what you exactly want from this page but you could remove dulicate issue by replacing this 

title__c newtitle1 = new Title__c(Name=wrap.title.name,Program__c=program.id);

and use this

title__c newtitle1;

if( wrap.title.Id == null ) {
     newtitle1 = new Title__c(Name=wrap.title.name,Program__c=program.id);
 }
else {
     newtitle1 = new Title__c(Id = wrap.title.Id,  Name=wrap.title.name,Program__c=program.id);
}
Thing to understands that I used Id check and based on it created the instance of newTitle1 . If wrap.title.Id not null then populated it so that record is updated insted on insert.

Let me know if you have any questions.