5

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/

6
  • This will probably be a stupid question... Any reason you can't simply set the field's default value to a formula based on $RecordType.DeveloperName? SHould work for text fields, for picklists - you can set default val. per record type too...
    – eyescream
    Commented Apr 30, 2013 at 18:08
  • the field_to_set is a picklist based on a query from the opportunity's account. Also, the field_to_set has a dependant multiselect picklist.... I don't think a formula can handle all that, right? Commented Apr 30, 2013 at 18:23
  • No, for picklists you select 1 default per rec. type and that's it, no logic. Was worth trying though, question seemed to make it only rec. type-specific. To force navigation to standard page you'll have to make sure the URL contains &nooverride=1. Also - add an if in the loop to stop copying sfdc.override=1 if it does get copied.
    – eyescream
    Commented Apr 30, 2013 at 18:43
  • You're getting me closer- the &nooverride=1 stopped the looping. One last thing, the validation rules are firing when the opp LOADS. how do i get around that? Is there an immediate param? Commented Apr 30, 2013 at 18:52
  • Val. rules? But there's no save happening... Or do you mean required fields? Yeah, I had it too when I needed such trick, couldn't come up with anything meaningful. Ended up just putting some dummy data (like Name = Change me or "Opp for Account XYZ") in the url... You could also make (most of) the fields not required.
    – eyescream
    Commented Apr 30, 2013 at 18:55

1 Answer 1

4

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;   
}
2
  • Cool :) Make sure to mark it accepted when you can :) Also I think you could optimize a bit - for loop could become something smart about CurrentPage.getParameters, pass it to new page, set parameters... No real need to handcraft the url like that I think, especially with this "should there be ? or & now"
    – eyescream
    Commented Apr 30, 2013 at 22:18
  • Good catch- that's left over from debugging/pulling my hair out! Commented Apr 30, 2013 at 23:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .