Skip to main content

My requirment is - Junction Object (Junction1__c) record should get created when user create new record on "Account"/"Location" object

Data model -

Account Obj 

M_number__c field

Location object

M_number__c field

Junction1 Object

Account__c (M-D Rel with Account)

location__c (M-D rel with location)

When user create new recrod on Account Or Location object, it should automatically create new Rec on Junction Obj
1 answer
  1. Jul 29, 2020, 8:35 PM
    Hii Kristiana ,

    you have to write the trigger both Account and location becouse when you create Recode anywhere(Account/Location) Junction Object Recode shuold be Craeted.

     

    public class accountClassTriggerHandler{

    public static void insertAccount(List<Account> lstacc){

    List<Junction1__c> lstjunc = New List<Junction1__c>();

    for(Account objacc :lstacc){

    Junction1__c objjun = New Junction1__c();

    objjun.name = 'Test Junction Recode'; // you can map junction object name with account name like objun.name = objacc.name

    objjun.Account__c = objacc.Id;

    lstjunc.add(objjun);

    }

    if(lstjunc.size() > 0){

    insert lstjunc;

    }

    }

    }

    public class LOcationClassTriggerHandler{

    public static void insertlocation(List<Location__c> lstloc){

    List<Junction1__c> lstjunc = New List<Junction1__c>();

    for(Location__c objloc :lstloc){

    Junction1__c objjun = New Junction1__c();

    objjun.name = 'Test Junction Recode'; // you can map junction object name with account name like objun.name = objloc.name

    objjun.location__c = objloc.Id;

    lstjunc.add(objloc);

    }

    if(lstjunc.size() > 0){

    insert lstjunc;

    }

    }

    }

    trigger AccountTrigger on Account(After insert){

    if(Trigger.isInsert && Trigger.isAfter){

    accountClassTriggerHandler.insertAccount(Trigger.New);

    }

    }

    trigger LocationTrigger on location__c(After insert){

    if(Trigger.isInsert && Trigger.isAfter){

    LOcationClassTriggerHandler.insertlocation(Trigger.New);

    }

    }

    Thanks Bablu