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
Luke Higgins 23Luke Higgins 23 

Apex trigger to create child record based on specific criteria

I am trying to create a child record (jstcl__PlacementTeamMember__c) after the creation of the parent record (ts2__Placement__c). I am able to get all of it populated in the new child record besides the jstcl__User__c field. It seems like the "newTeamMem.ts2__Client__r.VAM_or_NAM_Member__c" portion is incorrect. This is problematic to the IF statement as well because it is being fired everytime a parent record is created when it should only fire when the criteria is met. 

trigger autoAddNAM_VAM on ts2__Placement__c (after insert) {
    List<jstcl__PlacementTeamMember__c> teamMems = new List<jstcl__PlacementTeamMember__c>();
    
    for(ts2__Placement__c newTeamMem : Trigger.New){
        if (newTeamMem.ts2__Client__r.VAM_or_NAM_Member__c != null){
            teamMems.add(new jstcl__PlacementTeamMember__c(
                	jstcl__Placement__c = newTeamMem.Id,
                	jstcl__User__c = newTeamMem.ts2__Client__r.VAM_or_NAM_Member__c,
                	jstcl__CommissionPlan__c = 'a4g370000008PGRAA2',
                	jstcl__SplitPercent__c = 100));
        }
    }
    insert teamMems;
    
}
Thank you.
Best Answer chosen by Luke Higgins 23
Maharajan CMaharajan C
Hi Luke,

From the Trigger.New It's not possible to access the lookup records value it only hold the particular record details. So you have to query that parent record details from the database and then you have to  access:

Your trigger should be like below but please check the Object Api name's in the below code:

trigger autoAddNAM_VAM on ts2__Placement__c (after insert) {
    List<jstcl__PlacementTeamMember__c> teamMems = new List<jstcl__PlacementTeamMember__c>();
    set<Id> tsClientSet = new set<id>();
    Map<ID, ts2__Client__c> tsClientmap = new Map<ID, ts2__Client__c> ();   // Check the API Name of ts2__Client__c object
    
    for(ts2__Placement__c newTeamMem : Trigger.New)
    {
        if(newTeamMem.ts2__Client__c != null)
        {
            tsClientSet.add(ts2__Client__c);
        }
    }
    
    if(ts2__Client__c.size() > 0)
    {
        tsClientmap = new Map<ID, ts2__Client__c>([SELECT Id, VAM_or_NAM_Member__c from ts2__Client__c where ID IN: tsClientSet]);
    }
    
    for(ts2__Placement__c newTeamMem : Trigger.New){
        if(tsClientmap.Containskey(newTeamMem.ts2__Client__C))
        {
            if (tsClientmap.get(newTeamMem.ts2__Client__C).VAM_or_NAM_Member__c != null){
                teamMems.add(new jstcl__PlacementTeamMember__c(
                        jstcl__Placement__c = newTeamMem.Id,
                        jstcl__User__c = tsClientmap.get(newTeamMem.ts2__Client__C).VAM_or_NAM_Member__c,
                        jstcl__CommissionPlan__c = 'a4g370000008PGRAA2',
                        jstcl__SplitPercent__c = 100));
            }
        }
    }
    insert teamMems;
    
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

 

All Answers

Maharajan CMaharajan C
Hi Luke,

From the Trigger.New It's not possible to access the lookup records value it only hold the particular record details. So you have to query that parent record details from the database and then you have to  access:

Your trigger should be like below but please check the Object Api name's in the below code:

trigger autoAddNAM_VAM on ts2__Placement__c (after insert) {
    List<jstcl__PlacementTeamMember__c> teamMems = new List<jstcl__PlacementTeamMember__c>();
    set<Id> tsClientSet = new set<id>();
    Map<ID, ts2__Client__c> tsClientmap = new Map<ID, ts2__Client__c> ();   // Check the API Name of ts2__Client__c object
    
    for(ts2__Placement__c newTeamMem : Trigger.New)
    {
        if(newTeamMem.ts2__Client__c != null)
        {
            tsClientSet.add(ts2__Client__c);
        }
    }
    
    if(ts2__Client__c.size() > 0)
    {
        tsClientmap = new Map<ID, ts2__Client__c>([SELECT Id, VAM_or_NAM_Member__c from ts2__Client__c where ID IN: tsClientSet]);
    }
    
    for(ts2__Placement__c newTeamMem : Trigger.New){
        if(tsClientmap.Containskey(newTeamMem.ts2__Client__C))
        {
            if (tsClientmap.get(newTeamMem.ts2__Client__C).VAM_or_NAM_Member__c != null){
                teamMems.add(new jstcl__PlacementTeamMember__c(
                        jstcl__Placement__c = newTeamMem.Id,
                        jstcl__User__c = tsClientmap.get(newTeamMem.ts2__Client__C).VAM_or_NAM_Member__c,
                        jstcl__CommissionPlan__c = 'a4g370000008PGRAA2',
                        jstcl__SplitPercent__c = 100));
            }
        }
    }
    insert teamMems;
    
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

 
This was selected as the best answer
Luke Higgins 23Luke Higgins 23
You da bomb Maharajan