• Helena Alwyn 1
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
I want to update a date field to the perivous record in the child object whenever a new child record is created for account. How can I do it?
 
I want to update a date field to the perivous record in the child object whenever a new child record is created for account. How can I do it?
 
public class cloneM {
    
    private final Opportunity o;    
    public CloneM (ApexPages.StandardController 
                       stdController) {
                           this.o = (Opportunity)stdController.getrecord();
                       }
    
    Public opportunity newOpp{get; set;}
    
        
    public void cloneOpp(){
        
        list<es__c> esList = [select id from es__c where opportunity_name__c =: o.id];

        
        String soql = cloneUtil.getCreatableFieldsSOQL('opportunity','id=\''+o.id+'\'');
        
        opportunity opp = (opportunity)Database.query(soql);
        opportunity newOpp = opp.clone(false, true);
        newOpp.stageName = 'Not Worked';
        insert newOpp;
        
        if(esList.size() >0){             
                        
            for(es__c es : esList){
                String soql2 = cloneUtil.getCreatableFieldsSOQL('es__c','id=\''+ es.id + '\'');
                es__c oldEs = (es_mpan__c)Database.query(soql2);
                es__c newEs = oldEs.clone(false,false);
                insert newEs;
            }	
        }
    }
}

Here is my class, I am trying to create a custom button to clone a parent record and all child records associated to it. The issue is I hit a govenor limit on the when looping through the esList as I am querying all the fields for each record. I knew this would happen but I am sruggling to solve the issue around looping through a list to retrieve all fields for each record prior to insert?
 
if(esList.size() >0){             
                        
            for(es__c es : esList){
                String soql2 = cloneUtil.getCreatableFieldsSOQL('es__c','id=\''+ es.id + '\'');
                es__c oldEs = (es__c)Database.query(soql2);
                es__c newEs = oldEs.clone(false,false);
                insert newEs;
            }	
        }

This is my issue, I am calling a utility to dynamically pull all field information for each record:

Having this is the loop is the offending line..
es__c oldEs = (es__c)Database.query(soql2);
Does anyone have any ideas on how I can resolve this without having everyfield in the class? 

Thanks in advance.