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

Clone with RecordType

The business' needs for their custom opprotunity cloning has changed and they need the cloned opportunity to be cloned to edit mode, with the correct Record Type based upon the opportunity name.  I am struggling on how to do this.    This is my code, but it generates errors on the if statement.  Should I be doing this in a different way?  Also, is there a conditional operator "like" that I can use for the opportunity name is like or the opportunity name contains?  Should I be doing 2 select statements?  Thanks for any suggestions.



public class OppCloneController {  

    private ApexPages.StandardController controller {get; set;}  

    public Opportunity opp {get;set;} 
    public List<OpportunityLineItem> items = new List<OpportunityLineItem>();

    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS  

    public ID newRecordId {get;set;}  

     // initialize the controller  

    public OppCloneController(ApexPages.StandardController controller) {  

     //initialize the stanrdard controller  

      this.controller = controller;  

       // load the current record  

         opp = (Opportunity)controller.getRecord();  

    // method called from the VF's action attribute to clone the opp 

    public PageReference cloneWithItems() {  

          // setup the save point for rollback  

        Savepoint sp = Database.setSavepoint();  

        Opportunity newOP;  

         try {  

          //copy the opportunity - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE  

             opp = [select Id, Name, Account.Name, AccountID, StageName, CloseDate, OwnerID, Type_of_Job__c, 
             Invoice_Requirements__c, Customer_Account__c, Contact_Name__c, Customer_E_mail__c, RecordTypeID,
             Phone__c, fax__c, Pricebook2Id
             from Opportunity where id =]; 
             newOP = opp.clone(false);  
           //check to see if the opportunity name contains DTV or DirecTV, if so apply the appropriate record type,
           //otherwise use standard record type
             if (opp.Name = 'DirecTv' || 'DTV'){
             	newOpp.RecordTypeID = 'DTV New Standard';
                }else {
             	newOpp.RecordType = opp.RecordTypeId;




You need to specify an actual Record Type Id instead of the Record Type name.  You should be able to do something like this:



newOpp.RecordTypeId = [Select Id
                       From RecordType r 
                       Where r.Name='DTV New Standard' limit 1].Id;



For like in apex, check out the String methods (  You could use contains.


Thanks, I'll give that a try.