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
Amack DawgAmack Dawg 

Changing Cloned Record fields before its Saved? (ISCLONE fncn)

Our sales team will clone opportunities with the same accounts for new upsell opps, but there is a critical Checkbox Field with a Validation Rule that we need to default as "FALSE" (unchecked) however, when the SR clones the record, the checkbox defaults as 'TRUE' which "lets" the rep unwittingly bypass the correct sales step that the Checkbox/validation rule is designed to guide against.  

I am trying to create some business logic that resets a cloned Opportunity check-box field to FALSE if that Opportunity record is cloned. 

Here is what I have tried:
IF (ISCLONE(), Example_Checkbox_field1__c = FALSE, Example_Checkbox_field1__c = TRUE)
I figured out that this rule is only accepted by the syntax checker if the Eval Criteria is set to "Created, and every time it's edited.'

However, this WFR does not work (I made sure it was Activated), cloning the opportunity still results with this Checkbox defaulted as TRUE in the record edit page. 

Is there a way to create a formula that sets the 'Example_Checkbox_field1__c' field to FALSE right after 'Clone' button was clicked and BEFORE the record is actually saved for the first time?
pconpcon
I was trying to do this via a WFR and was having some problems only restricting it to cloned records.  It sounds like per your workflow that it can never be checked for any new records.  Is that correct?  If so then you could create a WFR that fires on create that just sets that field to false regardless.  Or a very simple trigger to do the same.  If you only want to do it with cloned records then you can use the new isClone() sObject method that was released in Winter '16.

If I'm reading the documentation right then you should be able to do the following trigger
 
trigger OpportunityClone on Opportunity (before insert) {
    for (Opportunity o : Trigger.new) {
        if (o.isClone()) {
            o.Example_Checkbox_field1__c = false;
        }
    }
}
NOTE: This code has not been tested and may contain typographical or logicial errors.
Amack DawgAmack Dawg
@pcon, thanks for ypur help on this, apologies in advance for the 6-month rezz'ing of this thread. This code compiled successfully and I was able to get 100% code coverage on it. However, it's not actually achieving the result I want in sandbox--Cloned opportunity records do not have that checkbox field changed upon cloning the record and hitting the 'Save' (aka DML 'insert' function).

However, when I edited the logic to be:
 
trigger OpportunityClone on Opportunity (before insert) {
    for (Opportunity o : Trigger.new) {
        if (o.isClone()) {
            o.Example_Checkbox_field1__c = false;
        } else { o.Example_Checkbox_field1__c = false; }
    }
}
I get the intended result, but this is not the desired outcome: Under this 'else' logic, ALL records have the media placement field reset, not just cloned records.

What's surprising is that my Test Class has this:
 
newTestOpp.Media_Placement__c = 'MP Type';
newTestOpp.Media_Plan_Attached__c = true;

//create the Test Class!
insert newTestOpp;
        
//Cloning the newTestOpp & DML insert the record
Opportunity clonedTestOpp = newTestOpp.clone();
insert clonedTestOpp;               
                
//Grabbing cloned opp Id and asserting that Media Plan Attached field is set to False
Opportunity queriedClonedOpp = [Select Id, Media_Plan_Attached__c FROM Opportunity where Id = :clonedTestOpp.Id LIMIT 1];
System.assertEquals(false, queriedClonedOpp.Media_Plan_Attached__c);

I only want an attempted insertion of Cloned Opportunity Records to fire off this trigger-- any chance you have some logic handy to implement this?

Thanks for all the help so far!
pconpcon
What API version is your test and your trigger?
Amack DawgAmack Dawg
@pcon, API for both test and trigger are 36.0
Francesco BoccassiFrancesco Boccassi
Dear All,
Did you find a solution?
Lawrence-AccentureLawrence-Accenture
@pcon, @Amack... my testing indicates that isClone() doesn't return true for records cloned in the UI. Only for records cloned in Apex. (Although not sure if that persists through a record save and query in a separate process, or just within the currently executing process).