4

Redirect to editview from beforesave hook logic

I am trying to redirect from before save hook logic if an error occurs in it.It should redirect to edit view with error.I am using this code:

function ShowError($errorMsg,$beanID){
    try{
        SugarApplication::appendErrorMessage('An error has been occured: '.$errorMsg);
        $params = array(
            'module'=> 'ad123_Ads',
            'action'=>'EditView', 
            'id' => $beanID
        );
        SugarApplication::redirect('index.php?' . http_build_query($params));
    } catch (Exception $e) {
        echo 'Caught exception: ',  $e, "\n";
    }

}

But the problem is after redirect all fields get unfilled.

wscourge
  • 8,047
  • 10
  • 43
  • 66

2 Answers2

1

One of the params is wrong, it has to be record instead of id.

Try this:

try{
         SugarApplication::appendErrorMessage('An error has been occured: '.$errorMsg);
                $params = array(
                  'module'=> 'ad123_Ads',
                  'action'=>'EditView', 
                  'record' => $beanID
                );
        SugarApplication::redirect('index.php?' . http_build_query($params));
            }
    catch (Exception $e) {
          echo 'Caught exception: ',  $e, "\n";
        }

I don't recommend this kind of validations, as you will lose the changes made to the form. Usually, you have to do JS validation before submitting so the user has time to correct the errors before submitting and losing all the changes.

mrbarletta
  • 838
  • 9
  • 15
1
SugarApplication::appendErrorMessage('End Date must be after Start Date');
$queryParams = array(
                'module' => $module_name,
                'action' => $action,
                'record' => $recordId,
            );
            SugarApplication::redirect('index.php?' . http_build_query($queryParams));

Here $module_name is the module which you are writing logic hook on. $action is 'EditView' or 'DetailView'. $recordId is $bean->id

You need to understand that before_save logic hook will not return a record ID as it has not been assigned yet. This will return the page blank.

I think the best way to tackle this is by using after_save logic hook. When you pass the bean->id to above recordId this will restore all the values that are on the form.