I have Four objects in salesforce Project , Project Phase , Project Task, Task Details.

Project Is master Project Phase Is child. Project Phase is Master and Project task is child Project Task is Master and Task Details is child.

In the system, there is a project, along with phases and tasks and details. I want to create a sample record by copying these objects data.

I have a situation now i have three tasks records and i want to create tasks, phases, details of these tasks and associate this to new Project.

How do i achieve this?

In simple words i have task records and i have to insert first a new hardcoded project than associate these task and the phases of these tasks and details of these task to new hardcoded project.


1 Answer 1


Depending on the size of your project, you might consider using the SObjectUnitOfWork from the financial force apex commons libary:

It allows you to register all your objects and define the relations and as soon as you are done, you just insert the whole thing. #bulkified Your code would look somewhat like this:

fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
    new Schema.SObjectType[]{

for (Integer i=0 ; i<100 ; i++) {
    Account a = new Account(Name= 'Test' + i);

    for (Integer j=0 ; j<5 ; j++) {
        Contact c = new Contact(LastName = 'Test'+i + ' ' +j);
        uow.registerNew(c, Contact.AccountId, a);

        Note n = new Note(Body='Test '+i + '' + j, Title='Test'+i+j);
        uow.registerNew(n, Note.ParentId, c);


If it's just a small project, I would suggest creating Maps with your parent as a key and walk through them setting their relationship.

  • Is there any other way except using any these library. Nov 30, 2017 at 7:38
  • @HasanShamsi Yes, anything you can do with ff-commons you can do by hand. It's open source, so you might want to just read it. Or you can write your own specialty version if you like.
    – sfdcfox
    Nov 30, 2017 at 8:04
  • Well I do have tasks and i would need to insert its parent, grandparent and child as seperate records . This is where i do not know how to achieve that Nov 30, 2017 at 11:27

Your Answer

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

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