You need to sign in to do that
Don't have an account?
Frances Allen
How to preseve the original record after cloning in Apex?
I want to clone a record through Apex using a trigger to customize staging and update date fields.
I've run into two problems:
1. My original record isn't saved. I want the original record to save and a copy to be made from it. I'm looking for an automatic cloning action essentially.
The copy is known by the addage "planning" onto the name of the Opportunity.
2. The trigger is acting on all updates, meaning I can't change and save a field without activating the trigger. Do
I just need to change the parameters? Is there a way to check and limit with Trigger.Old?
Trigger that conditionally stages.
Thanks.
I've run into two problems:
1. My original record isn't saved. I want the original record to save and a copy to be made from it. I'm looking for an automatic cloning action essentially.
The copy is known by the addage "planning" onto the name of the Opportunity.
2. The trigger is acting on all updates, meaning I can't change and save a field without activating the trigger. Do
I just need to change the parameters? Is there a way to check and limit with Trigger.Old?
Trigger that conditionally stages.
trigger FinancialPlanning on Opportunity (before update) { List<Opportunity> oppList = new List<Opportunity>(); //creating list of opps in which items will live for(Opportunity x : Trigger.new) { x.Name = x.Name + ' planning'; if (x.StageName == '0 - Deferred'){ x.StageName = '1 - Research'; x.Ask_Ready_Date__c = x.Ask_Ready_Date__c + 365; x.npsp__Ask_Date__c = x.npsp__Ask_Date__c + 365; x.Verbal_Date__c = x.Verbal_Date__c + 365; x.Committed_Date__c = x.Committed_Date__c +365; x.CloseDate = x.CloseDate + 365; }else if (x.StageName == '0 - Declined'){ x.StageName = '1 - Research'; x.Ask_Ready_Date__c = x.Ask_Ready_Date__c + 365; x.npsp__Ask_Date__c = x.npsp__Ask_Date__c + 365; x.Verbal_Date__c = x.Verbal_Date__c + 365; x.Committed_Date__c = x.Committed_Date__c +365; x.CloseDate = x.CloseDate + 365; }else if (x.StageName == '0 - Unresponsive') { x.StageName = '1 - Research'; x.Ask_Ready_Date__c = x.Ask_Ready_Date__c + 365; x.npsp__Ask_Date__c = x.npsp__Ask_Date__c + 365; x.Verbal_Date__c = x.Verbal_Date__c + 365; x.Committed_Date__c = x.Committed_Date__c +365; x.CloseDate = x.CloseDate + 365; }else if (x.StageName == '1 - Research') { x.Ask_Ready_Date__c = x.Ask_Ready_Date__c + 365; x.npsp__Ask_Date__c = x.npsp__Ask_Date__c + 365; x.Verbal_Date__c = x.Verbal_Date__c + 365; x.Committed_Date__c = x.Committed_Date__c +365; x.CloseDate = x.CloseDate + 365; }else if (x.StageName == '2 - Intro'){ x.Ask_Ready_Date__c = x.Ask_Ready_Date__c + 365; x.npsp__Ask_Date__c = x.npsp__Ask_Date__c + 365; x.Verbal_Date__c = x.Verbal_Date__c + 365; x.Committed_Date__c = x.Committed_Date__c +365; x.CloseDate = x.CloseDate + 365; }else if (x.StageName == '3 - Educate'){ x.Ask_Ready_Date__c = x.Ask_Ready_Date__c + 365; x.npsp__Ask_Date__c = x.npsp__Ask_Date__c + 365; x.Verbal_Date__c = x.Verbal_Date__c + 365; x.Committed_Date__c = x.Committed_Date__c +365; x.CloseDate = x.CloseDate + 365; }else if (x.StageName == '4 - Ask Ready'){ x.npsp__Ask_Date__c = x.npsp__Ask_Date__c + 365; x.Verbal_Date__c = x.Verbal_Date__c + 365; x.Committed_Date__c = x.Committed_Date__c +365; x.CloseDate = x.CloseDate + 365; }else if (x.StageName == '5 - Request'){ x.Verbal_Date__c = x.Verbal_Date__c + 365; x.Committed_Date__c = x.Committed_Date__c +365; x.CloseDate = x.CloseDate + 365; }else if (x.StageName == '6 - Verbal'){ x.Committed_Date__c = x.Committed_Date__c +365; x.CloseDate = x.CloseDate + 365; }else if (x.StageName == '7 - Committed'){ x.Committed_Date__c = x.Committed_Date__c +365; x.CloseDate = x.CloseDate + 365; }else{ x.Committed_Date__c = x.Committed_Date__c +365; x.CloseDate = x.CloseDate + 365; } //get this picklist's value and update field with method. x.Designated_Fiscal_Year__c = x.Designated_Fiscal_Year__c ; if (oppList.size() > 0) insert oppList; oppList.add(x); } }Here is what I've got in my controller class:
public class FinancialCloningController { //added an instance varaible for the standard controller private ApexPages.StandardController controller {get; set;} //instance for the variables being passed by id on the url private Opportunity opp {get;set;} //set the id of the record that is created public ID newRecordId {get;set;} public FinancialCloningController(ApexPages.StandardController controller) { this.opp = (Opportunity)controller.getRecord(); this.controller = controller; opp = (Opportunity)controller.getRecord(); } public PageReference cloneOpportunity() { // setup the save point for rollback Savepoint sp = Database.setSavepoint(); Opportunity newOpp; try{ //Copy the Opportunity - only include the records you want to clone opp = [SELECT Id, Name, StageName FROM Opportunity WHERE ID = :opp.id]; insert newOpp; newOpp.clone(false, false, false, false); //set the id of the new po created for testing newRecordId = newOpp.id; } catch (Exception e){ // roll everything back in case of error Database.rollback(sp); ApexPages.addMessages(e); return null; } return new PageReference('/'+newOpp.id+'/e?retURL=%2F'+newOpp.id); } }I'm not sure that I can isolate a triggers actions to a button. Obviously, I'm very new to Salesforce and don't know if there is a way to do this without triggers. I feel the trigger is too powerful in this case. Why is the original record saving as a new record (conditional logic is fine and works), and can this trigger be isolated to a button action?
Thanks.
Yes, we can make trigger run only once and in your scenario it is quite easy. Below scenario is for Before Update as in Before Insert call Trigger will always processed once. In Above example, update call will always run once when record is inserted and updated.
If you require to make an update once after the record is inserted, then please follow below steps:
Hope this helps!!!
Thanks,
Gaurav
Skype: gaurav62990
All Answers
I understand your first problem
- Create a List<Opportunity>, and add Opportunity record into it.
- Clone the first record and update the name of Opportunity_Planning and add the record in list.
- Now, save List. This wil save both the record.
Could you please explain your second problem.Thanks,
Gaurav
Skype: gaurav62990
I only need this trigger to fire once, but any updates I make to the document trigger the changes.
Yes, we can make trigger run only once and in your scenario it is quite easy. Below scenario is for Before Update as in Before Insert call Trigger will always processed once. In Above example, update call will always run once when record is inserted and updated.
If you require to make an update once after the record is inserted, then please follow below steps:
Hope this helps!!!
Thanks,
Gaurav
Skype: gaurav62990
Thank you for this solution, however, I am still getting an error that 'Trigger does not exist'.
Can we do a screen sharing session and figure out what is actually failing on your side. Please contact me on skype: gaurav62990
Thanks,
Gaurav