• JennyK5
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 6
    Replies
Hi,

I built a custom clone controller but would like to find out if it is possible to  insert the url for the old record into the new record during cloning?
Hi I built a custom controller on a Opportunity object. Everything is working fine, except when I cancel the cloning operation by clickign the Cancel Button it still creates a clone opportunity. Anyone has idea as to how to fix it?
I am building a clone opportunity controller which is cloning opportunity, bid__c and and BidAccount__c  a parent, child and grandchild relationship. everything in the code is working fine except the grandchild records BidAccount are getting related to the old Child record ( bid)  I am not sure what I am doing wrong.
Please see the code sample below:
//This class is used to clone the Existing Opportunity and related Bids.

public class CloneOpportunityRebidController
{
   
   

    //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 extOpportunity {get;set;}
  
    // set the id of the record that is created
    public ID newRecordId {get;set;}

    // initialize the controller
    public CloneOpportunityRebidController(ApexPages.StandardController controller)
    {
        //initialize the stanrdard controller
        this.controller = controller;
        // load the current record
        extOpportunity = (Opportunity)controller.getRecord();
        
    }

    // method called from the VF's action attribute to clone the Case
    public PageReference cloneOpportunity()
    {
        // setup the save point for rollback
        Savepoint sp = Database.setSavepoint();
        Opportunity newOpportunity;
        List <Bid__c> extBid;
        List <BidAccount__c>extOpportunityBidAccts;
       
        try {
   // Fetch source Opportunity fields to populate(clone)for target Opportunity           
            extOpportunity = [
                select Id,Name,LeadRating__c,OpportunityClassification__c,LeadSource
                    
                from Opportunity
                where id =: extOpportunity.id];
                 
             
            if(extOpportunity != null)
            {
                newOpportunity = extOpportunity.clone(false);
                insert newOpportunity;
                // set the id of the new Opportunity created for testing
                newRecordId = newOpportunity.id;
                   
                //newOpportunity is a copy of the opportunity with bids not copied over and extOpportunity is old opportunity
                // from which we want to copy over the bids
                //Get list of Bids from the old opportunity to populate into the new opportunity
                //extBid= the bid data from collection of Bids where the bids opportunity is equal to
                //existing opportunity

                extBid =  [
                    select ID, Opportunity__c
                    from Bid__c
                    where Opportunity__c =:extOpportunity.id];
               
         
                List<Bid__c> insertextBid = new List <Bid__c>();
               
                //extBid = the bid data from collection of bids where the bid's opportununity equals the existing opportunity
               
               
                if(extBid != null){  // if existing opportunity returned a collection of Bids
                    for(Bid__c extOpportunityBid :extBid) // Every Bid in a collection of Bid is called extOpportunityBid
                    {
                      
                      Bid__c newextOpportunityBid = extOpportunityBid.clone(False); //Made a copy of the Bid
                        newextOpportunityBid.Opportunity__c = newRecordId; 
                      
                        extOpportunityBidAccts = [
                    SELECT Bid__r.Id,Account__c,Bid__c, Bid__r.Name, Bid__r.Opportunity__c,Name
                            from BidAccount__c
                            where Bid__r.Opportunity__c =:extOpportunity.id];
                        List<BidAccount__c>insertBidAccount=new list<BidAccount__c>();
                        if(extOpportunityBidAccts != null) { 
                            for(BidAccount__c extOpportunityBidAccount :extOpportunityBidAccts) // Every Account in a collection of Bid is called extOpportunityBidAccount
                            {
                                
                                BidAccount__c newextOpportunityBidAccount = extOpportunityBidAccount.clone(False); //Made a copy of the BidAccount
                               newextOpportunityBidAccount.Bid__r.Opportunity__c= newRecordId;  
                               
                                InsertBidAccount.add(newextOpportunityBidAccount);
                                 
                            }
                            insert InsertBidAccount;
                           
                       }
                        InsertextBid.add(newextOpportunityBid);
                    }
                    insert InsertextBid;
                }
                //// Insert BidAccounts for every Bid.
    
            }
        } catch (Exception e){
            // roll everything back in case of errors
           
            Database.rollback(sp);
           
            ApexPages.addMessages(e);
           
            return null;
         }

        return new PageReference('/'+newOpportunity.id+'/e?retURL=%2F'+newOpportunity.id);
    }

}

Hi I built a custom controller on a Opportunity object. Everything is working fine, except when I cancel the cloning operation by clickign the Cancel Button it still creates a clone opportunity. Anyone has idea as to how to fix it?
I am building a clone opportunity controller which is cloning opportunity, bid__c and and BidAccount__c  a parent, child and grandchild relationship. everything in the code is working fine except the grandchild records BidAccount are getting related to the old Child record ( bid)  I am not sure what I am doing wrong.
Please see the code sample below:
//This class is used to clone the Existing Opportunity and related Bids.

