2

I want when click Save in Edit View of some module (example Contact) to get pop up with some message (later I will get options OK and Cancel on that pop up.).

My function

YAHOO.SUGAR.MessageBox.show({msg: 'Foo'} );

is working when I put it at top of editviewdefs.php (I also must include cache/include/javascript/sugar_grp_yui_widgets.js) ) file and when opening that view I am getting that pop up. But I want it to popup on Save,not when opening EditView (this was just testing that showed me that YAHOO function is working). So I try to create seperate customJavascript.js file in custom/modules/Contacts:

    //<script type="text/javascript"
 src="cache/include/javascript/sugar_grp_yui_widgets.js"></script>
    function check_custom_data()
    {
    YAHOO.SUGAR.MessageBox.show({msg: 'Foo'} );

    }

I modified custom/modules/Contacts/metadata/editviewdefs.php

<?php
$module_name = 'Contacts';
$viewdefs ['Contacts'] = 
array (
  'EditView' => 
  array (
    'templateMeta' => 
    array (
      'form' => 
      array (
        'hidden' => 
        array (
          0 => '<input type="hidden" name="opportunity_id" value="{$smarty.request.opportunity_id}">',
          1 => '<input type="hidden" name="case_id" value="{$smarty.request.case_id}">',
          2 => '<input type="hidden" name="bug_id" value="{$smarty.request.bug_id}">',
          3 => '<input type="hidden" name="email_id" value="{$smarty.request.email_id}">',
          4 => '<input type="hidden" name="inbound_email_id" value="{$smarty.request.inbound_email_id}">',
        ),
      ),

      array(
         'buttons' =>
        array (
         0 =>
          array(
           'customCode' =>
            '<input title="Save [Alt+S]" accessKey="S" onclick="this.form.action.value=\'Save\'; return check_custom_data();" type="submit" name="button" value="'.$GLOBALS['app_strings']['LBL_SAVE_BUTTON_LABEL'].'">',
          ),
         1 =>'Cancel'
        )
      ),
        'includes'=> array(
   array('file'=>'custom/modules/Contacts/customJavascript.js'),
      ),
..........
.......

but when I click Save in EditView nothing happens but I want in that moment to get pop up with message (later I will add OK and Cancel options).

What am I doing wrong? thank you

Updated with code for pop up only with some condition:

....
     window.formToCheck = formname;

        var contactTypeField = document.getElementById('first_name');
        if (contactTypeField.value == 'Tori')
        {
        if (confirm("This dialog will pop-up whenever the user click on the Save button. "
                + "If you click OK, then you can execute some custom code, and then "
                + "execute the old form check function, which will process and submit "
                + "the form, using SugarCRM's standard behavior.")) {

            var customCodeVariable = 5;
            customCodeVariable = 55 + (customCodeVariable * 5);

            return window.old_check_form(formname);
        }

        return false;
        }
Veljko
  • 1,708
  • 12
  • 40
  • 80

2 Answers2

11

There are a number of ways to do things in SugarCRM, which makes it both very powerful and at times very difficult to customize - as there are so many different options available to you.

To make some kind of pop-up, or any custom log, happen upon clicking the "Save" button, I'd recommend the below solution rather than altering the editviewdefs.

The below solution does not require you modify any core SugarCRM files, so it is upgrade safe and can easily be installed on another instance.

What you will need to do is create a custom installable package, and install it into SugarCRM using the Module Loader.

This is the layout of the directory structure you will ultimately need to end up with:

SugarModuelPopUp
   ->custom
      ->include
         ->customPopUps
            ->custom_popup_js_include.php
            ->customPopUpContacts.js
   ->manifest.php

Create the SugarModuelPopUp folder, which will server as the root of this custom package.

Inside of SugarModuelPopUp, create a new PHP file with the name manifest.php. This file tells SugarCRM how to install the package.

In manifest.php, paste the following code:

<?php
$manifest = array(
        array(
                'acceptable_sugar_versions' => array()
        ),
        array(
                'acceptable_sugar_flavors' => array()
        ),
        'readme' => 'Please consult the operating manual for detailed installation instructions.',
        'key' => 'customSugarMod1',
        'author' => 'Kyle Lowry',
        'description' => 'Adds pop-up dialog on save on Contacts module.',
        'icon' => '',
        'is_uninstallable' => true,
        'name' => 'Pop-Up Dialog On Save',
        'published_date' => '2013-03-06 12:00:00',
        'type' => 'module',
        'version' => 'v1',
        'remove_tables' => 'prompt'
);

$installdefs = array(
        'id' => 'customSugarMod1',
        'copy' => array(
                array(
                        'from' => '<basepath>/custom/',
                        'to' => 'custom/'
                )
        ),
        'logic_hooks' => array(
                array(
                        'module' => 'Contacts',
                        'hook' => 'after_ui_frame',
                        'order' => 1,
                        'description' => 'Creates pop-up dialog on save action.',
                        'file' => 'custom/include/customPopUps/custom_popup_js_include.php',
                        'class' => 'CustomPopJs',
                        'function' => 'getContactJs'
                )
        )
);

Next, you will want to make the custom folder. Inside of that, create the include folder. Inside of that, create the customPopUps folder.

Next, you will want to create the custom_popup_js_include.php file. This file controls when and where your custom JavaScript gets included on the page. Paste in the below code:

<?php
// prevent people from accessing this file directly
if (! defined('sugarEntry') || ! sugarEntry) {
    die('Not a valid entry point.');
}
class CustomPopJs {
    function getContactJs($event, $arguments) {
        // Prevent this script from being injected anywhere but the EditView.
        if ($_REQUEST['action'] != 'EditView') {
            // we are not in the EditView, so simply return without injecting
            // the Javascript
            return;
        }
        echo '<script type="text/javascript" src="custom/include/customPopUps/customPopUpContacts.js"></script>';
    }
}

Next you will need to create the customPopUpContacts.js file, which will create the custom pop-up upon clicking the Save button in the Contacts module EditView. Paste in the below code:

function override_check_form() {
    // store a reference to the old form checking function
    window.old_check_form = window.check_form;
    // set the form checking function equal to something custom
    window.check_form = function(formname) {
        window.formToCheck = formname;
        // you can create the dialog however you wish, but for simplicity I am
        // just using standard javascript functions
        if (confirm("This dialog will pop-up whenever the user click on the Save button. "
                + "If you click OK, then you can execute some custom code, and then "
                + "execute the old form check function, which will process and submit "
                + "the form, using SugarCRM's standard behavior.")) {
            // you have clicked OK, so do some custom code here,
            // replace this code with whatever you really want to do.
            var customCodeVariable = 5;
            customCodeVariable = 55 + (customCodeVariable * 5);
            // now that your custom code has executed, you can let
            // SugarCRM take control, process the form, and submit
            return window.old_check_form(formname);
        }
        // the user clicked on Cancel, so you can either just return false
        // and leave the person on the form, or you can execute some custom
        // code or do whatever else you want.
        return false;
    }
}

// call the override function, which will replace the old form checker
// with something custom
override_check_form();

Once you have created the above directory structure, and the files in the correct folders, you can create a ZIP file of the project. It is important to note that for SugarCRM installable packages, your ZIP file must contain everything in the project directory. That is, you will not be zipping up the SugarModuelPopUp folder, but rather everything inside of it.

Next, you will want to install the custom package using SugarCRM's module loader. You can do this by:

  1. Go to the SugarCRM Admin page.
  2. Click on "Module Loader".
  3. Click on "Browse" and select the ZIP package.
  4. Click on the "Upload" button.
  5. Once the package is uploaded, find its entry in the list of installable packages, and click on "Install"; proceeding with the standard SugarCRM installation process.

With this custom package installed, whenever you click on the "Save" button in the Contacts module EditView, a dialog will pop-up. You can replace the dialog code with anything you want, so as log as you don't modify the code framing it.

Further, you should be able to use this project as a foundation for future function additions to SugarCRM EditViews. Any module which uses the check_form method upon clicking of the "Save" button can have this kind of custom logic executed.

To do so for Accounts, for example, you would do the following:

Add an entry to the logic_hooks array element in manifest.php for Accounts.

'logic_hooks' => array(
                array(
                        'module' => 'Contacts',
                        'hook' => 'after_ui_frame',
                        'order' => 1,
                        'description' => 'Creates pop-up dialog on save action.',
                        'file' => 'custom/include/customPopUps/custom_popup_js_include.php',
                        'class' => 'CustomPopJs',
                        'function' => 'getContactJs'
                ),
                array(
                        'module' => 'Accounts',
                        'hook' => 'after_ui_frame',
                        'order' => 1,
                        'description' => 'Creates pop-up dialog on save action.',
                        'file' => 'custom/include/customPopUps/custom_popup_js_include.php',
                        'class' => 'CustomPopJs',
                        'function' => 'getAccountJs'
                )
        )

Add a new method to the CustomPopJs in the custom_popup_js_include.php file for the Accounts JavaScript.

function getAccountJs($event, $arguments) {
        // Prevent this script from being injected anywhere but the EditView.
        if ($_REQUEST['action'] != 'EditView') {
            // we are not in the EditView, so simply return without injecting
            // the Javascript
            return;
        }
        echo '<script type="text/javascript" src="custom/include/customPopUps/customPopUpAccounts.js"></script>';
    }

Create the customPopUpAccounts.js file, and use the customPopUpContacts.js code as a base for the functionality you want.

There are other ways of accomplishing your goal in SugarCRM, but this is the one I use personally, and it has the benefit of being upgrade safe and easily migratable to other SugarCRM instances.

Kyle Lowry
  • 1,246
  • 10
  • 14
  • Man you are genius!hope you know that:) can't thank you enough...exactly what I wanted.-on OK to Save it on Cancel not to Save it!!! Just one more little thing- if I want this warning to pop up only when on example field Type of Contact is "Business". Where to enter that condition? In some php file or javascript file? If you could tell me just that additional thing. Thank you in advance – Veljko Mar 07 '13 at 01:11
  • 1
    In `customPopUpContacts.js`, just after `window.formToCheck = formname;` you can wrap that `confirm` code block in an if statement to make it only appear conditionally. Personally, I use jQuery with SugarCRM as I find it more straightforward to work with. But you could do something like `var contactTypeField = document.getElementById('contact_type'); if (contactTypeField.value == 'Business') { // the confirm block will go here }` – Kyle Lowry Mar 07 '13 at 01:15
  • I tried with first_field with testing purposes. I entered if Tori then pop up. If I click on Contact with Tori as first name everything works great. But if I try to click with Contacts with some other name (Lee) on Save nothing happens- it wont save my record but it should Save record, only not to show pop up. In case when first_name!=Tori I can do only Cancel, on Save nothing happens. Please take a look at my code I added it to original post. Thank you very much – Veljko Mar 07 '13 at 01:30
  • 1
    Sorry, should have realized that before I told you. At the end of the `if` block you added, you will need to have an `else` that executes the default check form function. So, at the end of that `if (contactTypeField.value == 'Tori')` block, add the following: `else { return window.old_check_form(formname); }` – Kyle Lowry Mar 07 '13 at 01:35
  • Sorry if disturbing or taking your time but I was wondering if you could maybe help me and with this one? I am calling some web service and I want to set response in some field (on one same button to be able to call web service and to set response in field) http://stackoverflow.com/questions/15272803/sugarcrm-how-to-populate-some-field-with-web-service-response thank you in advance – Veljko Mar 07 '13 at 14:14
  • @KyleLowry This is a really detailed answer, Thanks so much!! Now I am trying to add a pop-up window that should appear if we click on an account whose status is set to inactive. any thoughts on this ? Thanks in advance! :) – swifty Aug 06 '15 at 03:21
2

I have SugarCRM CE 6.5 and this method didn't work to me (I mean creating the new module). However, I modified the logic_hook and put the files directly in custom/include folder and it works!

Mogsdad
  • 44,709
  • 21
  • 151
  • 275