+ Start a Discussion
Kunal Purohit 4Kunal Purohit 4 

How to update master object field using junction object in trigger

i have two master detail object Author__c and Research_Paper__c and one junction object Author-Research_paper__c. Junction object has lookup field Author and Research-paper.. Now i want that after selecting Author name  from Lookup field, author name should be updated automatically in "Name of Author" in Research-paper__c object.
Here is my code:
trigger SampleAuthorPopulate on Author_Research_Paper__c (after insert,after update) {

    set<id> setid=new set<id>();
    set<id> setauthor=new set<id>();
   for(Author_Research_Paper__c ar:trigger.new)
    {
       
        setid.add(ar.Research_Paper__c);
        setauthor.add(ar.Author__c);
    }
     map<id,Research_Paper__c> rmap=new map<id,Research_Paper__c>();
    for(Research_Paper__c rc:[select id,Name_of_Primary_Author__c from Research_Paper__c where id in:setid])
    {
        rmap.put(rc.Id, rc);
    }
    
     list<Author_c> au=[select id,name from Author__c where name in: setauthor];
    list<Research_Paper__c> rlist=new  list<Research_Paper__c>();
    for(Author_Research_Paper__c ar:trigger.new)
    {
       if(rmap.containsKey(ar.Research_Paper__c))
            {
                 if(ar.Is_Primary_Author__c==true)
                {
                  
                 rmap.get(ar.Research_Paper__c).Name_of_Primary_Author__c=au.Name;
                rlist.add(rmap.get(ar.Research_Paper__c));
                }
               
            }
    }
   update rlist;
}
David Zhu 🔥David Zhu 🔥
You may refer the code below. There could be typos in the code.
Trigger SampleAuthorPopulate on Author_Research_Paper__c (after insert,after update) {


    List<Author_Research_Paper__c> updatedList = new List<Author_Research_Paper__c>();
    List<Id> authorIds = new List<Id>();
    List<Id> researchPaperIds = new List<Id>();
    for(Author_Research_Paper__c ar:trigger.new)
    {
        if (trigger.isInsert || (trigger.isUpdate && (trigger.oldMap.get(ar.Id).Author__c != ar.Author__c)))
                updatedList.add(ar);
                authorIds.add(ar.Author__c);
                researchPaperIds.add(ar.Research_Paper__c);
        }
    }

    if (updatedList.size() > 0)
    {
        Map<Id,Author__c> authorMap= new Map<Id,Author__c>([Select Id,Name from Author__c WHERE Id in :authorIds]);

        Map<Id,Research_Paper__c> reasarchPaperMap = new Map<Id,Research_Paper__c>([Select Id,Name_of_Primary_Author__c from Research_Paper__c where id in:researchPaperIds]);

        List<Research_Paper__c> updateResearchPapers = new List<Research_Paper__c>();
        for (Author_Research_Paper__c authorRearchPaper : updateList)
        {
            string newAuthorName = authorMap.get(authorRearchPaper.Author__c).Name;
            Research_Paper__c researchPaper = reasarchPaperMap.get(authorRearchPaper.Research_Paper__c);
            researchPaper.Name_of_Primary_Author__c = newAuthorName;
            updateResearchPapers.add(authorRearchPaper);
        }

        update updateResearchPapers;
    }
}
Kristiana GrangerKristiana Granger

Hi,

I am trying to create record on junction object when detailed obj records get created,  -- 

Scenario - 
Obj Name: venue, Field Name - PlaceID__c [Text(255)

Object Name: Time,  Field Name1 - PlaceID__c[Text(255)]
                                  Field Name2 - UniqueKey [Text(255) (External ID) ]

Name: JunctionObj1__c (Junction Object between Venue and Time) - Both Field(venue-time) M-D rel

--> I am trying to build Logic where new Venue/Time record gets creeated, it should automatically create new record on junction obj, here "PlaceID__c" is common field so I am querying based on it. 

--> For Instance - when user insert record on "Venue", I need to find corrosponding record of time object based on "PlaceId" field and create new rec on Junction Obj. 

I wrote some Psudo code, but its not working out,  Can anyone share some sample code