-1

User case :

Clone/copy records to new created record once it's lookup field is equal to another record's lookup field which have related records

Trigger

trigger TrigC on Test_C (after insert,after update) {


set<Id> ParID = new set<Id>();


for(Test_C TestCamp : Trigger.new)
{
  ParID.add(TestCamp.Core_Campaign_Model__c);
}
List<Test_Scenario__c> TS = [Select Id , Test_Campaign__c from Test_Scenario__c where  Test_Campaign__c In :ParID ];
List<Test_C > TC = [Select Id,name from Test_C where Id = : Trigger.new];

 List<Test_Scenario__c> TestScenarioToUpdate = new List<Test_Scenario__c>();
  List<Test_C > TestCampToUpdate = new List<Test_C >();

for(Test_C tc1 : Trigger.new){
    for (Test_Scenario__c Testsc : TS){ 
        if(Testsc.Test_Campaign__c == tc1.Core_Campaign_Model__c){
             
             Test_Scenario__c TScena = Testsc.clone(false, false, true,true);
             TScena .Test_Campaign__c = tc1.Id;
            TestScenarioToUpdate.add(TScena);
        }
    }
}
insert TestScenarioToUpdate;

} Error : System limit exception , too many dml rows

How can i avoid this?

3
  • 2
    The error means you're inserting too many records at one time. Two things you can do. 1) Make sure your logic is tightened up. Your trigger may be working with more records than expected. 2) Move the logic to a batch class if you are indeed working with 10k+ records on every batch of Test_C records. – Drew Kennedy Nov 23 '20 at 17:49
  • Question updated @DrewKennedy – Farkous Nov 25 '20 at 12:40
  • This question has been edited 17 times, mostly cosmetic changes and rollbacks. I am locking this post to prevent further cosmetic edits, which are simply noise at this point. – David Reed Feb 28 at 19:16
0

How to clone the children Test_case__c as well ??

The clone() method clones one object. It does nothing with any related objects you may or may not have queried other than to preserve their relationship with the cloned object. You can see this by executing:

Account a = [SELECT Id, Name, (SELECT Id, LastName FROM Contacts) FROM Account WHERE Id IN (SELECT AccountId FROM Contact) LIMIT 1];

System.debug('Initial Account is: ' + a);
System.debug('Initial Account Contacts are: ' + a.Contacts);

Account newAccount = a.clone(false, false, false, false);

System.debug('New Account is: ' + newAccount);
System.debug('New Account Contacts are: ' + newAccount.Contacts);

In my Developer Edition, this yields

12:25:16:018 USER_DEBUG [3]|DEBUG|Initial Account is: Account:{Id=0011R00002TrBSwQAN, Name=Child Account, RecordTypeId=01236000000STbiAAG}

12:25:16:019 USER_DEBUG [4]|DEBUG|Initial Account Contacts are: (Contact:{AccountId=0011R00002TrBSwQAN, Id=0031R00002KTspTQAT, LastName=Johnson})

12:25:16:020 USER_DEBUG [8]|DEBUG|New Account is: Account:{Name=Child Account, RecordTypeId=01236000000STbiAAG}

12:25:16:020 USER_DEBUG [9]|DEBUG|New Account Contacts are: (Contact:{AccountId=0011R00002TrBSwQAN, Id=0031R00002KTspTQAT, LastName=Johnson})

Note that the Contacts associated with the cloned Account are the same records - with Ids and all, and still associated with the original parent Account.

Even if they weren't, doing an insert of an Account has no effect on any records in its related-record lists. You have to perform DML on them separately.

The upshot is that your child objects are not being cloned because you haven't written code to clone them and that's not what either clone() or DML does.

You'll have to build that Apex to do the cloning according to your specific business requirements and relate them with the cloned parent objects before you insert the children. You may want to look into Unit of Work patterns to help organize this work and ensure that you are efficiently using your DML limits.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.