Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jenishshingala/c59934cea64f086e2afa4a2e6bae0967 to your computer and use it in GitHub Desktop.
Save jenishshingala/c59934cea64f086e2afa4a2e6bae0967 to your computer and use it in GitHub Desktop.
Clone With Opportunity Product
public with sharing class CloneOpportunityWithProducts{
@AuraEnabled
public static string cloneOpportunity(Id opportunityId) {
string query='select ';
string queryOLI='select ';
List<Opportunitylineitem> lstOLI = new List<Opportunitylineitem>();
List<Opportunitylineitem> lstOLIToInsert = new List<Opportunitylineitem>();
Schema.DescribeSObjectResult r = Opportunity.sObjectType.getDescribe();
List<String> apiNamesOpportunity = new list<String>();
for(string apiName : r.fields.getMap().keySet()){
apiNamesOpportunity.add(apiName);
}
for(string objstr:apiNamesOpportunity){
query+=objstr+',';
}
query=query.removeend(',');
query = query+' from opportunity where id=:opportunityid';
system.debug('QUERY-->'+query);
Opportunity op1 = Database.query(query);
Opportunity op2 = op1.clone(false);
op2.name = op1.name+' - Clone';
insert op2;
system.debug('ID-->'+op2);
Schema.DescribeSObjectResult roli = OpportunityLineItem.sObjectType.getDescribe();
List<String> apiNamesOLI = new list<String>();
for(string apiName : roli.fields.getMap().keySet()){
apiNamesOLI.add(apiName);
}
for(string objstr:apiNamesOLI){
if(objstr!='totalprice')
queryOLI+=objstr+',';
}
queryOLI=queryOLI.removeend(',');
queryOLI = queryOLI+' from OpportunityLineItem where opportunityid=:opportunityid';
lstOLI = Database.query(queryOLI);
if(lstOLI!=null && lstOLI.size()>0){
for(OpportunityLineItem objOLI:lstOLI){
opportunitylineitem objNewOLI = objOLI.clone(false);
objNewOLI.opportunityid = op2.id;
lstOLIToInsert.add(objNewOLI);
}
insert lstOLIToInsert;
}
return op2.id;
}
}
<aura:component controller="CloneOpportunityWithProducts" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<lightning:spinner variant="brand" size="medium" />
</aura:component>
({
doInit : function(component, event, helper) {
// Prepare the action to load case record
var action = component.get("c.cloneOpportunity");
action.setParams({"opportunityId": component.get("v.recordId")});
// Configure response handler
action.setCallback(this, function(response) {
var state = response.getState();
if(component.isValid() && state === "SUCCESS") {
// Prepare a toast UI message
var resultsToast = $A.get("e.force:showToast");
resultsToast.setParams({
"type": "success",
"message": "Opportunity has been successfully cloned with Opportunity Products."
});
// Update the UI: close panel, show toast, refresh case page
$A.get("e.force:closeQuickAction").fire();
resultsToast.fire();
var sobjectEvent=$A.get("e.force:navigateToSObject");
sobjectEvent.setParams({
"recordId": response.getReturnValue()
});
sobjectEvent.fire();
} else {
console.log('Problem getting Case, response state: ' + state);
}
});
$A.enqueueAction(action);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment