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
Karthik TathiReddyKarthik TathiReddy 

Creating the new records in child object while adding them to the parent related list

**************Controller*************
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>();
        
       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 r : PTLst){

          titlestr.add(r.name);

         }

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

    // 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 newtitle1;
    
           }
    
         }

      PageReference programPage = new ApexPages.StandardController(program).view();
    
      programPage.setRedirect(true);
    
      return programPage;

    }

}



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

<apex:page StandardController="Program__c" extensions="ProgramController">
<apex:form >
  <apex:pageBlock >
  <apex:pageBlockButtons >
  <apex:commandButton action="{!addTitle}" value="Add Title"/>
  </apex:pageBlockButtons>
  <apex:pageBlockSection >
  <apex:pageBlockTable value="{!WrapList}" var="w">
  <apex:column headerValue="Action">
  <apex:inputCheckbox value="{!w.Selected}"/>
  </apex:column>
  <apex:column value="{!w.title.name}"/>
  </apex:pageBlockTable>
  </apex:pageBlockSection>
  </apex:pageBlock>
  </apex:form>
</apex:page>

 The above visualforce page I created a button and assigned the VFpage to the buttonURL to the parent object Program__c. Title__c is the title object. The above code results in adding the selected records to the related list of a program__c record. In this process what ever the records I added from the Title object the same records creating again in the title object which resulting to duplicate records. Please help me out regarding this.

Best Answer chosen by Admin (Salesforce Developers) 
logontokartiklogontokartik

Looks like your controller is messed up causing the duplicate stuff. If I understand right, all you are doing is displaying all titles that are not assigned to current program and then allowing user to select titles and assign them to current program?  Try the below code and let me know if it works.

 

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>();
       
       for(Title__c t1 : [SELECT id,name,Program__c FROM Title__c where Program__c != :program.Id]){
         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){
             wrap.title.Program__c = program.Id;
             newTitle.add(wrap.title);
           }
         }
		 
	  upsert newtitle;	 

      PageReference programPage = new ApexPages.StandardController(program).view();
    
      programPage.setRedirect(true);
    
      return programPage;

    }

}

 

All Answers

logontokartiklogontokartik

Looks like your controller is messed up causing the duplicate stuff. If I understand right, all you are doing is displaying all titles that are not assigned to current program and then allowing user to select titles and assign them to current program?  Try the below code and let me know if it works.

 

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>();
       
       for(Title__c t1 : [SELECT id,name,Program__c FROM Title__c where Program__c != :program.Id]){
         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){
             wrap.title.Program__c = program.Id;
             newTitle.add(wrap.title);
           }
         }
		 
	  upsert newtitle;	 

      PageReference programPage = new ApexPages.StandardController(program).view();
    
      programPage.setRedirect(true);
    
      return programPage;

    }

}

 

This was selected as the best answer
Karthik TathiReddyKarthik TathiReddy

Thank you Kartik. Your code is really helpful to me. Thanks a lot.

Karthik TathiReddyKarthik TathiReddy

Hi kartik!

It is more to add some logic to your code. If I delete any records from the added records in the related list, Its deleting from the Title object. But after deleting any record from the related list it should not delete any records from the title object. Could you give help regarding this?

logontokartiklogontokartik

Hmm. For your use-case, you have to create a junction object between Program__c & Title__c. And in your VF page, when someone adds a title create a record of that junction object which shows up on the Program__c related list. 

 

This would solve your issue of deleting records, and also allows you to establish a many-many relationship between objects.