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

  • I noticed that there is no where condition at your SugarQuery object, so all existing data are manipulated, which may trigger to memory overflow or timeout.

    Regards

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Hi André Lopes,

         Thank you for your response,Let me check by including the where condition in my logic.

    Regards,

  • 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;
         }
    }
  • Hi Ateeq Ur Rehman

              Thank you for your Response, I have tried with your logic.It allowing the duplicate records.I need to stop duplicating the record being created by throwing the error messages.

    Regards,

    Roshini 

  • 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 

  • Hi kenneth,

           Thank you for your reply.Yes, I am facing the same issue that you have discussed in the above mentioned forum.

    I have tried your solution. Now its throwing custom required error message along with my API response that i am displaying via sugar alert.It is working.Thank you for your time and guidance:):) and I thank everyone for the good suggestions:)

  • I recall there was a bug that would prevent the frontend from handeling the api exceptions correctly in the case of a new record. When updating an existing record the message from the exception is correctly shown to the user. Nevertheless, 

    Kenneth Brill it's way better to have the check in both the UI and the backend. Never trust your user's input. When this check is only available in the frontend what would happen if someone created records using the api from a external integration?