Insert accounts in Sugar CRM using Codeigniter

//load new database of Sugar CRM as defined in database.php
        $this->db2 = $this->ci->load->database('db_crm', TRUE);
 
        //generate unique id for sugar crm . length:36
        $accounts_id=$this->generateCrmID('accounts');
 
        //insert account into CRM
        $account_data=array(
            'id'=> $accounts_id,
            'name'=> $user_details['first_name'].' '.$user_details['last_name'],
            'date_entered'=> date('Y-m-d h:i:s'),
            'date_modified'=> date('Y-m-d h:i:s'),
            'modified_user_id'=> '417785b3-1644-6586-11f7-4b2a02c9fede',
            'created_by'=> '417785b3-1644-6586-11f7-4b2a02c9fede',
            'description'=> strip_tags($content),
            'deleted'=> 0,
            'assigned_user_id'=> '417785b3-1644-6586-11f7-4b2a02c9fede',
            'account_type'=> 'Customer',
            'industry'=> $user_details['industry'],
            'annual_revenue'=> '',
            'phone_fax'=> '',
            'billing_address_street'=> $user_details['address1']." ".$user_details['address2'],
            'billing_address_city'=> $user_details['city'],
            'billing_address_state'=> $user_details['state'],
            'billing_address_postalcode'=> $user_details['zip'],
            'billing_address_country'=> $user_details['country'],
            'rating'=> '',
            'phone_office'=> $user_details['mobile'],
            'phone_alternate'=> $user_details['phone'],
            'website'=> $user_details['website'],
            'ownership'=> '',
            'employees'=> '',
            'ticker_symbol'=> '',
            'shipping_address_street'=> '',
            'shipping_address_city'=> '',
            'shipping_address_state'=> '',
            'shipping_address_postalcode'=> '',
            'shipping_address_country'=> '',
            'parent_id'=> '',
            'sic_code'=> '',
            'campaign_id'=> '');
 
        $this->db2->insert('accounts',$account_data);
 
        $email_id=$this->generateCrmID('email_addresses');
        $email_data=array(
            'id' => $email_id,
            'email_address' => $user_details['email'] ,
            'email_address_caps' => strtoupper($user_details['email'] ),
            'invalid_email' => 0,
            'opt_out' => 0,
            'date_created' => date('Y-m-d h:i:s'),
            'date_modified' => date('Y-m-d h:i:s'),
            'deleted' => 0
        );
        $this->db2->insert('email_addresses',$email_data);
 
        $email_addr_id=$this->generateCrmID('email_addr_bean_rel');
        $email_addr_bean_rel_data=array(
            'id' => $email_addr_id,
            'email_address_id' => $email_id,
            'bean_id' => $accounts_id,
            'bean_module' => 'Accounts',
            'primary_address' => 0,
            'reply_to_address' => 0,
            'date_created' => date('Y-m-d h:i:s'),
            'date_modified' => date('Y-m-d h:i:s'),
            'deleted' => 0,
        );
        $this->db2->insert('email_addr_bean_rel',$email_addr_bean_rel_data);
 
        //load default database
        $this->ci->load->database('default', TRUE);

Dependent function(s)

function generateCrmID($condition)
    {
 
        do
        {
            $crm_id = "CONX".random_string('unique');
 
            switch($condition)
            {
                case 'accounts':
                    $query = $this->ci->db->get_where('accounts', array('id' => $crm_id));
                    break;
                case 'email_addresses':
                    $query = $this->ci->db->get_where('email_addresses', array('id' => $crm_id));
                    break;
                case 'email_addr_bean_rel':
                    $query = $this->ci->db->get_where('email_addresses', array('id' => $crm_id));
                    break;
                default:
                    $query = $this->ci->db->get_where('accounts', array('id' => $crm_id));
                    break;
            }
            //ticketmaskid FROM '". TABLE_PREFIX ."' WHERE '' = '". $dbCore->escape() ."';");
            $id=$this->ci->db->count_all();
 
        } while ($id!=0);
 
        return $crm_id;
 
    }
Category(s): CodeIgniter, PHP
Tags: , ,

One Response to Insert accounts in Sugar CRM using Codeigniter

  1. Thanks for sharing this information, I don’t know about anybody else, but I can totally use it.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

 

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">