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
Gary PayneGary Payne 

How to display a custom object record after it was created via an Apex Class?

The following Apex Class creates a new custom record called "NBR" (New Business Request) after the Opportunity is Close Won and a picklist field called "Taken From" are filled in and then saved.  The current behavior after saving the Opportunity is displaying the Opportunity Detail screen.  We need to display the new NBR record in Edit Mode instead of displaying the Opportunity Detail screen.  Can Apex Code be added to the following Apex Class to accomplish this, or is something else needed?

public class NBRManagement {

    public static final String MESSAGE_ERROR_NBR_EXISTS  = 'This opportunity already has a NBR.';
    
    public static void beforeNBRInsert(List<NBR__c> nbrList){
 
        updateFields(nbrList,new Map<Id, NBR__c>());
    } 

    public static void beforeNBRUpdate(List<NBR__c> nbrList, Map<Id, NBR__c> oldNBRMap){
        updateFields(nbrList,oldNBRMap);
    }

    public static void updateFields(List<NBR__c> nbrList, Map<Id, NBR__c> oldNBRMap){

    Set<Id> oppIds = new Set<Id>();
    Map<Id,Opportunity> opportunityMap = new Map<Id,Opportunity> ();   
    List<NBR__c> relatedNBRs;
    boolean isInsert=false;
    
    if(oldNBRMap==null || oldNBRMap.size()==0){
      isInsert=true;
    }

    for(NBR__c nbr : nbrList){
    
      if (nbr.Account__c==null && nbr.Opportunity__c!=null){
        oppIds.add(nbr.Opportunity__c);
      }  
                       
    }//end-for
    
    if (oppIds.size()>0){
      
      for(Opportunity opp : [Select Id, AccountId, (Select Id From NBR__r) From Opportunity Where Id in: oppIds]) {
      
        opportunityMap.put(opp.Id, opp);  
        
      }
    }

     
    for(NBR__c nbr : nbrList){

      if(nbr.Opportunity__c!=null
        && opportunityMap.containsKey(nbr.Opportunity__c)){
          
        relatedNBRs = opportunityMap.get(nbr.Opportunity__c).NBR__r;
        
      }else{
        relatedNBRs = null;
      }

      if(isInsert
        && relatedNBRs!=null 
        && relatedNBRs.size()>0){
      
        nbr.addError(MESSAGE_ERROR_NBR_EXISTS);
      }
    
      if (nbr.Account__c==null 
          && nbr.Opportunity__c!=null
          && opportunityMap.containsKey(nbr.Opportunity__c)){

        nbr.Account__c = opportunityMap.get(nbr.Opportunity__c).AccountId;

      }  



                
    }//end-for
      
    }

}
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Gary Pane.,

 Hello Gary, If you like to navigate away after save other than to the detail page, you will have to customize the Edit page and write your custom rule for the save button instead of using the standard one. 
[Note: Right now the creation of NBR is happening through the trigger code, which wont help you in navigating away from detail page]

There is also an other way you could do that, but it will be one button click away after saving your opportunity, you can have a custom button cretaed on the opportunity page which works only when the Stage is closed won and will navigate you to the edit page of the NBR that was recently created as a child to that opportunity.

You could use formula buttons in this case., please have a look at below link:
https://help.salesforce.com/apex/HTViewSolution?id=000004375&language=en_US 

Hope it helps.

Thanks,
balaji