AnsweredAssumed Answered

I cannot pass any info to a bean object in an after_save action for logic hook

Question asked by John John on Mar 6, 2014
Latest reply on Mar 6, 2014 by Jason Eggers
Well... This is my issue, all i simply want to do is when a user enters a unformatted ssn# in my field ssn_c
i want it to pass a formatted version to my field ssnformat_c


This is what i have done so far, i have created a line in my logic_hooks.php array as such.

<?php
// Do not store anything in this file that is not part of the array or the hook version.  This file will
// be automatically rebuilt in the future. 
 $hook_version = 1; 
$hook_array = Array(); 
// position, file, function 
$hook_array['before_save'] = Array(); 
$hook_array['before_save'][] = Array(1, 'Contacts push feed', 'modules/Contacts/SugarFeeds/ContactFeed.php','ContactFeed', 'pushFeed'); 
$hook_array['before_save'][] = Array(77, 'updateGeocodeInfo', 'custom/modules/Contacts/ContactsJjwg_MapsLogicHook.php','ContactsJjwg_MapsLogicHook', 'updateGeocodeInfo');
$hook_array['after_ui_frame'] = Array(); 
$hook_array['after_ui_frame'][] = Array(1, 'Contacts InsideView frame', 'modules/Connectors/connectors/sources/ext/rest/insideview/InsideViewLogicHook.php','InsideViewLogicHook', 'showFrame'); 
$hook_array['after_save'] = Array(); 
$hook_array['after_save'][] = Array(77, 'updateRelatedMeetingsGeocodeInfo', 'custom/modules/Contacts/ContactsJjwg_MapsLogicHook.php','ContactsJjwg_MapsLogicHook', 'updateRelatedMeetingsGeocodeInfo'); 
$hook_array['after_save'][] = Array(1, 'Update Portal', 'custom/modules/Contacts/updatePortal.php','updatePortal', 'updateUser'); 

/************ This is my added hook*************/
$hook_array['after_save'][]=Array(2, 'ssnformat', 'custom/modules/Contacts/ssnformat.php', 'ssnformatclass', 'ssnformatdo');

?>
And this is my logic hook code in ssnformat.php

<?php
if(!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');
class ssnformatclass {
    
function ssnformatdo($bean, $event, $arguments){
$bean->custom_fields->retrieve();
$bean->ssnformat_c = preg_replace("/^(\d{3})(\d{2})(\d{4})$/", "$1-$2-$3", $bean->ssn_c);
 
    }
}
?>
    
So it takes a nine digit text XXXXXXXXX
and copies it with format to ssnformat_c XXX-XX-XXXX

so when I edit a contact and save it, nothing is placed in my ssnformat_c field

While testing I decided to check my code is working so i replaced all my logic code to just this.


<?php
//prevents directly accessing this file from a web browserif(!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');
class ssnformatclass {
    
function ssnformatdo($bean, $event, $arguments){
$bean->custom_fields->retrieve();
$bean->ssnformat_c = '123-12-1234';
 
    }
}
?>

And once i edit and save a contact.....nothing happens

So i decided to try it as a before_save hook with code

<?php
//prevents directly accessing this file from a web browser
if(!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');
class ssnformatclass {
    
function ssnformatdo($bean, $event, $arguments){
$bean->ssnformat_c = preg_replace("/^(\d{3})(\d{2})(\d{4})$/", "$1-$2-$3", $bean->ssn_c);
 
    }
}
?>
    
And nothing happens, this is expected because i know i read somehwere you cannot grab data from a field that has not been saved yet....so i tried just to see if all my other code is working, with a before_save aray

<?php
//prevents directly accessing this file from a web browser
if(!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');
class ssnformatclass {
    
function ssnformatdo($bean, $event, $arguments){
$bean->ssnformat_c = '123-12-1234':
 
    }
}
?>


i edit and save my contact and usre enough the ssnformat_c field is populated with the text

So my issue is driving me crazy

I understand that in order to use custom field data on an after_save i need

$bean->custom_fields->retrieve();

as the first line in my logic

and then when i try to simply edit the field with

$bean->ssnformat_c = 'sometext';

Nothing happens

yet when i do it on a before save....it saves no problem

I just need a gentle push as i have been grinding out this issue for days and need help understanding the problem with my logic or coding would help me with such a useful tool as logic hooks.


I will Much apreciate the help!

I am using sugar 6.5 ce
I am just getting into php coding and only have experience with a small amount of python, and c++ and much experience with html and css


-John

Outcomes