Skip to content

Leads bean is reset before final parent display - going through Basic::getEmailAddressId() #9640

Open
@vaudoin

Description

@vaudoin

Issue

In modules containing emails (like Leads or Contacts) , the bean is reset before completing view.

I discovered this issue while I was trying to change a custom field on Leads, custom view.detail.php (display fonction). The change was lost after parent::display(); was called.

Expected Behavior

Any custom field change done on custom view.detail.php should be kept after calling parent display.

Actual Behavior

I discovered on Basic::getEmailAddressId(), called in modules with emails that the bean is reset with the call of $this->retrieve();
That induces all changes made on view.detail.php to be removed.

Possible Fix

Basic.php fonction getEmailAddressId()
I don't see the point to call retrieve() here but I might miss something. And I guess the definite fix should be more deeply analysed, to check for side effects.
But here is the fix that works, at least for me.

private function getEmailAddressId($emailField)
{
	$log = LoggerManager::getLogger();

	$this->validateSugarEmailAddressField($emailField);
	$emailAddress = $this->cleanUpEmailAddress($this->{$emailField});

	if (!$emailAddress) {
		$log->warn('Trying to get an empty email address.');
		return null;
	}

	// List view requires us to retrieve the mail so we can see the email addresses
	
	// VAU : clone $this to not call retrieve on it (that resets content)
//      if (!$this->retrieve()) {
	$copieOfThis = clone $this;
	if (!$copieOfThis->retrieve()) {

		$log->fatal('A Basic can not retrive.');
		return null;
	}
	
	$found = false;
	// VAU : call $addresses on $copieOfThis
//		$addresses = $this->emailAddress->addresses;
	$addresses = $copieOfThis->emailAddress->addresses;
	foreach ($addresses as $address) {
		if ($this->cleanUpEmailAddress($address['email_address']) === $emailAddress) {
			$found = true;
			$emailAddressId = $address['email_address_id'];
			break;
		}
	}

	if (!$found) {
		// Changed exception to error as demo data is never selected.
		$log->fatal('A Basic bean has not selected email address. (' . $emailAddress . ')');
		return null;
	}

	return $emailAddressId;
}

Steps to Reproduce

  1. Add a custom view.detail.php on Leads module.
  2. On display() function add a change on a custom field, for instance
function display()
{
  $this->bean->my_custom_field_c = 'THE TEST';
  parent::display();
}
  1. The change is lost on the view.

Context

We could probably discuss

Your Environment

  • SuiteCRM Version used: 7.12.4
  • Environment name and version: PHP 7.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: ModuleIssues & PRs related to modules that do not have specific labelPriority:ImportantIssues & PRs that are important; broken functions, errors - there are workaroundsStatus:Needs AssessedNeeds the core team to assessType: BugBugs within the core SuiteCRM codebase

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions