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 Reply Children
No Data