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
MattyRayMattyRay 

Creating multiple related records from the related list using a VF page.

Need some help please.

 

We have a related record on the opportunity called "prospect". The prospect simply connects the opportunity to contacts. We have a related list for prospects, but the users want an easy way to create multiple "prospects" at one time. 

 

I want to create a button on the related list that takes you to a VF page with a list of contacts by name.  I will need to select multiple names then submit, creating a prospect record for each selected contact.  

 

Any Ideas?

Best Answer chosen by Admin (Salesforce Developers) 
sushant sussushant sus

hi,

 

you have to make a cuttom button and call a page

 

like this

window.open('/apex/DuplicateScheduleTemplate?id={!

JobSuite__Schedule_Template__c.Id}','_self');// please exact format

 

code for check with contact

 

controller :

public with sharing class checkboxcls {
    public PageReference selected() {
    System.debug('------------------------>lstwrap is'+lstwrap);
    for(wrappecls w:lstwrap){
        if(w.check==true){
            System.debug('---------->'+w.con);
            System.debug('-----+++++++++----->'+w.con.name);

           // you can use apexpage .cuurent.getparameter(id) (*check syntax ) to get opp id that pospect is assign to opp
           // here you can add your logic of creating prospect and  use contact id for assign to contact

            // create prospect object instance add to list of prospect object


                                   }
                                         }  // insert prospect list
        return null;
                                                      }
    public class wrappecls{
        public boolean check{get; set;}
        public Contact con{get; set;}
     
        public wrappecls(Contact c){
            con =c;
            check=false;
                                                    }
     
                                      }
 
    LIst<wrappecls> lstwrap= new List<wrappecls>();
 
    public List<wrappecls> getitems(){
    List<Contact> lst=[Select id, name, phone from Contact];
    for(Contact c:lst){
    lstwrap.add(new wrappecls(c));
                              }
    System.debug('------------------------>lstwrap is'+lstwrap);
    return lstwrap;
                                                       }
                                                         }

 

page:

 

<apex:page controller="checkboxcls">
<apex:form >
    <apex:PageBlock >
        <apex:pageBlockTable value="{!items}" var="i">
        <apex:column >
            <apex:inputCheckbox value="{!i.check}"/>
        </apex:Column>
                <apex:column value="{!i.con.name}"/>
                <apex:column value="{!i.con.phone}"/>
        </apex:pageBlockTable>
        <apex:commandButton value="Selected" action="{!selected}"/>
    </apex:PageBlock>
</apex:form>
</apex:page>

 

 

 

All Answers

sushant sussushant sus

hi,

 

you have to make a cuttom button and call a page

 

like this

window.open('/apex/DuplicateScheduleTemplate?id={!

JobSuite__Schedule_Template__c.Id}','_self');// please exact format

 

code for check with contact

 

controller :

public with sharing class checkboxcls {
    public PageReference selected() {
    System.debug('------------------------>lstwrap is'+lstwrap);
    for(wrappecls w:lstwrap){
        if(w.check==true){
            System.debug('---------->'+w.con);
            System.debug('-----+++++++++----->'+w.con.name);

           // you can use apexpage .cuurent.getparameter(id) (*check syntax ) to get opp id that pospect is assign to opp
           // here you can add your logic of creating prospect and  use contact id for assign to contact

            // create prospect object instance add to list of prospect object


                                   }
                                         }  // insert prospect list
        return null;
                                                      }
    public class wrappecls{
        public boolean check{get; set;}
        public Contact con{get; set;}
     
        public wrappecls(Contact c){
            con =c;
            check=false;
                                                    }
     
                                      }
 
    LIst<wrappecls> lstwrap= new List<wrappecls>();
 
    public List<wrappecls> getitems(){
    List<Contact> lst=[Select id, name, phone from Contact];
    for(Contact c:lst){
    lstwrap.add(new wrappecls(c));
                              }
    System.debug('------------------------>lstwrap is'+lstwrap);
    return lstwrap;
                                                       }
                                                         }

 

page:

 

<apex:page controller="checkboxcls">
<apex:form >
    <apex:PageBlock >
        <apex:pageBlockTable value="{!items}" var="i">
        <apex:column >
            <apex:inputCheckbox value="{!i.check}"/>
        </apex:Column>
                <apex:column value="{!i.con.name}"/>
                <apex:column value="{!i.con.phone}"/>
        </apex:pageBlockTable>
        <apex:commandButton value="Selected" action="{!selected}"/>
    </apex:PageBlock>
</apex:form>
</apex:page>

 

 

 

This was selected as the best answer
MattyRayMattyRay

Thanks for the help.  I think this will do what I need. 

Peter HerzogPeter Herzog
Matty,

Did you get this to work in your org? If so, would you mind sharing your code? I am doing something similar but simpler. 

I want to create a related record of my custom object for every contact I select in a list.  

Thanks!
Peter