1

I am new to SugarCRM, so please bear with me. I am trying to add a pop-up window in SugarCRM. I need this pop-up window to show up whenever a user clicks on an Account whose status is set to 'Inactive'.

Now, I am trying to follow the instructions mentioned in this link except I am using a little more fancy? pop-up window which are based off of the YUI 2 SimpleDialog component.

SugarCRM- How to get POPUP when click on Save button?

Currently, A pop-window appears when I click on the edit button to edit the accounts. But, I want it to appear in the DetailView, when an account is inactive a pop up should be presented.

So far my code looks like this:

manifest.php:

        <?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' => 'Abhay Hans',
                'description' => 'Adds pop-up Message when an account is inactive.',
                'icon' => '',
                'is_uninstallable' => true,
                'name' => 'Pop-Up Dialog if inactive account',
                'published_date' => '2015-07-08 12:00:00',
                'type' => 'module',
                'version' => 'v1.7',
                'remove_tables' => 'prompt'
        );

        $installdefs = array(
                'id' => 'customSugarMod1',
                'copy' => array(
                        array(
                                'from' => '<basepath>/custom/',
                                'to' => 'custom/'
                        )
                ),
                'logic_hooks' => array(
                        array(
                                'module' => 'Accounts',
                                'hook' => 'after_ui_frame',
                                'order' => 1,
                                'description' => 'Creates pop-up message on load if user inactive',
                                'file' => 'custom/include/customPopUps/custom_popup_js_include.php',
                                'class' => 'CustomPopJs',
                                'function' => 'getAccountJs'
                        )
                )
        );

Custom_popup_js_include.php:

    <?php
    // prevent people from accessing this file directly
    /*if (! defined('sugarEntry') || ! sugarEntry) {
        die('Not a valid entry point.');
    }*/
    class CustomPopJs {
        function getAccountJs($event, $arguments) {
            // Prevent this script from being injected anywhere but the EditView.
            /*if ($_REQUEST['action'] != 'DetailView' ) {
                // 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>';
            echo '<script type="text/javascript" src="custom/include/javascript/sugarwidgets/SugarYUIWidgets.js"></script>';
        }
    }

customPopUpAccounts.js

document.addEventListener('DOMContentLoaded', function() {
    checkUserStatus();
}, false);


function checkUserStatus()
{
    if($("#aq_account_status_c").val() != "Active" )
    {   
        YAHOO.SUGAR.MessageBox.show({msg: 'This account is inactive ', title: 'Inactive Account'} );
    }

}
Community
  • 1
  • 1
swifty
  • 181
  • 2
  • 12
  • The detail view is tough to call events on as (at least in the version I'm using) the text is just there, and there's no HTML id's related to the values which you could call innerHTML on. Have you considered having your after_ui_frame check run an AJAX call? – Reisclef Aug 08 '15 at 16:52
  • I am trying to make it appear in the DetailView using the onLoad functionality? but that also doesn't seem to work for me. – swifty Aug 09 '15 at 01:58
  • Like I said, what you're checking in your JavaScript (if you put the value of account_status_c into console.log) is null as that id doesn't exist in the detail view. It does in the edit view. Do you know how to make an AJAX call in sugar? There may be another way, but it's the only way I know you'd be able to categorically check as you need to check the database value of your field as it's not findable in the DOM and I don't believe the bean is available with an after_ui_frame logic hook. – Reisclef Aug 09 '15 at 05:45
  • Oh! Thanks. now I get it. I am sorry I am new to this. I don't know how to make an AJAX call in sugar yet. I will look into it. But I will appreciate if you could show me how to do it ? Thanks! – swifty Aug 09 '15 at 22:25
  • Would it be possible for you to show how to make an AJAX call in sugar in my case? I tried getting my head around AJAX call in sugar. I am having a bit of tough time solving this. Would really appreciate your help!! – swifty Aug 10 '15 at 07:54
  • Cool, I realised after I posted the above that an actual AJAX call isn't necessary as you're already using the logic hook, give me a few minutes... – Reisclef Aug 10 '15 at 08:45

1 Answers1

1

To summarise the comments, and elaborate:

  • The EditView has several input fields with id attributes which serve as good identifiers for Javascript value getting.
  • However, the detail view does not. It makes a table, and puts the values where it belongs based on the definitions.

Therefore, you need to make a database call to ascertain the account value. I've got the below working on my detail view:

function getAccountJs($event, $arguments) {

    //This is in case the DB connection file isn't included by default  
    require_once 'include/database/DBManagerFactory.php';

    echo '<script type="text/javascript" src="custom/include/javascript/sugarwidgets/SugarYUIWidgets.js"></script>';    
    // Check if the view is Detail View, if so, check the DB if account is inactive
    if ($_REQUEST['action'] == 'DetailView' ) {

        $accountID = $_REQUEST["record"];

        $db =  DBManagerFactory::getInstance();
        $query =    "select aq_account_status_c  from accounts_cstm
                    where id_c = '". $accountID . "'";
        $result = $db->query($query);   
        $row = $db->fetchByAssoc($result);  

        //If the returned row is not "Active" show 
        if ($row['aq_account_status_c'] != "Active") {
            echo "<script>YAHOO.SUGAR.MessageBox.show({msg: 'This account is inactive ', title: 'Inactive Account'} );</script>";
        }
    }

    //If it is Edit view, use the same javascript as before
    if ($_REQUEST['action'] == 'EditView' ) {
        echo '<script type="text/javascript" src="custom/include/customPopUps/customPopUpAccounts.js"></script>';
    }   
}
Reisclef
  • 2,056
  • 1
  • 22
  • 25
  • Thank you so much! This seems to be working now.. The only problem is the popup is still appearing when the account status is 'Active'. I believe this is because I am not using the right condition ? the aq_account_status_c is a DropDown field. Could you help me how to fetch the right value and – swifty Aug 10 '15 at 10:02
  • Ah, right. So if you look at your dropdown list, what is the key and the value? I made mine Active => Active, but it's possible yours is A => Active. If you make the PHP if statement correspond with the KEY value of the dropdown list (which is what is stored) then it should all work well! – Reisclef Aug 10 '15 at 10:05
  • Yes! Thanks again!!! I found the problem. I just checked my database, i mean I check how it was storing the values. It was "active" instead of "Active" and now it works just fine. Again, Thank you so much! I am new to this and learning new things everyday! :) – swifty Aug 10 '15 at 10:14
  • Glad I could help! Sugar is a bit funny sometimes. The most difficult part is learning how best to work within the framework, and when you can't, how to use the best workaround. Good luck going forward! :) – Reisclef Aug 10 '15 at 10:34
  • Hi again! I think I need your help again, but for a very small problem this time. I am trying to check if a checkbox is checked/unchecked in sugar and perform actions accordingly in real-time. This is how I am checking: $(document).ready(function(){ $('#aq_physical_same_as_postal_c').change(function() {if($(this).prop("checked")){ /*do something}*/} else {/*do something}*/}. The problem is It's always picking up whatever is in the else block. This makes me think that the problem is my check/condition? what do you think?. FYI It worked when I tried it on my test html page. – swifty Aug 11 '15 at 06:22
  • This is what I am trying [test code here](https://jsfiddle.net/abhayhans/4e0rczwy/4/) – swifty Aug 11 '15 at 06:34
  • This should be another question. Have a search! This turned up in my search: http://stackoverflow.com/questions/2260888/javascript-if-box-checked – Reisclef Aug 11 '15 at 07:40