How to refresh Detail View within logic hook?

I have logic hook which changes custom field on Accounts after add/delete relationship to Contacts (simply I count related Contacts).
Everything works fine but after logic I need to refresh DetailView to show proper value of updated custom field.

Any help ?

<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not a valid entry point');
    
    
    class contacts_after_relationship_add
    {
 	function after_relationship_add_delete ($bean, $event, $arguments)
        {
		if ($arguments['module'] == "Accounts" && $arguments['related_module'] == "Contacts"){

			$account = new Account(); 
			$account->retrieve($_REQUEST['record']); 
			$contacts = $account->get_linked_beans('contacts','Contact'); 
			$a = 0;
			foreach ( $contacts as $contact ) { 
			    $a++;
			}
			
			$bean->amg_kontakty_coun_c = "$a";	
			$bean->save();
        	}
	}   
    }

Hello maciej,

Can you specify a bit more you process? It’s a bit confuse.

Where is the hook? Where are the user when adding/deleting the relation?

Regards,

At the account details view, I have a custom field “Number of contacts”
My hook is after_relationship_add and after_relationship_delete
After add/remove contact relations on the Contacts subpanel, I save the current number of contacts
My goal is to refresh the “Number of contacts” value on detail view.
Of course, when I go to another view and return to the account view, I see the correct value, but my goal is to show the actual value immediately after adding / removing the relationship.

I see 2 ways on doing your request.

1st
In your hook make a redirect. (make your this is the last hook)

2nd
With Javascript create a handler you your buttons and use that handler to make your page refresh or even only that field

I would prefer the 2nd one

I’ve been working through adding a after_relationship_add logic hook and I saw this behavior when I had a php error in my logic hook code that was causing the php to halt mid-step. Once I cleared up the php coding issues the page would refresh on adding a related record. Try adding some debug statements like

$GLOBALS['log']->fatal('***Some debug text***');

to your logic hook’s class function so you can see how far your code is progressing by watching your suitecrm.log after you trigger the code.

Load a js file in your DetailView for Accounts and set it to reload the view once the save button for Contacts is clicked.

Something like


<script>
$(document).ready(function(){
    $('#contact_save_button_name').on('click' , function(){
           location.reload(true);
     });
});
</script>
2 Likes

also looking for this… another solution is to reload the whole page automatically on subpanel update. This is done by adding a custom controller.php

details here: https://suitecrm.com/suitecrm/forum/suitecrm-7-0-discussion/21178-refresh-sub-panel-values-when-another-sub-panel-is-updated

Video on that page shows the script, just replace what’s between script tags with “window.location.reload();” and it will reload the whole page instead of a subpanel. I’m still looking how to update just a field and will come back with an answer if I find it.

1 Like

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

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

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

Simple but effective

where would that code go ?