Redirecting to another page inside a PHP script in Sugar

Sugar Dev Team —  January 18, 2013 — 12 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.

12 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.

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