public class CloneOpportunityRebidController
{
   
   

    //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 extOpportunity {get;set;}
  
    // set the id of the record that is created
    public ID newRecordId {get;set;}

    // initialize the controller
    public CloneOpportunityRebidController(ApexPages.StandardController controller)
    {
        //initialize the stanrdard controller
        this.controller = controller;
        // load the current record
        extOpportunity = (Opportunity)controller.getRecord();
        
    }

    // method called from the VF's action attribute to clone the Case
    public PageReference cloneOpportunity()
    {
        // setup the save point for rollback
        Savepoint sp = Database.setSavepoint();
        Opportunity newOpportunity;
        List <Bid__c> extBid;
        List <BidAccount__c>extOpportunityBidAccts;
       
        try {
   // Fetch source Opportunity fields to populate(clone)for target Opportunity           
            extOpportunity = [
                select Id,Name,LeadRating__c,OpportunityClassification__c,LeadSource
                    
                from Opportunity
                where id =: extOpportunity.id];
                 
             
            if(extOpportunity != null)
            {
                newOpportunity = extOpportunity.clone(false);
                insert newOpportunity;
                // set the id of the new Opportunity created for testing
                newRecordId = newOpportunity.id;
                   
                //newOpportunity is a copy of the opportunity with bids not copied over and extOpportunity is old opportunity
                // from which we want to copy over the bids
                //Get list of Bids from the old opportunity to populate into the new opportunity
                //extBid= the bid data from collection of Bids where the bids opportunity is equal to
                //existing opportunity

                extBid =  [
                    select ID, Opportunity__c
                    from Bid__c
                    where Opportunity__c =:extOpportunity.id];
               
         
                List<Bid__c> insertextBid = new List <Bid__c>();
               
                //extBid = the bid data from collection of bids where the bid's opportununity equals the existing opportunity
               
               
                if(extBid != null){  // if existing opportunity returned a collection of Bids
                    for(Bid__c extOpportunityBid :extBid) // Every Bid in a collection of Bid is called extOpportunityBid
                    {
                      
                      Bid__c newextOpportunityBid = extOpportunityBid.clone(False); //Made a copy of the Bid
                        newextOpportunityBid.Opportunity__c = newRecordId; 
                      
                        extOpportunityBidAccts = [
                    SELECT Bid__r.Id,Account__c,Bid__c, Bid__r.Name, Bid__r.Opportunity__c,Name
                            from BidAccount__c
                            where Bid__r.Opportunity__c =:extOpportunity.id];
                        List<BidAccount__c>insertBidAccount=new list<BidAccount__c>();
                        if(extOpportunityBidAccts != null) { 
                            for(BidAccount__c extOpportunityBidAccount :extOpportunityBidAccts) // Every Account in a collection of Bid is called extOpportunityBidAccount
                            {
                                
                                BidAccount__c newextOpportunityBidAccount = extOpportunityBidAccount.clone(False); //Made a copy of the BidAccount
                               newextOpportunityBidAccount.Bid__r.Opportunity__c= newRecordId;  
                               
                                InsertBidAccount.add(newextOpportunityBidAccount);
                                 
                            }
                            insert InsertBidAccount;
                           
                       }
                        InsertextBid.add(newextOpportunityBid);
                    }
                    insert InsertextBid;
                }
                //// Insert BidAccounts for every Bid.
    
            }
        } catch (Exception e){
            // roll everything back in case of errors
           
            Database.rollback(sp);
           
            ApexPages.addMessages(e);
           
            return null;
         }

        return new PageReference('/'+newOpportunity.id+'/e?retURL=%2F'+newOpportunity.id);
    }

}

Hi All,

DId not find a similar question in the past so here it goes:
We have created a custom clone functionality. What this does is clone the record, empty some fields, update the parents, and then inserts the record in the database. Afterwards the page is redirected to the edit page of the record.

However, the standard Salesforce Edit detail record page has a cancel button. This cancel button just directs to the ?retURL paramters. So it will go back the to detail view. 

Because we performed the clone and we needed to insert this cloned record into the DB before redirecting to the Edit page, the record will not be 'cancelled' in the way of cancellation the entire clone. So the record must be deleted again.

Is there a way to capture the Cancel event? Or to redirect the page to the edit detail view of the record without inserting it into the DB? So then the user can still Click on Save and we have the requisted functionality. 

Thanks,
Robin 
  • September 11, 2014
  • Like
  • 1