How To: Clone files and documents in Salesforce
January 30, 2020
Submit a Queueable to Clone Records
September 1, 2020

How To: Select Record when Copying to a Different Record Type

The copy page is typically used to copy related lists from one parent record to another record of the same object type. It also can copy related lists to another object type when the child object has lookup relationships to both from and to object types.

The built in prompt field expect the same object type, so a little custom code is needed to prompt for the other object type. The below example uses an object called Grantee_Report__c and Opportunity to illustrate how related records of the Grantee_Report__c can be coppied to the Opportunity.

Controller and Visualforce page

public with sharing class OpportunityPromptCopyController {
    public OpportunityLineItem oli {get; set;}
    public OpportunityPromptCopyController() {
        oli = new OpportunityLineItem();
    }

    public PageReference cancel() {
        return new PageReference('/' + ApexPages.currentPage().getParameters().get('fromid'));
    }
    public PageReference next() {
        if (String.isBlank(oli.OpportunityId)) {
            ApexPages.addMessage(new ApexPages.message(Apexpages.Severity.WARNING, 'Please select a Request'));
            return null;
        }
        PageReference pageRef = Page.lcrm_scp__ScpCopy;
        map<String, String> paramMap = ApexPages.currentPage().getParameters();
        pageRef.getParameters().put('cfg', paramMap.get('cfg'));
        pageRef.getParameters().put('fromid', paramMap.get('fromid'));
        pageRef.getParameters().put('retURL2', paramMap.get('fromid'));
        pageRef.getParameters().put('rid', oli.OpportunityId);
        return pageRef;
    }
}
<apex:page controller="OpportunityPromptCopyController" lightningStylesheets="true" tabStyle="Opportunity" >
<apex:form >
<apex:pageMessages id="errormsg" />
<apex:pageBlock title="Select an Opportunity" mode="maindetail">
<apex:pageBlockButtons id="buttons" location="top">
<apex:commandButton action="{!next}" value="Next" reRender="errormsg" />
<apex:commandButton action="{!cancel}" value="Cancel" immediate="true" />
</apex:pageBlockButtons>
<apex:pageBlockSection collapsible="false" columns="1" >
<apex:pageBlockSectionItem />
<apex:inputField value="{!oli.OpportunityId}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Custom Button

/apex/OpportunityPromptCopy?lss=1&cfg=copyToOpp&fromid={!Grantee_Report__c.Id}

Comments are closed.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
Read more