New ID for new record in before_save

Hi, here’s the problem:
I have a logic_hook called at the time of saving a new record, more precisely in before_save. Here, I need to change the ID of this record:

$bean->new_with_id = true;
$bean->id=$new_code;

Of course, I do checks on this ID, taking the last one entered, so $new_code will contain the last value entered+1. I didn’t insert the line

$bean=newBean("MODULE");

because I’m in before_save(), the bean is already being created.

So far so good, it works, but when I save a related field (present in the form), it is not saved, I have to reopen the form and insert that field again. This doesn’t happen if I don’t modify the ID:

// $bean->new_with_id = true;
// $bean->id=$new_code;

Does anyone know how to fix this? Thanks.

Version 7.10.20

Sugar Version 6.5.25 (Version 344)

I think the problem is due to the relationship table between the two modules, in the field …ida is stored the original ID value and not the one I am going to modify, I tried to change it:

$bean->...ida=$new_code

but it doesn’t work, do you know how to do it?

It seems that the relationship is already created when the code gets to the logic hook…

I suppose you could simply run a direct SQL UPDATE query and change things in that relationship table.

I wanted to avoid directly manipulating the DB, is there no way to edit a field in the relationship?

Possibly there is, though I wanted to do that the other day and couldn’t find out how…

But editing the id of the relationship, I don’t think there is a way, no…

I don’t want to modify the id of the relation, but only the ida field of that relation

How about breaking the relationship to the old bean and creating a new relation to the new one? That’s essentially what the ida field is doing there

I was actually trying to do this

$bean_account->load_relationship('relationship');
$id_account=$bean_account->'relationship'->get();

foreach ( $id_account as $id_rel ) {
        if($id_rel==$bean->_ida){
                   $bean_account->'relationship'->delete($id_rel,$bean);
			$bean_account->'relationship'->add($bean);
        }

but surely I’m doing something wrong because nothing happens, not even error

Are you using a debugger? (you should)

You could trace that into the SugarBean code and see what happens. It’s probably ignoring changes to the ids

The moment I enter before_save, the IDA field is not set, there is only IDB, that’s why when I search for relationships it doesn’t find anything.
Manually setting

$bean->_ida=$new_code;

and printing the $bean to the screen, the changes exist, but if I go to look in the DB, that field has been populated with a different ID, the default one.
I have no idea why or how to fix it.

Me neither! But my post above was about how to get an idea and find out :slight_smile:

1 Like

Update:
I moved all the logic into after_relationship_add. In the before_save the relationship was not yet saved, now I have the relationship created which I delete, after which I edit my ID as desired and add it to the relationship.
The problem is that now in the DB, every time I save a record, I have two records, one with alphanumeric ID whose field deleted=1 and the other with my ID.
Unfortunately I didn’t have any other ideas to solve the problem, if there is any better idea I will gladly listen to it, for now it works.

1 Like

That sounds good - or at least, good enough :slight_smile:

I wouldn’t have any problems with using a direct SQL query right there, to permanently delete the row with the “deleted=1”, to avoid cluttering the database.

If I remember correctly, after a certain amount of time, deleted=1 are permanently deleted from Suite, right? Or do they remain indefinitely? If not, I’ll have to do a SQL DELETE, although I’m taking it easy on a direct delete.

That is true if you have the “purge” job enabled, which is disabled by default.

Then you need to check if that job actually deletes these records, because it does some kinds of purges, but not others. I would bet that it never touches relationship tables.

How do I check if I have the “purge” job enabled?

Look in Admin / Schedulers for a “Prune” job (it’s not called purge, my mistake)

And this is the code that runs:

→ https://github.com/salesagility/SuiteCRM/blob/master/modules/Schedulers/_AddJobsHere.php#L299

Yes, I found it in the schedule list, but it’s inactive, as you said, I don’t know what it does, if it deletes the deleted reports as well.
If I operate directly on the DB, can I delete only that record from the relationship table? Or do I necessarily have to delete some other records from other tables?

Note that you might have two rows, not just one, for each relationship: one with your id as ida, the other as idb.

If you delete that, you break the relationship. That’s it. You might leave some orphaned records behind, or some loose ends, but that depends on application-level considerations (on what you’re doing with those records, your business flows etc).

So, if you know these are new relationships, or new records, and not a complex web of related records, you can set things up just as you want with some direct SQL.

Thanks pgr, there are 2 rows stored, one with the ID given by Suite and the other with the ID given by me, I can delete the one with the Suite ID and at the end only my row is left