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
miiWorksmiiWorks 

Set Static Field Value on Custom Field in Apex Class On New Record Save

Hello. I am hoping this is something simple. I have written a class to do a 'deep cloe' and copy child record. On my parent (Presentation__c) I am wanting to override the clone value of the "miiEventV1__Status__c" field with the value of 'Planned'. I have validation which is conflicting, so each cloned/new record, should have this field set to the static value. The issue is that on my clone, it will usually have another value, thus the reason to statically set the field value.

 

Here is my Controller, with my commented out attempt prior to the insert of the new record. Can anyone offer a suggestion how to set a fixed value at creation of my new record


Thanks

 

public class PresentationWithTicketTypeController {
 
    //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 miiEventV1__Presentation__c pres {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 PresentationWithTicketTypeController(ApexPages.StandardController controller) {
 
        //initialize the stanrdard controller
        this.controller = controller;
        // load the current record
        pres = (miiEventV1__Presentation__c)controller.getRecord();
 
    }
 
    // method called from the VF's action attribute to clone the pres
    public PageReference cloneWithItems() {
 
         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         miiEventV1__Presentation__c newPRES;
 
         try {
 
              //copy the project plan - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             pres = [Select Id, RecordTypeId, miiEventV1__Status__c, miiEventV1__Presentation_Type__c, miiEventV1__Active__c, miiEventV1__Actual_Duraction_Mins__c, miiEventV1__Booking_Link__c, miiEventV1__Breaks_and_Refreshments__c, miiEventV1__Date_Publish_Media__c, miiEventV1__Date_of_Presentation__c, miiEventV1__Display_Name__c, miiEventV1__GoToWebinar_Prefix__c, miiEventV1__Long_Description__c, miiEventV1__Media_Description__c, miiEventV1__Media_Duration_MM_SS__c, miiEventV1__Media_URL__c, miiEventV1__Presentation_Times__c, miiEventV1__Presentation_Type_Display_Name__c, miiEventV1__Short_Description__c, miiEventV1__Tags__c, miiEventV1__Time_Zone__c, miiEventV1__Venue_Room__c, miiEventV1__Venue__c, miiEventV1__Video_Domain__c, miiEventV1__Video_Height__c, miiEventV1__Video_Type__c, miiEventV1__Video_Width__c, miiEventV1__Webinar_ID__c, miiEventV1__Webinar_Maximum_Registrations__c, miiEventV1__What_You_Will_Learn__c, Media_Channel_ID__c, Amazon_Bucket__c, Amazon_File_Name__c, Media_Centre_Program_Name__c, YouTube_Video_ID__c, YouTube_Display_in_HD__c, Start_Video_at__c, Record_Type_Custom__c, Viewing_Security__c FROM miiEventV1__Presentation__c where id = :pres.id];
             newPRES = pres.clone(false);
             
//pres.miiEventV1__Status__c = 'Planned'; insert newPRES; // set the id of the new pres created for testing newRecordId = newPRES.id; // copy over the child record - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE List<miiEventV1__Presentation_Ticket_Type__c> tickettypes = new List<miiEventV1__Presentation_Ticket_Type__c>(); for (miiEventV1__Presentation_Ticket_Type__c ptt : [Select Id, Name, miiEventV1__Presentation__c, miiEventV1__Active__c, miiEventV1__Amount_Per_Ticket__c, miiEventV1__Maximum_Tickets_Available__c, miiEventV1__Ticket_Type_Internal_Name__c, miiEventV1__Ticket_Type_Website_Name__c, miiEventV1__Ticket_Type__c, miiEventV1__Total_Expected_Revenue__c, Product_For_Sale__c, Ticket_Image_URL__c, Display_Groups__c FROM miiEventV1__Presentation_Ticket_Type__c where miiEventV1__Presentation__c = :pres.id]) { miiEventV1__Presentation_Ticket_Type__c newPTT = ptt.clone(false); newPTT.miiEventV1__Presentation__c = newPRES.id; tickettypes.add(newPTT); } // copy over the child record - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE List<miiEventV1__Speaker2Presentation__c> speakers = new List<miiEventV1__Speaker2Presentation__c>(); for (miiEventV1__Speaker2Presentation__c psp : [Select miiEventV1__Presentation__c, miiEventV1__Speaker__c, miiEventV1__Active__c FROM miiEventV1__Speaker2Presentation__c where miiEventV1__Presentation__c = :pres.id]) { miiEventV1__Speaker2Presentation__c newPSP = psp.clone(false); newPSP.miiEventV1__Presentation__c = newPRES.id; speakers.add(newPSP); } insert tickettypes; insert speakers; } catch (Exception e){ // roll everything back in case of error Database.rollback(sp); ApexPages.addMessages(e); return null; } return new PageReference('/'+newPRES.id); } public static testmethod void Test1() { miiEventV1__Presentation__c p=new miiEventV1__Presentation__c(); ApexPages.StandardController sc = new ApexPages.standardController(p); PresentationWithTicketTypeController pres=new PresentationWithTicketTypeController (sc); pres.cloneWithItems(); } }

 


Best Answer chosen by Admin (Salesforce Developers) 
miiWorksmiiWorks

I managed to achieve this

 

 //copy the project plan - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             pres = [Select Id, RecordTypeId, miiEventV1__Status__c, miiEventV1__Presentation_Type__c, miiEventV1__Active__c, miiEventV1__Actual_Duraction_Mins__c, miiEventV1__Booking_Link__c, miiEventV1__Breaks_and_Refreshments__c, miiEventV1__Date_Publish_Media__c, miiEventV1__Date_of_Presentation__c, miiEventV1__Display_Name__c, miiEventV1__GoToWebinar_Prefix__c, miiEventV1__Long_Description__c, miiEventV1__Media_Description__c, miiEventV1__Media_Duration_MM_SS__c, miiEventV1__Media_URL__c, miiEventV1__Presentation_Times__c, miiEventV1__Presentation_Type_Display_Name__c, miiEventV1__Short_Description__c, miiEventV1__Tags__c, miiEventV1__Time_Zone__c, miiEventV1__Venue_Room__c, miiEventV1__Venue__c, miiEventV1__Video_Domain__c, miiEventV1__Video_Height__c, miiEventV1__Video_Type__c, miiEventV1__Video_Width__c, miiEventV1__Webinar_ID__c, miiEventV1__Webinar_Maximum_Registrations__c, miiEventV1__What_You_Will_Learn__c, Media_Channel_ID__c, Amazon_Bucket__c, Amazon_File_Name__c, Media_Centre_Program_Name__c, YouTube_Video_ID__c, YouTube_Display_in_HD__c, Start_Video_at__c, Record_Type_Custom__c, Viewing_Security__c FROM miiEventV1__Presentation__c where id = :pres.id];
             newPRES = pres.clone(false);
             newPRES.miiEventV1__Status__c = 'Planned';
             newPRES.miiEventV1__Active__c = true;             
             newPRES.miiEventV1__Video_Type__c = '';     
             newPRES.miiEventV1__Media_Description__c = 'TBA'; 
             newPRES.miiEventV1__Media_Duration_MM_SS__c = 00.00; 
             newPRES.miiEventV1__Media_URL__c = ''; 
             newPRES.miiEventV1__Tags__c = ''; 
             newPRES.Start_Video_at__c = NULL;
             newPRES.miiEventV1__Date_Publish_Media__c = NULL;
             newPRES.Amazon_File_Name__c = ''; 
             newPRES.YouTube_Video_ID__c = '';         
           
             insert newPRES;