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 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;
    }
}

    
        
        
            
                    
                    
            
            
                
                
            
        
    

Custom Button

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

Comments are closed.