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
  • Why wouldn't you just do this in the record.js?  It is almost never a good idea to have code that throws an error code in the logic hooks if you don't absolutely have to.

    I dont really understand what

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

    is going to return with no SELECT or WHERE clauses, but it would be easy enough to use JavaScript to check for your conditions and then just show an error to the user and allow them to correct the issue.

  • Hi Kenneth Brill

                 Thanks for your Response and really sorry for the late reply. I have written an API to fetch my DB values and i  called that api in both create and record.js to validate the duplicate record,But when i try to save the record during creation... filed validation is overridden by the save function(create.js) i.e. even though i have written the required field validation using addValidationTask()  it is saving the record without value.whereas it is working fine during record update(record.js) .

             I could not able to validate the record at the time of creation in both the logics(logichook and javascript valiadation)

              Kindly suggest, how can i override the save button during creation without disturbing the Field validation. 

  • I think your problem is probably the same one I am having with an API call in the validation

    My question is here How to do a validation that requires an API call 

Reply Children