Created_by in BeanFactory don't work

Hi! I’m creating a bean for a custom module and when I pass a user ID to assign it to “created_by”, it doesn’t take it. The strange thing is that the same ID, if I assign it to “assigned_user_id”, it takes it and shows it. Does anyone know why this is? I’m using the BeanFactory to do this, but it’s not saving anything to the database. Thanks!

Well, I wouldn’t be able to explain why, but the solution was the following:

I saved the bean without assigning the “created_by” property, then I retrieved it using BeanFactory::getBean('module','id'), and assigned the created_by property to that bean, saved it again and it worked.

The function that saves the Bean has different code paths whether it’s a new bean or an existing one, so it’s understandable that your workaround had different results.

It would be interesting to examine the code in SugarBean and know, in the “new bean” code path, where exactly it’s getting the created_by user name information from. It’s not the created_by field, that you have uncovered already. But it’s getting it from somewhere, probably the logged in user.

1 Like

You should try
$bean->set_created_by = false;
before saving the new bean. (Replace $bean by the appropriate variable)

2 Likes