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
skiptomylou11skiptomylou11 

Automatic creation of a Child record via trigger and storing the respective ID

Dear all,

 

I have put together the trigger below which creates a child record (SYS_Capture_Plan__c) when a new master record (Project__c) is created.

From what I saw this works fine.

 

trigger AutoCreateCapturePlan on Project__c (after insert) {
    List<SYS_Capture_Plan__c> SYSCapturePlan = new List<SYS_Capture_plan__c>();
     
    for (Project__c newPosition: Trigger.New) {
         {
            SYSCapturePlan.add(new SYS_Capture_Plan__c(
                        Name = newPosition.Name + ' - Capture Plan',
                        Project__c = newPosition.id ));
        }
    }
    insert SYSCapturePlan;
}

 What I would now like to do is record the ID of the child object that gets created on a filed on the master level (Field name = Capture_Plan_id__c).

I would actually like to hide the related list in the master object page layout and instead include a button (using the Capture_plan_id__c field) to link it.

Can this be included in the existing code? (probably a stupid question).

 

Moreover, I'm struggling with writing a test script for this. Could anybody help?

 

Many thanks,

patrick

vishal@forcevishal@force
trigger AutoCreateCapturePlan on Project__c (after insert) 
{
    List<SYS_Capture_Plan__c> SYSCapturePlan = new List<SYS_Capture_plan__c>();
     
    for (Project__c newPosition: Trigger.New) {
         {
            SYSCapturePlan.add(new SYS_Capture_Plan__c(
                        Name = newPosition.Name + ' - Capture Plan',
                        Project__c = newPosition.id ));
        }
    }
    insert SYSCapturePlan;

    for(SYS_Capture_Plan__c cpc : SYSCapturePlan)
    {
	Project__c project = new Project__c(id = cpc.Project__c);
	project.Capture_Plan_id__c = cpc.Id;
	lstProjectsToUpdate.add(project);
    }

    update lstProjectsToUpdate;
}

 Hi!

 

This isn't exactly the right code, I just thought of showing you an example of how you can achieve what you want to do. Its not exactly right because the field where you'll be storing the Id's of child records should have some specific format. Like in case of multiple child records, how should the id be stored in that field?

 

 

About test coverage for this trigger,  all you need to do is create a test Project__c record with all the required fields and values(if there are any conditions) and insert it, rest will be taken care of.

 

Here all you would need is the Project Name and Id for the child record to get created :

 

so your test coverage can be :

 

@isTest
private class test {

    static testMethod void myUnitTest() {
        Project__c project = new Project__c(Name='test');// in case of required fields other than name, do //add them too.
        insert project; // once you perform this action, the trigger gets fired and creates the child        //record for the Project.
    }
}

 

skiptomylou11skiptomylou11

Hi Vishal,

 

Many thanks for the super fast response on the test code and an option on how to store the ID on the master object.

Actually my idea was that the ID field will always only store one ID, as the only child will be the one created via the trigger.

In the end this should somewhat be a 1:1 relationship.

 

Again, many thanks and best regards,

patrick

vishal@forcevishal@force

in that case, this should help! 

I hope it did :)

skiptomylou11skiptomylou11

Dear Vishal,

 

When saving I receive the following error message:

Error: Compile Error: Variable does not exist: project at line 20 column 12

 

trigger AutoCreateCapturePlan on Project__c (after insert) {
    List<SYS_Capture_Plan__c> SYSCapturePlan = new List<SYS_Capture_plan__c>();
     
    for (Project__c newPosition: Trigger.New) {
         {
            SYSCapturePlan.add(new SYS_Capture_Plan__c(
                        Name = newPosition.Name + ' - Capture Plan',
                        Project__c = newPosition.id ));
        }
    }
    insert SYSCapturePlan;
    
 for(SYS_Capture_Plan__c cpc : SYSCapturePlan)
    {
    Project__c project = new Project__c(id = cpc.Project__c);
    project.Capture_Plan_id__c = cpc.Id;
    lstProjectsToUpdate.add(project);
    }

    update lstProjectsToUpdate;
}

I assume the 2nd part of the trigger (saving the ID) needs to be covered with test code as well, right?

 

Many thanks,

patrick