adding parameters when the user selects a certain record type causing a loop

I’m overriding the new button on Opportunity and depending on the record type the user selects, I want to add on a parameter to auto-populate a field. The problem that I’m facing is that I’m getting stuck in a loop. I set the sfdc.override=1

Set up:

  1. On Opportunity over-riding the new button keeping the recordtype picker, and redirecting them to the VF page below
  2. From the VF page, calling the action (see below)
  3. Setting the redirect in the extension

Here’s the URL that is built:

 /006/e?FIELD_ID=VALUE&accid=001M00000XXXXX&core.apexpages.devmode.url=1&ent=Opportunity&RecordType=0124000000XXXXX&retURL=/001M00000XXXXX&save_new=1&sfdc.override=1

VF:

 <apex:page standardcontroller="Opportunity" extensions="MyRedirect"  action="{!route}">
 </apex:page>

Extension

public class MyRedirect{
 private final ApexPages.StandardController controller;

 public Redirect(ApexPages.StandardController controller){
   this.controller = controller;
 }


public PageReference route(){
  RecordType r = [select id from RecordType  where SObjectType = 'Opportunity' and Name = 'ABC'];
  String rtId = (String)r.id;       
  If(ApexPages.currentPage().getParameters().get('RecordType').left(15) == rtId.left(15)){
            //buildURL
        Map<String, String> params = new Map<String, String>();
        params =  ApexPages.currentPage().getParameters();
       string url ='/006/e?';
       boolean flag = false;
        for(String varI : params.keyset()){
            if (!flag){
             String s =params.get(varI);
            url +=  varI +'=' +s ;               
            flag = true;     
            }
            else{
         String s =params.get(varI);
            url +=  '&' +varI +'=' +s ;
            }
        }           
        url+= '&FIELD_ID' + '=' + 'ADD VALUE';

           PageReference pageRef = new PageReference(url);
           pageref.setredirect(true);


           return pageref;
     }
     else
          return null;
}

}

References:

http://blog.jeffdouglas.com/2008/11/14/redirecting-users-to-different-visualforce-pages/

Answer

With much help from eyescream, here’s the solution. The difference to the code was adding the nooverride=1 paramter and removing save=new =1.

Here’s the code

 public PageReference route(){
  RecordType r = [select id from RecordType  where SObjectType = 'Opportunity' and Name = 'ABC'];
  String rtId = (String)r.id;       

       //buildURL
        Map<String, String> params = new Map<String, String>();
        params =  ApexPages.currentPage().getParameters();
       string url ='/006/e?'; //'/setup/ui/recordtypeselect.jsp?ent=Opportunity&save_new_url=/006/e';
       boolean flag = false;
        for(String varI : params.keyset()){
             String s =params.get(varI);
             if (varI != 'save_new'){

            if (!flag){

            url +=  varI +'=' +s ;               
            flag = true;     
            }
            else{


            url +=  '&' +varI +'=' +s ;
            }
            }
        }
        system.debug(url);
            String oppAcctId = ApexPages.currentPage().getParameters().get('accid'); 
         If((ApexPages.currentPage().getParameters().get('RecordType').left(15) == rtId.left(15)) && (oppAcctId != null) ) {
            Account a = [select id, MoneyGuard_Payment_Option__c from account where id = :oppAcctId limit 1];          
           url+= '&FIELD_ID' + '=' + VALUE;  
        }

        url += '&nooverride=1';
        system.debug(url);
        PageReference pageRef = new PageReference(url);


        return pageref;   
}

Attribution
Source : Link , Question Author : PartOfTheOhana , Answer Author : PartOfTheOhana

Leave a Comment