Clone a record Apex

******************************************
trigger clonProdNonTX on Contract_Addendum__c (before insert) {List<Id> list_prod_ids = new List<Id>();
for(Contract_Addendum__c adden : Trigger.New){
list_prod_ids.add(adden.Product_Request_ID__c);

if (adden.Type__c=='Price / Product Conversion'&&adden.Billing_State__c!='TX')
{

Product__c p = [select Id, IsDeleted, Name, RecordTypeId,
CreatedDate, CreatedById, LastModifiedDate,
LastModifiedById, SystemModstamp, Opportunity__c,
Account__c, Adder_per_M_Wh__c, Ancillary_Adder__c,
Base_Charge__c, Bena_Amount__c, Black_Start_Amount__c,
Block_1_Adder_Per_MWh__c, Block_1_Price_per_MWh__c, Block_1_Type__c,
Block_1_Volume__c, Block_2_Adder_Per_MWh__c, Block_2_Price_per_MWh__c,
Block_2_Type__c, Block_2_Volume__c, Block_3_Adder_Per_MWh__c,
Block_3_Price_per_MWh__c, Block_3_Type__c, Block_3_Volume__c,
Capacity_Passthrough__c, Charge__c, Customer_50_Kw__c,
Customer_Notification_Waiver__c, ERCOT_Ancillary_Passthrough__c,
Emergency_Load_Mis_Debit__c, Ercot_Administration_Team__c,
Fixed_Gas_Price__c, Flat_Or_Scheduled__c, Forcasted_Usage__c,
Gas_Index__c, Heat_Rate__c, Holiday_Type__c, Line_Loss__c,
Load_Factor__c, Load_Fixed__c, Local_Balancing_Energy_Service_Charge__c,
Lower_Bandwidth__c, MCPE_Index__c, Management_Fee_Amount__c, Monthly_k_Wh__c,
NITS_Passthrough__c, Non_Spin_Bill_Amount__c, Off_Peak__c, On_Peak__c,
Out_of_Market_Energy_Charge__c, Out_of_Market_Replacement_Capacity__c,
PK_Days_High__c, PK_Days_Low__c, PK_Hours_High__c, PK_Hours_Low__c,
PPL_TD_Loss_Lookup__c, Percentage_Product_1__c, Percentage_Product_2__c,
Percentage_Product_3__c, Percentage_Product_4__c, Product_1__c,
Product_2__c, Product_3__c, Product_4__c, Product_Risk_Category__c,
RECs__c, RMR_Passthrough__c, RMR_Reserve_Service__c, Real_Time_or_Day_Ahead__c,
Rebate__c, Regulation_Down_Service__c, Regulation_Up_Service__c,
Replacement_Reserve_Service__c, Replacement_Reserve_Uplift__c, Retail_Adder__c,
Service_Type__c, Special_Billing__c, T_D_Line_Loss__c, Term_Usage__c, Term__c,
Transmission_Distribution_Losses__c, Upper_Bandwidth__c, kW__c, Product_Type__c,
Additional_RECs_1_Percentage__c, Additional_RECs_1_State__c, Additional_RECs_2_Percentage__c,
Additional_RECs_2_State__c, Additional_RECs_3_Percentage__c, Additional_RECs_3_State__c,
Base_Point_Deviation_Amount__c, Charge_for_Emerg_Power_Increase_Amount__c,
Hub_to_Zone_Basis__c, Pricing_Type__c, Real_Time_Rev_Neutrality_Alloc_Amount__c,
Sapphire_Indicator__c, UFE_Line_Loss__c, MTM_Indicator__c, POLR_Indicator__c,
Legacy_CMS_Id__c, Time_Stamp_for_product__c, ARR_Pass_Through_Charge_Credit__c,
Ancillary_Services_Pass_Through_Charge__c, ISO_Pass_Through_Charge__c, Marginal_Loss_Credit__c,
Operating_Reserves_Pass_Through_Charge__c, Percentage_Locked_1__c, Percentage_Locked_2__c,
Percentage_Locked_3__c, RECs_Pass_Through_Charge__c,
TEC_Credit_or_Charge__c from Product__c where id = :list_prod_ids limit 1][0];

Product__c p2 = p.clone(false, true);
system.debug('New Product/Conversion ID:' + p2.Id);
insert p2;
adden.Product_Request_ID__c = p2.Id;
}

}
}
********************************

http://sfdc.arrowpointe.com/2011/03/28/cloning-records-in-apex/ — orig. post!
/* query lead and then clone it */
String soql = Utils.getCreatableFieldsSOQL('lead','id=\'00Q3000000aKwVN\'');
lead l = (Lead)Database.query(soql);
lead l2 = l.clone(false, true);
insert l2;
***************************************************
public with sharing class Utils{
    // Returns a dynamic SOQL statement for the whole object, includes only creatable fields since we will be inserting a cloned result of this query
    public static string getCreatableFieldsSOQL(String objectName, String whereClause){
        
        String selects = '';
        
        if (whereClause == null || whereClause == ''){ return null; }
        
        // Get a map of field name and field token
        Map<String, Schema.SObjectField> fMap = Schema.getGlobalDescribe().get(objectName.toLowerCase()).getDescribe().Fields.getMap();
        list<string> selectFields = new list<string>();
        
        if (fMap != null){
            for (Schema.SObjectField ft : fMap.values()){ // loop through all field tokens (ft)
                Schema.DescribeFieldResult fd = ft.getDescribe(); // describe each field (fd)
                if (fd.isCreateable()){ // field is creatable
                    selectFields.add(fd.getName());
                }
            }
        }
        
        if (!selectFields.isEmpty()){
            for (string s:selectFields){
                selects += s + ',';
            }
            if (selects.endsWith(',')){selects = selects.substring(0,selects.lastIndexOf(','));}
            
        }
        
        return 'SELECT ' + selects + ' FROM ' + objectName + ' WHERE ' + whereClause;
        
    }
}
************************
Print Friendly, PDF & Email
This entry was posted in Development. Bookmark the permalink.