When using before_save in SugarCRM 6.5 how do I stop the save?

I am going to use before_save to review a Merge before it completes. 

If both Contact records have the two different Student Ids, I want to stop the process and display a message saying it can't be done.

  • Try paste this when you need show error:

    require_once 'include/api/SugarApiException.php';
    throw new SugarApiExceptionInvalidParameter("It can't be done");
    
  • I don't believe that works with 6.5. Plus I don't have the file include/api/SugarApiException.php in the SugarCRM 6.5 instance.

  • A brute way of doing it is:

    die("it can't be done.");

    Or you can:

    SugarApplication::appendErrorMessage('<div style="color:#555; border-radius:10px; font-family:Tahoma,Geneva,Arial,sans-serif;font-size:11px; padding:10px 10px 10px 36px; margin:10px; border:1px solid #f5aca6; background:#ffecec 50%;">

                            <span style="font-weight:bold; text-transform:uppercase;">it can't be done.</span></div>'

                    );

  • Hello Amy

    I am trying to get the same in Sugar CE 6.5, however when I introduce the message it is shown in the screen... but the record gets saved anyway. Even application gets moved from Edit View to Detail View with the message on top of the record Detail View.

    Could you please share how can you

    a) Show the message and "stop" or "prevent" the save action to take place

    b) Is is possible to show the message, remain in the Edit View, and keep the data of the record you were trying to save?.

    Thanks a lot in advance

  • Here is the code I used, fill free to email me if you have any questions:

    acox63@jccc.edu

    Note: I am also working an Email Type Customization in SugarCRM 7 case you know someone who is interested.

    <?php

    global $db, $sugar_config;

    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    class Contacts_Logic_Hooks_Stop_Merge_JCCC_ID
    {


        public function stop_saving_if_JCCC_exists($bean, $event, $arguments)
        {

            $action = $_REQUEST['action'];
            $jccc_id = null;
            $link_to_record_with_jccc_id = null;

            if ($action == 'SaveMerge') {


                // If main record has JCCC ID record it.
                if(!is_null($bean->constituent_id_c) && $bean->constituent_id_c <> '')
                    $jccc_id = $bean->constituent_id_c;

                // Loop through each id that user is trying to merge.
                foreach($_REQUEST['merged_ids'] as $bean_id)
                {
                    // Load Bean User is proposing to merge.
                    $bean_to_merge = BeanFactory::getBean('Contacts', $bean_id);
                    // If bean bring merged has a JCCC ID.
                    if($bean_to_merge->constituent_id_c <> '' && !is_null($bean_to_merge->constituent_id_c))
                    {
                        // If all previous beans have not had JCCC IDs
                        if(is_null($jccc_id))
                        {
                            // Add JCCC ID to $jccc_id so that it can be compared to other merging records JCCC IDs.
                            $jccc_id = $bean_to_merge->constituent_id_c;
                            $link_to_record_with_jccc_id = "<a href='index.php?module=Contacts&action=DetailView&record=$bean_to_merge->id'>$bean_to_merge->first_name $bean_to_merge->last_name</a>";
                        }
                        // If merging record has a different JCCC ID as last bean.
                        else if($jccc_id <> $bean_to_merge->constituent_id_c)
                        {
                            // Kill process and provide link to original link.
                            $link = "<a href='index.php?module=Contacts&action=DetailView&record=$bean->id'>$bean->first_name $bean->last_name</a>";
                            die("Merge Stopped. Two records with different JCCC IDs can not be merged.<BR>$link");
                        }
                    }
                }
                // If the main record doesn't have a JCCC ID,
                if((is_null($bean->constituent_id_c) || $bean->constituent_id_c == '') && !is_null($jccc_id))
                    // Stop Merge and direct user to record with JCCC ID.
                    die("Merge Stopped. Contact record with JCCC ID must be primary. <BR> JCCC ID $jccc_id
     $link_to_record_with_jccc_id");

            }
        }

    }

    ?>