0

I am working on parsing CSV file to insert bulk records on an object (OBJECT 1)and populate two lookup fields(OBJECT 2 & OBJECT 3) on it. If the lookup records are already present in the database I need to query those and tag it to the object and if the records are not present I need to insert those records also and tag it to the object getting inserted. I need to also capture the error rows and send them to the user who started the upload.

My approach -

Object 1 Fields - Object 2 Lookup Object 3 Lookup

staging object - To store CSV rows and error messages on rows

Final result is to created object 1 records with lookups populated

I created two Batch classes step1batch and step2batch

trigger staging object (before insert)
 {
insert staging object with object 2 and 3 lookups
}

trigger staging object(after insert)
{
call step1batch
}

step1batch 
{
insert object 2 and 3 if the records are not there and update staging object lookup

  final
   {
call step2batch
    }
}

step2batch
  {
insert object 1 records for every stagingrecord objects only for which object 2 & 3 lookups are populated

final
{
Need to send failed list of rows to the user - this step is where I am stuck
}
  }


```
How do I capture the failed rows from the csv and send it to user
  • DML and CPU timeout are both transaction-wide limits. You need to consider all code and automation running in the transaction, not just this trigger, to determine where limits consumption is taking place. Presumably, this code is not complete even in itself, since you appear to be accumulating records for DML that is not shown here. – David Reed Aug 25 at 19:32
  • I have run the code in a new org and it seems to be working fine for limited number of records but if I try to insert 1000 records at a go and I have to insert 200 object 2 records and 150 object 3 records i think this would hit the DML limit. – Badduboy Aug 25 at 19:37
  • Could you help me with some pointers on how could I run this code using batch apex or queuable. I am reading the documentation of batch class and queuable methods but I am not sure how could I implement this run this efficiently. – Badduboy Aug 25 at 19:38
  • That would involve a rearchitecting of a lot of your code, including code that is not shown here. You may wish to consider using Unit of Work patterns instead of this elaborate dance with external Ids as you consider changing to an async pattern. – David Reed Aug 25 at 19:46
  • hi @DavidReed I have looked into the documentation of Unit of work patterns but I can't seem to figure out whether these patterns are applicable to parent-child relationship objects or can we apply to two different objects which are getting inserted at the same time like ```` insert object 1; insert object 2; ````` Also, I modified my code but I am unable to figure out how to capture the errors. Please have a look. – Badduboy Sep 2 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.