AnsweredAssumed Answered

Sugar 7 : Creating record through code : how to force id with bean ?

Question asked by Gaelle Fernandez on Apr 19, 2016
Latest reply on Apr 19, 2016 by Caim Astraea

Hello there,


I have a simple question for you :

Is it a way to force the id of a record when creating it through code ?


Let me explain ...

I have a custom module called de_Devis, and i am successfully creating new records through code (scheduled task) via bean like this :

$myNewDevis = new de_Devis(); $myNewDevis->name = "myName"; $myNewDevis->custom_field_c = "My Custom"; $myNewDevis->save();


with the save() function, my newDevis "myName" is created with a GUID like this : aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee.

Ok, that's how sugar works, and most of the time it is enough.

But i am working with external data provider and they have to find the right IDs they already give the datas. So i have to give my new created records an id like -say- AxCBd-AxCBc-AxCBb-AxCBa

So, i've tried to force the id before saving like this :

$myNewDevis = new de_Devis(); $myNewDevis->id = "AxCBd-AxCBc-AxCBb-AxCBa"; $myNewDevis->name = "myName"; $myNewDevis->custom_field_c = "My Custom"; $myNewDevis->save();

But this does not work.


I thought of overriding it after the saving, like

$myNewDevis = new de_Devis(); $myNewDevis->id = "AxCBd-AxCBc-AxCBb-AxCBa"; $myNewDevis->name = "myName"; $myNewDevis->custom_field_c = "My Custom"; $generatedId = $myNewDevis->save(); $myNewDevisWithCustomId = new de_Devis(); $myNewDevisWithCustomId->retrieve($generatedId); $myNewDevisWithCustomId->id = "AxCBd-AxCBc-AxCBb-AxCBa"; $myNewDevisWithCustomId->save();

But this won't work, right ?


So, do you have any idea on how to force custom id when creating new records via Save() ?


Thanks a lot for your time, i'll be really gratefull if you could help me.