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
LaurenP6777LaurenP6777 

Using FieldSet in Deep Clone Controller

Good Afternoon, 

I have been using the Controller below as a deep clone on the Opportunity object. I would like to use FieldSets to determine which fields on the Opportunity Object should be included in the deep clone. This way, any administrator can update the FieldSet, hence updating the controller. Unfortunately, i can't see to grasp the concept and am having trouble replacing the list of fields with my fieldset.

Can someone show me how to replace the query under "//COPY THE OPPORTUNITY - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE" with a query that uses the FieldSet :"ECD_WSC_Field_Set"?

Thank you!
 
public class AddWSCECDController {
 
    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
     // add the instance for the variables being passed by id on the url
    private Opportunity o {get;set;}
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}
 
    // initialize the controller
    public  AddWSCECDController(ApexPages.StandardController controller) {
 
        //initialize the stanrdard controller
        this.controller = controller;
        // load the current record
        o = (Opportunity)controller.getRecord();
 
    }
 
    // method called from the VF's action attribute to clone the po
    public PageReference AddWSCECD() {
 
         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         Opportunity newO;
 
 
         try {
 
              //COPY THE OPPORTUNITY - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             
o = [select Id,
              name,    
              parent_opportunity__c,
              Parent_Opportunity__r.Name, 
              Parent_Opportunity__r.id,           
               Compound_Name__c,
              Compound__r.Id,
              StageName,
              LeadSource,
              Account.Name,
              AccountId,
              CurrencyISOCode,
              CloseDate,                        
              Original_Lead_Inquiry_Date__c,
              Protocol_Number__c,
              Protocol_Title__c,
              Therapeutic_Area__c,
              Therapeutic_Indication__c,
              Therapeutic_Indication_Other__c,
              High_Risk_Compound_GCP__c,
              General_Requests_Comments__c,
              Description,
              Route__c,
 Study_Phase__c,
              of_Covance_Sites__c,
Sites_per_country__c,
of_Subjects_Enrolled__c,
Special_Procedure_Requested__c,
If_Other_enter_here__c,
Assigned_Client_Manager__c,
Bill_Code__c,
Client_Flexible_on__c,
Site_GCP__c,
CMS_Number__c,
COMS_Number__c,
Contract_Analyst__c,
Covance_Study__c,
ERT_Collaboration_N__c,
ERT_Collaboration_Other__c,
ERT_Collaboration__c,
Est_CTA_Regulatory_Application_Date__c,
Estimated_LSLV__c,
Expected_Dosing_Treatment_Duration__c,
Financial_Check__c,
First_in_Human_Study__c,
FPI_Date__c,
GCP_Triage_notes__c,
Go_to_Bid_Defense_2nd_3rd_Round__c,
Government_Contract__c,
Government_Funding__c,
If_No_Why__c,
Please_Provide_Details_GCP_Non_Standard__c,
Please_Provide_Details_GCP_Bid_Defence__c,
RFP_RFI_Received__c,
Last_Subject_Visit__c,
Mode_of_Response_GCP__c,
MSA__c,
New_Oppty_Pull_Through_or_Rescue_Study__c,
Operational_questionnaire_required__c,
Patients_Healthy_Volunteers__c,
Potential_use_of_WOCBP_M3__c,
Proposed_Funding_by_Client__c,
Protocol_Synopsis_Available_Attached_GCP__c,
Pull_Through_Compound__c,
Push_Through_Compound__c,
Questionnaire_Completion_Date__c,
Region_Important_to_Client__c,
Required_Level_of_Costing_Detail__c,
RFP_RFI_Sent_to_Triage__c,
Screening_Start__c,
Services_Requested__c,
Special_Patient_Population__c,
Other_Special_Patient_Population__c,
Test_Material_Owner__c,
Trials_Ongoing_with_Client_in_CDS_Labs__c,
Trials_Ongoing_with_Client_in_CPS__c,
Customer_Status__c,
Operational_Site_Selected__c,
Differentiator_1_GCP__c,
Differentiator_2_GCP__c,
Differentiator_3_GCP__c,
Differentiator_4_GCP__c,
Differentiator_5_GCP__c,
Differentiator_6_GCP__c,
Differentiator_7_GCP__c,
Differentiator_8_GCP__c,
Differentiator_9_GCP__c,
ForecastCategoryName,
Therapeutic_Area_Group__c,
CMS_Parent_Oppty_Number__c,
Strategy_for_this_Project_Proposal__c,
Any_feedback_on_RBM_specifically__c,
Any_special_unusual_tasks_requested__c,
CLS_Proposal_Requirement__c,
Cost_Director__c,
Costing_Associate__c,
Countries__c,
Director__c,
ECD_Senior_Director__c,
Executive_Sponsor_if_required__c,
Expected_Study_Duration_FPI_to_LPO__c,
Feasibility_Information__c,
Finance_notes__c,
Inpatient_requirement__c,
Is_Japan_included_in_Country_Selection__c,
Legal_Rep_Required__c,
Level_of_Response__c,
MDG_Targeted_Opportunity__c,
Mechanism_of_action__c,
Opportunity_Type__c,
OS_P_Director__c,
Patients__c,
Physician__c,
Project_Region__c,
Proof_of_Concept__c,
Proposal_Associate__c,
Region__c,
RFP_RFI__c,
Scope_of_Services__c,
Study_Conduct_Location__c,
Test_Material_Owner__r.Id,
Time_Zone__c,
type_of_compound__c,
Was_RBM_proposed__c,
Was_RBM_requested__c from Opportunity where id = :o.id];
             
             newO = o.clone(false);
             
             Datetime DateMouse = Datetime.now();
             String ConvertedMouseDate = DateMouse.format('MM/dd/yy h:mm a');
             RecordType RecType = [Select Id From RecordType  Where SobjectType = 'Opportunity' and DeveloperName = 'ECD'];
             
             
             if(o.Parent_Opportunity__c==null){
             newo.Parent_Opportunity__c = o.Id;
             newo.WSC__c=true;
             newo.name=o.name+' WSC '+ConvertedMouseDate;
             newo.RecordTypeid=RecType.id;
             newo.NextStep='WSC';
             newo.Integrated_Opportunity2__c='No'; 
             newo.Stagename='Prospect';}
               
                     
             
             if(o.Parent_Opportunity__c!=null){
             newo.Parent_Opportunity__c = o.Parent_Opportunity__c;
             newo.RecordTypeid=RecType.id;
             newo.Stagename='Prospect';
             newo.WSC__c=true;
             newo.NextStep='WSC';  
             newo.Integrated_Opportunity2__c='No';     
             newo.name=o.Parent_Opportunity__r.Name+' WSC '+ConvertedMouseDate; }
           
              
           
             
             insert newO;
             
 
             // set the id of the new po created for testing
               newRecordId = newO.id;
               
                    // copy over oppty teams - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<OpportunityTeamMember> oppteam = new List<OpportunityTeamMember>();
             for (OpportunityTeamMember tr : [Select r.Id, r.UserId, r.OpportunityId,r.TeamMemberRole From OpportunityTeamMember r where User.IsActive=true AND OpportunityId = :o.id]) {
                  OpportunityTeamMember newtr = tr.clone(false);
                  newtr.OpportunityId = newO.id;                  
                  oppteam.add(newtr);

             }
             insert oppteam;

               
                   // copy over Operational Team - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<Operational_Team__c> team = new List<Operational_Team__c>();
             for (Operational_Team__c op : [Select p.Id, p.name, p.role__c, p.Lead_Site__c, p.Opportunity__c, p.Opportunity__r.Id From Operational_Team__c p where Opportunity__r.Id = :o.id]) {
                  Operational_Team__c newop =op.clone(false);
                  newop.Opportunity__c = newO.id;                  
                  team.add(newop);
             }  
               
 
             // copy over the line items - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<OpportunityContactRole> items = new List<OpportunityContactRole>();
             for (OpportunityContactRole cr : [Select r.Id, r.ContactId, r.OpportunityId, r.Isprimary, r.role From OpportunityContactRole r where OpportunityId = :o.id]) {
                  OpportunityContactRole newcr = cr.clone(false);
                  newcr.OpportunityId = newO.id;                  
                  items.add(newcr);

             }
             insert items;
             insert team;
            
             
         } catch (Exception e){
             // roll everything back in case of error
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
         }


        Opportunity ooppss = [SELECT shortid__c FROM opportunity WHERE id = :newO.id];
 
        return new PageReference('/'+newo.id+'/e?&saveURL='+newo.id+'&retURL='+o.id);
    }
 
}

 
BalajiRanganathanBalajiRanganathan
You have to use describe. see the link below on how to use field set describe

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_fieldsets_describe.htm