+ Start a Discussion
Devendra@SFDCDevendra@SFDC 

retURL with multiple parameters

Hello Folks,

 

How to use retURL with multiple parameters in apex method?

 

Thanks,

Devendra

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Okay, so the vital piece of information was that the URL posted was that generated after clicking the 'cancel' button on the opportunity edit page, rather than that generated by the controller.

 

Having tried this in my dev org, it turns out that the URLEncoding isn't needed when you add the parameters via the PageReference.getParameters(), as this does the encoding for you.

 

The following should do the trick:

 

public pagereference clickMe()
{
    PageReference pg = new PageReference('/'+opp.Id +'/e');
    String returlstr='/apex/PageRetURL2?accountid='+acct.id+'&contactid='+con.Id;

    pg.getParameters().put('retURL', returlstr);
    pg.setRedirect(true);
    return pg;
}

 

All Answers

Devendra@SFDCDevendra@SFDC

I tried with following code,

 

public pagereference clickMe(){
        PageReference pg = new PageReference('/'+opp.Id+'/e?retURL=/apex/PageRetURL2?accountId='+acct.Id+'%26'+'contactid='+con.Id);
        pg.setRedirect(true);
        return pg;
    }

 

At the place of "&"  I have used '%26' and it started working.

 

Is there any other solution?

 

Thanks,

Devendra

bob_buzzardbob_buzzard

That is the correct way to do it.  Essentially you have a parameter that actually contains a full URL, including the query string separator '?' and the parameter separator '&'.  You have to URL encode these characters, otherwise they will be interpreted as part of the main URL, so in this case when the browser hits the '&' between accountId and contactid, it is interpreted as the end of the returl parameter, which no doubt breaks your URL.

 

You should URL encode everything in the value (e.g. the right hand side of the '=') of each parameter.

Devendra@SFDCDevendra@SFDC

Hello Bob, Thanks for the quick response.

"You should URL encode everything in the value (e.g. the right hand side of the '=') of each parameter."

Can you please let me know how to encode that part?

Thanks,
Devendra

bob_buzzardbob_buzzard

When you are adding parameters to the URL, its better to add them one at a time as an URL encoded string, rather than putting them all in the page reference.

 

Thus you'd have something like:

 

public pagereference clickMe(){
        PageReference pg = new PageReference('/'+opp.Id +'/e';
String returlstr='/apex/PageRetURL2?accountid=' + acct.id + '&contactid=' + con.Id;
String encreturl=EncodingUtil.URLEncode(returlstr, 'utf-8');
pg.getParameters().put('retURL', encreturl); pg.setRedirect(true); return pg; }
Devendra@SFDCDevendra@SFDC

Hi Bob,

 

I have tried below code:

 

PageReference pg = new PageReference('/'+opp.Id +'/e');
        String returlstr='/apex/PageRetURL2?accountid='+acct.id+'&contactid='+con.Id;
        String encreturl=EncodingUtil.URLEncode(returlstr, 'UTF-8');
        pg.getParameters().put('retURL', encreturl);
        pg.setRedirect(true);
        return pg;

 

It gives  the error as "URL No Longer Exists". Encode add the some extra characters in URL string. What may be the cause?

 

Thanks,

Devendra

bob_buzzardbob_buzzard

Encode makes characters URL-safe.

 

What does the URL look like?

bob_buzzardbob_buzzard

I can't see how the code I posted could generate that - for example, the '/e' has gone missing.

 

Can you post the code from your controller.

Devendra@SFDCDevendra@SFDC

 

I am using one controller method.

 

public pagereference clickMe(){
        PageReference pg = new PageReference('/'+opp.Id +'/e');
        String returlstr='/apex/PageRetURL2?accountid='+acct.id+'&contactid='+con.Id;
        String encreturl=EncodingUtil.URLEncode(returlstr, 'UTF-8');
        pg.getParameters().put('retURL', encreturl);
        pg.setRedirect(true);
        return pg;
}

 this method open an Opportunity in Edit mode. On Cancel it should navigate to PageRetURL2 with those two param's.

 

which is not happeing when I use URLEncode.

 

I have tried URLDecode, and it works. But we should use URLEncode, Am I correct?

 

Thanks,

Devendra

bob_buzzardbob_buzzard

Decode won't do anything as the string wouldn't be url encoded at that point.

 

I don't understand where the 'retURL' parameter could have gone - the code that you have posted doesn't look like it generated the code above, as the parameter is missing its name.  Rather the URL looks like it has been concatenated with its parameters.

bob_buzzardbob_buzzard

I'm going to try this on my dev org to see if I can figure it out - I'm using much the same technique in my code and its working fine.

bob_buzzardbob_buzzard

Okay, so the vital piece of information was that the URL posted was that generated after clicking the 'cancel' button on the opportunity edit page, rather than that generated by the controller.

 

Having tried this in my dev org, it turns out that the URLEncoding isn't needed when you add the parameters via the PageReference.getParameters(), as this does the encoding for you.

 

The following should do the trick:

 

public pagereference clickMe()
{
    PageReference pg = new PageReference('/'+opp.Id +'/e');
    String returlstr='/apex/PageRetURL2?accountid='+acct.id+'&contactid='+con.Id;

    pg.getParameters().put('retURL', returlstr);
    pg.setRedirect(true);
    return pg;
}

 

This was selected as the best answer
Devendra@SFDCDevendra@SFDC
Thanks Bob.

Its working now, very well explained by you!!

Thanks,
Devendra