How to insert the record using beans ??

Hi All ,

i am able to to update the record and delete the record using bean method but i am not able to insert the data using bean my code for updating record are as below


$lead = new Lead();
	$lead->retrieve($id);
	$lead->email1 = $subscriber->email;
	$lead->save();

I am trying to insert the record like below


$bean = BeanFactory::newBean('Lead');   //Create bean
$bean->name = 'Example Record';   //Populate bean fields
$bean->save();   //Save
$record_id = $bean->id;  //Retrieve the bean id

can you please guide me on this ?? any one admins ???

maybe you should use ‘Leads’ instead of ‘Lead’ (note the letter ‘s’ that is missing in the module name.:

BeanFactory::newBean(‘Leads’);

PS: I haven’t tried this but I believe that the module is called Leads and not Lead

Yes but while updating the data we are creating object of Lead class so don’t we have to use the same ??/

As I said I haven’t tried it and what you say does make sense: why would the name of the class be different with respect to the name of the module?

In any case I gave an untested suggestion for you to try. Please post back the result of your test so that everyone can benefit from your findings in the future.

yes it worked :wink:

i Think for adding the records through beans we need to use name of the module and for updating name of the class

so it worked like this

Adding


$bean = BeanFactory::newBean('Leads');   //Create bean  using module name 
$bean->name = 'Example Record';   //Populate bean fields
$bean->save();   //Save
$record_id = $bean->id;  //Retrieve the bean id

And For updating


$lead = new Lead();                                                                 / / object of class
	$lead->retrieve($id);                                                       / /   retrieve the bean  
	$lead->email1 = $subscriber->email;                            / /    update it 
	$lead->save();                                                              / /     save it 

Going to point out that using the following is some what the old way of loading the bean and the record required.

$lead = new Lead();
$lead->retrieve($id);

and this can be done using the BeanFactory using the following code.

$bean = BeanFactory::getBean(‘Leads’, $id );

however you can use either way both work but if you are hsing the BeanFactory to create the record I would use the BeanFactory to update the record to keep it simple.

Regards,

Ian.

Thank you for your quick reply can you please share us , how can we add record ( if i don’t use the bean:factory )

i have tried like this to insert the record with creating object

$lead = new Lead();
$lead->first_name = ’ mr. ';
$lead->last_name = ’ bean ’ ;
$lead->save();

but it did not work , so i was following bean:factory method to insert the record

did i miss any thing in above code

Thank you for your time and quick reply

As far as I am aware that should create a new lead. I very rarely use this method and tend to use the BeanFactory to load and retrieve beans.

It is recommend that the Bean Factory is used for SugarCRM versions 6.3 onwards.

Regards,

Ian

1 Like

Thank you lan really appreciate your quick reply and suggestion

1 Like