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
sfdc_beginner7sfdc_beginner7 

How to write a trigger for copying related list from one master object to another?

Hi ,I have 2 objects "Positions" and "Employment Websites" having many to many relationship between them with "Job Posting" as a junction object.
->I have a case "C1" on "Position" 
->Position "P1" is posted to 3 Employement Websites "W1" ,"W2" and "W3"
->My requirement is to show case "C1" on Employement Website "W1" ,"W2" and "W3"

I have written a trigger for the above requirement but i am stuck
Objects :
Position
Employment Websites
Job Posting (Junction object for Position and Employement Websites)
CaseRelatedList( junction object between case and employement websites)
case (look up to Position)
I am trying to copy case related list from Position to Employment Websites .
Trigger :
trigger CloneCaseRelatedList on Case (after insert) {
    set<string> posId = new set<string>();
    Map<string,Job_Posting__c> posEmpWebsite = new Map<String,Job_Posting__c>();
    if(Trigger.isInsert)
    {
        for(Case c : Trigger.new)
        {
            posId.add(c.Position__c);
                system.debug(posId);
        }
        
    }
    for(Job_Posting__c j : [Select Position__c ,Employment_Website__c from Job_Posting__c where Position__c in : posId ])
    {
        posEmpWebsite.put(j.Position__c, j);
        system.debug(posEmpWebsite);
    }
    //I am stuck at this point How I can loop through all employmemt websites that position have
        
    }
1.I have created CaseRelatedList as a junction object for Employement Website and Case
2.Created trigger on case and from there I am getting positions
3.finding employemnt websites related to position through junction object 'Job Posting'
4.Need to loop through result of step 3. 
How can I loop through it so that I can insert case and employment website on junction object caseRelatedList.
Thanks in advance!!
Best Answer chosen by sfdc_beginner7
VamsiVamsi
Assuming Position is a parent for Case then Please find the below 
 
trigger CopyCaseToEW on Case (after insert) 
{
    set<ID> posId = new set<ID>();
    Map<ID,List<Job_Posting__c>> posEmpWebsite = new Map<ID,List<Job_Posting__c>>();
     for(Case c : Trigger.new)
        {
         if(c.Position__c!=null)
         {
            posId.add(c.Position__c);
                system.debug(posId);
         }
        }

    for(Position__c p : [select id,(select id,Employment_Website__c from Job_Postings__r) from position__c where ID IN : posId])
    {           
         List<Job_Posting__c> internallist = new List<Job_Posting__c>();
        internallist.addAll(p.Job_Postings__r);
         posEmpWebsite.put(p.id,internallist);
    }
	List<CaseRelatedList__c> CrelatedList = new List<CaseRelatedList__c>();
    for(Case cn :Trigger.new)
    {
        if(cn.Position__c!=null && posEmpWebsite.containsKey(cn.Position__c))
        {
            for(Job_Posting__c jp : posEmpWebsite.get(cn.Position__c))
            {
             CaseRelatedList__c cl = new CaseRelatedList__c();
             cl.Name = cn.CaseNumber;
             cl.Case__c = cn.id;
             cl.Employment_Website__c = jp.Employment_Website__c;
             CrelatedList.add(cl);
            }
        }
    }
List<Database.SaveResult> Finalres = Database.insert(CrelatedList,false);
}


 

All Answers

VamsiVamsi
Hi,

Is Case C1 parent for Position or is Position Parent for Case ? 
VamsiVamsi
Assuming Position is a parent for Case then Please find the below 
 
trigger CopyCaseToEW on Case (after insert) 
{
    set<ID> posId = new set<ID>();
    Map<ID,List<Job_Posting__c>> posEmpWebsite = new Map<ID,List<Job_Posting__c>>();
     for(Case c : Trigger.new)
        {
         if(c.Position__c!=null)
         {
            posId.add(c.Position__c);
                system.debug(posId);
         }
        }

    for(Position__c p : [select id,(select id,Employment_Website__c from Job_Postings__r) from position__c where ID IN : posId])
    {           
         List<Job_Posting__c> internallist = new List<Job_Posting__c>();
        internallist.addAll(p.Job_Postings__r);
         posEmpWebsite.put(p.id,internallist);
    }
	List<CaseRelatedList__c> CrelatedList = new List<CaseRelatedList__c>();
    for(Case cn :Trigger.new)
    {
        if(cn.Position__c!=null && posEmpWebsite.containsKey(cn.Position__c))
        {
            for(Job_Posting__c jp : posEmpWebsite.get(cn.Position__c))
            {
             CaseRelatedList__c cl = new CaseRelatedList__c();
             cl.Name = cn.CaseNumber;
             cl.Case__c = cn.id;
             cl.Employment_Website__c = jp.Employment_Website__c;
             CrelatedList.add(cl);
            }
        }
    }
List<Database.SaveResult> Finalres = Database.insert(CrelatedList,false);
}


 
This was selected as the best answer
sfdc_beginner7sfdc_beginner7
Hi Vamsi ,
Great!!!
Thanks alot ....That was exactly my requirement .