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
AmarjeetAmarjeet 

Error in cloning opoortunity using custom visualforce page and apex controller

User-added image

I am getting the above error. My requirement is to change the currency field data, if opportunity already having child quote records. So its not possible through standard . Thus I am working out for custom solution. Earlier its was working on clicking cancel it use to create clonned opportunity but now its not working now on create and on cancel both.

Apex:-


public with sharing class CloneOpportunityExtension {
    //to get opp id from url
    public String recId {get;set;}
    // for new currency and name
    public Opportunity cloneOpp {get;set;}
    
    public CloneOpportunityExtension(ApexPages.StandardController stdCon) {
        cloneOpp = new Opportunity();
        recId = null;
        recId = ApexPages.currentPage().getParameters().get('id'); 
        System.debug('recId'+recId);
        if(recId!=null) {
            // setting the currency on page load
            cloneOpp.CurrencyIsoCode = ((opportunity)stdCon.getRecord()).CurrencyIsoCode;
            // setting Name
            cloneOpp.Name = ((opportunity)stdCon.getRecord()).Name;
            

//setting Account Name
// cloneOpp.Account= ((opportunity)stdCon.getRecord()).Account;
        } else {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, ' '+Label.select_a_vaild_Opportunity));
        }
    }
   
    /**
    * to save the Clone Opportunity with New Currency
    * 
    */
    public PageReference saveNewOpp(){   
        PageReference pRef = null;
        if(recId!=null) {
            if(cloneOpp!=null) {
                try{
                    // calling clone class
                    String returnId = null;
                    returnId = Cls_CloneOptyRelated.CloneOpportunity(recId, cloneOpp.CurrencyIsoCode, cloneOpp.Name);
                    if(returnId!=null) {
                        pRef = new PageReference('/'+returnId);
                        pRef.setRedirect(true);
                    }
                    else {
                       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, ' '+Label.Issue_in_Cloning));
                    }
                }
                catch(Exception e) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, ' ' + Label.Issue_in_Cloning + ' ' + e.getMessage()));
                }
            }
        }
        return pRef;
    }
}


Visualforce Page:-

<apex:page standardController="Opportunity" extensions="CloneOpportunityExtension" tabStyle="Opportunity" title="{!$ObjectType.Opportunity.Label} {!$Label.Clone}">
<apex:sectionHeader subtitle="{!Opportunity.Name}" title="{!Opportunity.Name} {!$Label.Clone}" rendered="{!recId != null}"/>
<apex:form >
<apex:pageMessages >
</apex:pageMessages>
<apex:pageBlock title="{!$ObjectType.Opportunity.Label} {!$Label.Clone}">
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!saveNewOpp}" value="{!$Label.Clone}" rendered="{!recId != null}"/>
<apex:commandButton action="{!cancel}" value="{!$Label.Cancel}" rendered="{!recId != null}"/>
<apex:outputPanel rendered="{!recId == null}"> <input type="button" name="{!$Label.Back}" value="{!$Label.Back}" class="btn" onclick="window.top.location='/006/o';"/>
</apex:outputPanel>
</apex:pageBlockButtons>
<apex:pageBlockSection columns="1">
<apex:inputField value="{!cloneOpp.Name}" label="{!$ObjectType.Opportunity.fields.Name.Label}"/>
<apex:inputField value="{!cloneOpp.CurrencyIsoCode}" label="{!$ObjectType.Opportunity.fields.CurrencyIsoCode.Label}"/>
<!--- <apex:inputField value="{!cloneOpp.Account.Name}" label="{!$ObjectType.Opportunity.fields.Account.Name.Label}"/> ----> </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
AmarjeetAmarjeet
These is the error:-

Quote Currency: Can't change currency code. Enable quotes without parent opportunities.
Issue in cloning Opportunity, Please Contact System Administrator Update failed. First exception on row 0 with id 0Q05E0000009purSAA; first error: FIELD_INTEGRITY_EXCEPTION, Can't change currency code. Enable quotes without parent opportunities.: [CurrencyIsoCode]