Clone with Related List Example

/*
This class is a custom profile cloning process that include duplicating orig profile record and related lists: CHEMICAL CONSTITUENTS, RCRA/STATE CODES & DOT CODES
author - ben.c  
Create Date 10-27-2015-
*/

global class CloneProfile {
 
 WebService static Id createNewProfile(String pId){
 
 String SobjectApiName = 'Profile__c';
 Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
 Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
 
 String commaSepratedFields = '';
 for(String fieldName : fieldMap.keyset()){
 if(commaSepratedFields == null || commaSepratedFields == ''){
 commaSepratedFields = fieldName;
 }else{
 commaSepratedFields = commaSepratedFields + ', ' + fieldName;
 }
 }
 
 
 String query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName + ' where Id=\'' + pId + '\'';
 
 // Get profile record to be clone
 Profile__c pSRCList = Database.query(query);
 
 Profile__c cSRC = pSRCList.clone(false, true, false, false);
 
 // Set default values
 cSRC.Name='Please Update - ' + generateRandomString(20);
 cSRC.Queue__c='SALES';
 cSRC.Ownerid = UserInfo.getUserId();
 cSRC.NAICS_Applicability__c='';
 cSRC.NAICS_Codes__c='';
 cSRC.Accept_wastes__c='';
 cSRC.Coke_oven_by_product_recovery__c='';
 cSRC.High_TAB__c='';
 cSRC.TAB_Concentration__c='';
 cSRC.TAB_year__c='';
 cSRC.TAB_determination_method__c='';
 cSRC.NESHAP_Certification__c='';
 
 cSRC.Method_of_Shipment__c='';
 cSRC.Container_Type__c='';
 cSRC.Container_Size_Drum_Container__c='';
 cSRC.Container_UOM__c='';
 cSRC.Est_Volume_per_Shipment__c=null;
 cSRC.Projected_Volume_UOM__c='';
 cSRC.Est_Shipping_Frequency__c='';
 cSRC.Special_Handling_Instructions__c='';
 
 cSRC.Customer_Approved_Facilities_List_AFL__c='';
 cSRC.Customer_Disposal_Restrictions__c='';
 cSRC.Generator_Disposal_Restrictions__c='';
 cSRC.Generator_Approved_Facilities_List_AFL__c='';
 cSRC.Profile_Specific_Disposal_Instructions__c='';
 
 cSRC.Primary_TSDF_Facility_Requested__c=null;
 cSRC.Requested_Bill_Code__c=null;
 cSRC.Submission_Priority__c='';
 cSRC.Priority_Reason__c='';
 cSRC.Additional_Requested_TSDFs__c='';
 cSRC.Additional_Submission_Comments__c='';
 
 cSRC.ExpirationDate__c=null;
 cSRC.Last_Pickup_Date__c=null;
 cSRC.Multiple_Generator_Profile_MGP__c='N';
 cSRC.MSDS_Provided__c=null;
 cSRC.Waste_from_ER_Response_Remediation__c='No';
 cSRC.Analysis_Provided__c=null;
 cSRC.CESQG_Profile_Status__c=null;
 cSRC.CERCLA_Regulated_Waste__c=null;
 
 // Clone profile record
 insert cSRC;
 system.debug('New Profile ID:' + cSRC.Id);
 
 // Clone related list: CHEMICAL CONSTITUENTS
 createNewChemicalConstituents(pId,cSRC.Id);
 
 //Clone related list: DOT CODES
 createNewDotCodes(pId,cSRC.Id);
 
 //Clone related list: RCRA/STATE CODES
 createNewStateCodes(pId,cSRC.Id);
 
 //Clone related list: Approved Destinations
 // createNewApprovedDestinations(pId,cSRC.Id);
 
 return cSRC.Id;
 } 
/*
Clone related list: CHEMICAL CONSTITUENTS
*/ 
 public static void createNewChemicalConstituents(String pId,String cId){
 
 List<Chemical_Constituent__c> ccList = [Select Name,Qualifier__c,Waste_Profile__c,
 Chemical_Constituent__c,Minimum__c,
 Maximum__c,UOM__c,Manifest_NOS__c,
 Preview_Notes__c FROM Chemical_Constituent__c 
 Where Waste_Profile__c=:pId]; 
 
 Chemical_Constituent__c[] cloneRs = ccList.clone();
 for (Chemical_Constituent__c chem:cloneRS)
 {
 chem.Id=null;
 chem.Waste_Profile__c=cId;
 }
 insert cloneRs; 
 
 } 
/*
Clone related list: DOT CODES
*/ 
 public static void createNewDotCodes(String pId,String cId){
 
 List<Waste_Profile_DOT_Code__c> dcList = [Select Name,Default_DOT__c,DOT_N_O_S_Description__c,DOT_Name_Additon__c,
 Marine_Pollutant__c,RQ__c,RQ_Detail__c,Shipment_by_Sea_Flash_Point_C__c,
 Special_Handling_Instructions__c,DOT_Code__c, Waste_Profile__c 
 FROM Waste_Profile_DOT_Code__c Where Waste_Profile__c=:pId]; 
 
 Waste_Profile_DOT_Code__c[] cloneRs = dcList.clone();
 for (Waste_Profile_DOT_Code__c dc:cloneRS)
 {
 dc.Id=null;
 dc.Waste_Profile__c=cId;
 }
 insert cloneRs; 
 
 } 
/*
Clone related list: RCRA/STATE CODES
*/ 
 public static void createNewStateCodes(String pId,String cId){
 
 List<Waste_Profile_Code__c> scList = [Select Name, Waste_Profile__c,Code__c,Description__c,
 Print_Order__c, Print_on_Manifest__c,Sequence__c,State__c,
 Texas_RCRA_Code__c,Waste_Code__c, Waste_Profile_Code_ExtId__c,
 RecordTypeId
 FROM Waste_Profile_Code__c
 Where Waste_Profile__c=:pId]; 
 
 Waste_Profile_Code__c[] cloneRs = scList.clone();
 for (Waste_Profile_Code__c sc:cloneRS)
 {
 sc.Id=null;
 sc.Waste_Profile__c=cId;
 }
 insert cloneRs; 
 
 } 
/*
Clone related list: Approved Destinations
*/ 
/*
 public static void createNewApprovedDestinations(String pId,String cId){
 
 List<Approved_Destination__c> adList = [Select Name,Account__c,Key__c,Location__c,Operational_Comments_Notes__c,
 Process_Code__c,Stericycle_Location__c,Facility_Code__c,
 TSDF_Type__c,Waste_Profile__c 
 FROM Approved_Destination__c
 Where Waste_Profile__c=:pId]; 
 
 Approved_Destination__c[] cloneRs = adList.clone();
 for (Approved_Destination__c ad:cloneRS)
 {
 ad.Id=null;
 ad.Waste_Profile__c=cId;
 }
 insert cloneRs; 
 
 }
 */ 
 public static String generateRandomString(Integer len) {
 final String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
 String randStr = '';
 while (randStr.length() < len) {
 Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), chars.length());
 randStr += chars.substring(idx, idx+1);
 }
 return randStr; 
 } 
 
}




