Throwing exceptions in a logic hook in Sugar 9.2

In a before save logic hook I'm checking for certain conditions to validate the duplicate records. But i encounted with an error like  "There was an error while connecting to the server. Please try again." Actually i am trying to avoid duplicate entry for the combination of three fields.During creation and also when i try to copy the record i am getting the server error and invalid parameter exception error.

 

here is my code,

custom/module/<my_module>/validateduplicaterecord.php

<? php

$sq = new SugarQuery();
$sq->from( $recordBean, array('team_security' => false) );
$res = $sq->execute();

foreach( $res as $index=>$record ) {
if( $record['id'] !== $recordId && $record['approver_role_c'] === $approverRole &&
($approverRole=='Segment Lead' || $approverRole=='Segment Finance') && $record['segment_name_c'] === $Segment
&& $record['user_id_c'] === $Approver) {
throw new SugarApiExceptionInvalidParameter('An Record with "approver role and segment name" already exists');
}
else if( $record['id'] !== $recordId && $record['approver_role_c'] === $approverRole && $approverRole=='Solution Site'
&& $record['s01_site_id_c'] === $SiteName && $record['user_id_c'] === $Approver)
{
throw new SugarApiExceptionInvalidParameter('An Record with "approver role and site name" already exists');
}
else if( $record['id'] !== $recordId && $record['approver_role_c'] === $approverRole && $record['user_id_c'] === $Approver
&& ($approverRole=='Executive Team' || $approverRole=='BDM_GAM' || $approverRole=='SSCM') && $record['user_id_c'] === $Approver)
{
throw new SugarApiExceptionInvalidParameter('An Record with same "approver role and Approver name" already exists');
}
}

UI error-"There was an error while connecting to the server. Please try again."

sugarcrm log error-An exception happened: ( 422: invalid_parameter)An Record with same "approver role and                                               Approver name" already exists

below is the error log file,

sugarcrm log

 

Kindly Assist to overcome this issue.

 

Thanks

Parents
  • Here is some less verbose code

    <?php

    $sq = new SugarQuery();
    $sq->from( $recordBean, array('team_security' => false) );
    $res = $sq->execute();

    if( $record['id'] !== $recordId && $record['approver_role_c'] === $approverRole && $record['user_id_c'] === $Approver) {
         switch ($approverRole) {
              case 'Segment Lead':
              case 'Segment Finance':
                   if ($record['segment_name_c'] === $Segment)
                        throw new SugarApiExceptionInvalidParameter('A record with the same "approver role and segment name" already exists');
                   break;

              case 'Solution Site':
                   if ($record['s01_site_id_c'] === $SiteName)
                        throw new SugarApiExceptionInvalidParameter('A record with the same "approver role and site name" already exists');
                   break;

              case 'Executive Team':
              case 'BDM_GAM':
              case 'SSCM':
                   throw new SugarApiExceptionInvalidParameter('A record with the same "approver role and Approver name" already exists');
                   break;

              default:
                   break;
         }
    }
Reply
  • Here is some less verbose code

    <?php

    $sq = new SugarQuery();
    $sq->from( $recordBean, array('team_security' => false) );
    $res = $sq->execute();

    if( $record['id'] !== $recordId && $record['approver_role_c'] === $approverRole && $record['user_id_c'] === $Approver) {
         switch ($approverRole) {
              case 'Segment Lead':
              case 'Segment Finance':
                   if ($record['segment_name_c'] === $Segment)
                        throw new SugarApiExceptionInvalidParameter('A record with the same "approver role and segment name" already exists');
                   break;

              case 'Solution Site':
                   if ($record['s01_site_id_c'] === $SiteName)
                        throw new SugarApiExceptionInvalidParameter('A record with the same "approver role and site name" already exists');
                   break;

              case 'Executive Team':
              case 'BDM_GAM':
              case 'SSCM':
                   throw new SugarApiExceptionInvalidParameter('A record with the same "approver role and Approver name" already exists');
                   break;

              default:
                   break;
         }
    }
Children
No Data