Refresh / Reload a field

Hello

I have a logic hook that changes a field’s value, but in order to see the change I need to refresh the whole page… I want it to refresh the field in real time.

So I’m trying to accomplish something like this(https://suitecrm.com/suitecrm/forum/suitecrm-7-0-discussion/21178-refresh-sub-panel-values-when-another-sub-panel-is-updated#75194)

But instead of reloading a whole subpanel through the controller.php I’m trying to find a way to refresh a single field.

Can anyone advise what method needs to be used to reload a field?

For example to reload a subpanel it’s

showSubPanel(‘SUBPANEL_NAME’,null,true);

But what is the JS method that refreshes a single field?

Maybe for a single field you can just grab the value and put it in the field with your own Javascript.

Yep, it’s a good idea, I’m just concerned if it will be upgrade safe in case the markup ever changes in the future and it’s not the same setup (unlikely but still)

The markup shouldn’t be a problem, it is based on field names, for example the “Department” field:


<div class="col-xs-12 col-sm-8 detail-view-field inlineEdit" type="varchar" field="department">
    <span class="sugar_field" id="department"></span>
    <div class="inlineEditIcon col-xs-hidden">
        <span class="suitepicon suitepicon-action-edit"></span>
    </div>
</div>

This is genericly generated HTML, coming from tpl files (templates) with loops for each field.

Of course, to be upgrade-safe you need to make sure the actual overriding the view to inject HTML is done in an upgrade-safe way.

1 Like

Figured out how to do it from the controller.php when a new subpanel item is added.

Here’s the whole controller.php and the 5 minute video that explains what’s actually happening:



class un_inventoryController extends SugarController {

    /**
     *
     */
    function action_SubPanelViewer() {



    require_once 'include/SubPanel/SubPanelViewer.php';


// only if this is creation of new sale under accounts, refresh the screen so the salerow subpanel will be refreshed too
if ( array_key_exists('module', $_REQUEST) && array_key_exists('subpanel', $_REQUEST) && array_key_exists('action', $_REQUEST) &&
$_REQUEST['module'] == 'un_inventory' && $_REQUEST['subpanel'] == "un_inventory_leads_1" && $_REQUEST['action'] == "SubPanelViewer") {


    write_to_log(array("request" => $_REQUEST), "all conditions filled, custom controller called", true);

    // Get the ID of the inventory unit so we can fetch the new status_c field and update the field right away (otherwise we'll have to refresh the page

    $inventory = BeanFactory::getBean($_REQUEST["module"], $_REQUEST["record"]);
    $inventory_status_c = ucwords(str_replace("_", " ", $inventory->status_c));
    $field_to_update = "status_c";

    $js=<<<EOQ
<script>

    // Update the status
    $( document ).ready(function() { 
                
        document.querySelector('div[type="enum"][field="$field_to_update"]').innerHTML = "$inventory_status_c";
        
    });


</script>
EOQ;

    echo $js;


    }

}
}

?>
1 Like

Great video, once again! The only part I don’t like is the title mentioning that old thing called “SugarCRM” :wink:

Since you’re doing these videos, I notice you probably don’t have XDEBUG set up and connected to your PHP Storm? If you don’t, you really should. Sometimes it’s a pain to set up, especially for remote debugging, but it’s really worth the trouble, it’s a game changer for development.

1 Like

Thanks I’ll check it out! Already ended up buying phpstorm thanks to your advice! :slight_smile:

To refresh a detailView through a logic hook, you can use an easy “hack”.

In your logic_hook.php, place the following js code as your last “echo” before your return or exit:

echo "<script>location.reload();</script>";

Simple but effective