////

@isTest
private class TestCloneProfile 
{
 static testMethod void cloneProfile() { 
 
 Account acc = New Account(Name='Test', Type='Customer',Account_Status__c='Active', AccountExternalId__c='38410');
 insert acc;
 
 Profile__c prof = new Profile__c(Account__c = acc.Id, WasteDescription__c='Test',
 Name = 'Test Profile - 111111'); 
 insert prof;
 
 Chemical_Constituent__c chem = new Chemical_Constituent__c(Waste_Profile__c=prof.Id);
 insert chem;
 
 DOT_Code__c dCode = new DOT_Code__c(DOT_Identification_Number__c='TEST',
 Hazardous_Materials_Description__c='TEST',
 Hazardous_Class_or_Division__c='TEST',
 Packaging_Group__c='I',
 Inhalation_Zone__c='TEST',
 Inhalation_Hazard__c='N',
 ERG_Number__c='TEST');
 insert dCode;
 
 Waste_Profile_DOT_Code__c dc = new Waste_Profile_DOT_Code__c(Waste_Profile__c=prof.Id,DOT_Code__c=dCode.Id);
 insert dc;
 
 Waste_Profile_Code__c sc = new Waste_Profile_Code__c(Waste_Profile__c=prof.Id);
 insert sc;
 
 Profile__c p = [select Id from Profile__c limit 1]; 
 
 Test.startTest();
 
 CloneProfile.createNewProfile((String)p.Id); 
 
 Test.stopTest();
 
 }
}

























































Print Friendly, PDF & Email
This entry was posted in Development and tagged . Bookmark the permalink.