Redirecting to another page inside a PHP script in Sugar

sugarcrmdevelopers —  January 18, 2013 — 13 Comments

Here’s a great forums question

I have a form when clicking on it need to go other.php file-available in crm root folder.

I have given header(‘Location: other.php’); – It is not working.

I think need to provide action like header(‘Location: index.php?action=other’); – But some where need to register this file.

Kindly give your views for redirection issue.

Using the header() method can be challenging in Sugar, as you are dependent on whether or not output from the script has been returned to the user. A better choice, as pointed out by Sugar Community Member item, is to call the SugarApplication::redirect() method as shown below…

This will do a redirect using the PHP header() call with a 301 redirect if you can, otherwise will send a javascript call to redirect at that level if you can’t.

13 responses to Redirecting to another page inside a PHP script in Sugar

  1. 

    Just used this today. Thanks!

  2. 

    Additionally you could use http_build_query to secure your parameters:


    $queryParams = array(
    'module' => 'DetailView',
    'action' => 'Accounts',
    'record' => $recordId,
    );

    SugarApplication::redirect('index.php?' . http_build_query($queryParams));

  3. 

    Awesome. Worked with this today. Typo in the module/action strings, though 🙂

  4. 

    I make a registration form on PHP and displaying inputs to another page. here I want to generate “username already exist error” and want to redirect back to registration form page with an error message along with the form that “username already exist”. how to do it??

  5. 

    how is the equivalent to this in Sugar 7 ?

    • 

      It’s the same function

      • 

        I am adding a custom hook so when a lead is converted, it is marked deleted. This works however, since the lead is now deleted, sugar show an error that the page/data does not exist after the user converts the lead. So I am trying to add a redirect into the logic hook. I cannot get it working, sugar shows error that the lead was not converted (even though it was and marked deleted)

        if (‘Converted’ == $lead->status) {
        $lead->mark_deleted($lead->id);

        //new redirect somewhere?
        $urlParams = array(
        ‘module’ => ‘Accounts’,
        ‘action’ => ‘ListView’,
        );
        $url = ‘index.php?’ . http_build_query($urlParams);
        SugarApplication::redirect($url);
        //SugarApplication::redirect(‘index.php?/#Leads’); // contains header and exit
        exit;

        • 

          SugarApplication::redirect() does not have an effect here. In Sugar 7, the application is routed via JavaScript and not on the backend for the Sidecar based modules. You’ll need to write JavaScript to change behavior on the page.

  6. 

    that is a lot of work to instead just use the basic SugarApplication::redirect which has always worked.

    you can simply add a return_action to the query parameters like this

    //redirect access to the edit view of the new record
    $queryParams = array(
    ‘module’ => ‘Cases’,
    ‘action’ => ‘EditView’,
    ‘record’ => $new_case->id,
    ‘return_action’ => ‘#Cases/’. $new_case->id.’/edit’
    );
    SugarApplication::redirect(‘/index.php?’ . http_build_query($queryParams));

    for my use we needed to create a case and save it so that the person on the phone would have the case number to give the customer while they filled it out, so we needed to create the case, set some default values based on the user creating the case (business segment, stuff like that) and then show them the edit view so they could fill in the details. this approach worked in sugar 5, 6 and now 7. in fact, it worked without that return action in earlier versions of 7, but when we upgraded to 7.8.0 we needed to add that.
    we have that code in our custom action and have modified the create link to hit our action. up can do that in custom/modules/MODULE_NAME/clients/base/menus/header/header.php
    just copy the non-custom version of the file into the custom folder and make your changes.
    this is a sample of code changes we used for the cases module, but any module should work.

    $module_name = ‘Cases’;
    $viewdefs[$module_name][‘base’][‘menu’][‘header’] = array(
    array(
    ‘route’=>’#bwc/index.php?module=Cases&action=CreateNew’,
    ‘label’ =>’LNK_NEW_CASE’,
    ‘acl_action’=>’create’,
    ‘acl_module’=>$module_name,
    ‘icon’ => ‘fa-plus’,
    ),

    works just like older versions of sugar and is upgrade safe.
    hope this hels someone who doesn’t want to change the javascript because they have this code as part of an upgrade or they are more comfortable with it.

Trackbacks and Pingbacks:

  1. redirecting to other page - January 23, 2013

    […] […]

  2. Showing error messages and redirecting in SugarCRM – The Aust Gate - February 2, 2013

    […] other scripts in Sugar. John Mertic, Sugar’s community managers, wrote a short post about the SugarApplication redirect on the Sugar blog. Essentially it creates a 301 redirect (as a wrapper around the PHP header() […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s