0

I have a custom object called "Request for Startup" and an Opportunity. Several "Request for Startups" can be related to one Opportunity. Hence I created a junction object called "Request for Startup Assignment". In order to be able to filter, I created a text field called "Request_for_Startup__c". I want to fill/update that text field everytime a new "Request for Startup" is related to the Opportunity via the junction object.

In order to do so, I created a trigger on the junction object as well as a handler for the junction object and edited the Opportunity handler in order to write the names of the "Request for Startups" into the text field "Request_for_Startup__c". Here is the code that I inserted into the Oppotunity Handler:

//BEFORE UPDATE
public void onBeforeUpdate(List<Opportunity> triggerOld , List<Opportunity> triggerNew , Map<Id,Opportunity> triggerOldMap , Map<Id,Opportunity> triggerNewMap){

    //Recursivity control. Please add inside this IF any method that should NOT be executed more than once. 
        if(mustBeExecuted(triggerNewMap.keySet())){
            manageTimestamps(triggerNew, triggerOldMap);
            setVentureClientId(triggerNew);
            rollUpRFSAssignment(triggerNew, triggerNewMap);
        }

Further down:

private void rollUpRFSAssignment (List<Opportunity> triggerNew, Map<Id, Opportunity> newMap)
{

    for(Opportunity opp : triggerNew)
    {
        opp.RFS_Assignment__c ='';
    }    

    for(Opportunity opp : [SELECT ID FROM Opportunity WHERE id in : newMap.keyset()])
    {
        for(RequestforStartupAssignment__c sij : opp.Request_for_Startup_Assignments__r)
        {
            if(newMap.get(opp.Id).Request_for_Startup__c == '')
                newMap.get(opp.Id).Request_for_Startup__c = sij.Request_for_Startup__r.Name;
            else if ( (newMap.get(opp.Id).Request_for_Startup__c + ', ' + sij.Request_for_Startup__r.Name).length() < 254)
                newMap.get(opp.Id).Request_for_Startup__c = newMap.get(opp.Id).Request_for_Startup__c + ', ' + sij.Request_for_Startup__r.Name;
        }
    }       
}

Trigger:

trigger OpportunityRFS on RequestforStartupAssignment__c (after delete, after insert, after undelete, after update, before delete, before insert, before update) {

RequestforStartupAssignment_Handler handler = new 
RequestforStartupAssignment_Handler(Trigger.isExecuting);

if (Trigger.isBefore){

/* INSERT */
if (Trigger.isInsert) {
    handler.onBeforeInsert(Trigger.new); 
    }

/* UPDATE */
if(Trigger.isUpdate){
    handler.onBeforeUpdate(Trigger.old , Trigger.new , Trigger.OldMap , Trigger.NewMap); 
    }

/* DELETE */
if(Trigger.isDelete){
    handler.onBeforeDelete(Trigger.old , Trigger.oldMap); 
    }
}else{   

/* INSERT */
if(Trigger.isInsert){
    handler.onAfterInsert(Trigger.new , Trigger.newMap); 
    }

/* UPDATE */
if(Trigger.isUpdate){
    handler.onAfterUpdate(Trigger.old , Trigger.new , Trigger.oldMap , Trigger.newMap); 
    }

/* DELETE */
if(Trigger.isDelete){
    handler.onAfterDelete(Trigger.old , Trigger.oldMap); 
    }

/* UNDELETE */
if(Trigger.isUndelete){
    handler.onAfterUndelete(Trigger.new); 
    }   
}
}

Handler:

public without sharing class RequestforStartupAssignment_Handler {

private static Set<Id> processedIds;  
private Boolean isExecuting; 

public RequestforStartupAssignment_Handler(Boolean isExecuting){
    this.isExecuting = isExecuting; 
    if(processedIds == null) processedIds = new Set<Id>();  
    }

public void onBeforeInsert(List<RequestforStartupAssignment__c> triggerNew){
    }

//BEFORE UPDATE
public void onBeforeUpdate(List<RequestforStartupAssignment__c> triggerOld , List<RequestforStartupAssignment__c> triggerNew , Map<Id,RequestforStartupAssignment__c> triggerOldMap , Map<Id,RequestforStartupAssignment__c> triggerNewMap){

    //Recursivity control. Please add inside this IF any method that should NOT be executed more than once. 
        if(mustBeExecuted(triggerNewMap.keySet())){
        }
}
//BEFORE DELETE 
public void onBeforeDelete(List<RequestforStartupAssignment__c> triggerOld , Map<Id,RequestforStartupAssignment__c> triggerOldMap){
    }

public void onAfterInsert(List<RequestforStartupAssignment__c> triggerNew , Map<Id,RequestforStartupAssignment__c> triggerNewMap){

}
//AFTER UPDATE
public void onAfterUpdate(List<RequestforStartupAssignment__c> triggerOld , List<RequestforStartupAssignment__c> triggerNew , Map<Id,RequestforStartupAssignment__c> triggerOldMap , Map<Id,RequestforStartupAssignment__c> triggerNewMap){
//Recursivity control. Please add inside this IF any method that should NOT be executed more than once. 
    if(mustBeExecuted(triggerNewMap.keySet())){

    }
}
//AFTER DELETE
public void onAfterDelete(List<RequestforStartupAssignment__c> triggerOld , Map<Id,RequestforStartupAssignment__c> triggerOldMap){

}
//AFTER UNDELETE
public void onAfterUndelete(List<RequestforStartupAssignment__c> triggerNew){

}
/*
*  Identify whether a trigger event must be executed or not.  
*/
private Boolean mustBeExecuted(Set<Id> ids){
    //In a Before context we have to verify that these IDS have not been processed yet But not add them to the Set or the after won't fire. 
    if(Trigger.isBefore){
        if(processedIds.containsAll(ids)){
            return false; //If set contains Ithese DS then it run on an After in a previous execution within the same context.  
        }else{
            return true;  
        } 
    }else{
        if(!processedIds.containsAll(ids)){
            processedIds.addAll(ids); 
            return true;  
        }else{
            return false; 
        }
    } 
}
public enum TriggerCont {isINSERT,isUPDATE,isDELETE,isUNDELETE}

Unfortunatelly it does not trigger anything. Do you potentially know why?

Best, Max

  • I have updated my answer. Please check. – Mr.Frodo May 17 '19 at 8:56
0

Junction object is created only when there is many to many relationship between objects, if it is true then you are on right track.

Second, you want to perform some action basically on insertion and updation of junction object records so make sure you have added before insert, before update, after insert, after update (According to requirement ) in the Trigger declaration syntax.

trigger Trigger_Name on Object_name (before insert,before update,after insert, after update) {

Generally this is what we all forget.

Response after seeing your code:

In your handler you are not performing any action, all methods are basically blank which do nothing, so i don't think it would do anything.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.