Results 1 to 8 of 8

Thread: Import Function in Custom Modules

  1. #1
    simpsond is offline Member
    Join Date
    May 2007
    Posts
    7

    Unhappy Import Function in Custom Modules

    I have a problem I've not been able to overcome:

    After creating several new modules with Olavo's Module Builder (MB), I tried to import records in from a CSV file. No problem for the default SugarCRM-supplied modules. But NONE of the ones I created with MB even have an Import button. I've tried to code it, but I only get as far as a "blank" button (icon but no text saying "Import"). Clicking that takes me to the "Step 1" screen, which also shows up blank. I've reviewed the code in the /modules/Import directory, but my code mods do nothing. I'm afraid if I make any more mods to the import code, I risk breaking existing code.

    Can anyone point me in the right direction? I'm not a professional programmer, and I'm starting to feel I may have hit the proverbial brick wall with my current coding abilities.
    Last edited by simpsond; 2007-07-05 at 04:56 PM.

  2. #2
    simpsond is offline Member
    Join Date
    May 2007
    Posts
    7

    Unhappy Re: Import Function in Custom Modules

    I'm surprised. I've posted this question in two different Sugar forums and no one has responded at all. Is importing an unusual question or a difficult solution? This doesn't bode well for my little project

  3. #3
    ponce is offline Junior Member
    Join Date
    Jul 2007
    Location
    Blacksburg, VA, USA
    Posts
    4

    Default Re: Import Function in Custom Modules

    Hi,

    For the text next to the import button, you'll have to edit the language file(s) in your modules/<Module Name>/language directory. Insert a 'LBL_IMPORT' entry in that file. Then in menu.php, you'll add an entry in addition to the ones already there. It should look something like this:

    PHP Code:
    if(ACLController::checkAccess('<Module Name>''import'true))$module_menu[] =Array("index.php?module=<Module Name>&action=Import&step=1&return_module=<Module Name>&return_action=index"$mod_strings['LBL_IMPORT'],"Import"'<Module Name>'); 
    Now create an Import.php file in your module directory, and that's where you can start coding your own import script. I'm not familiar with using the default sugar import code, I can't help you there. I created my own import script.

    I hope that helps!

  4. #4
    simpsond is offline Member
    Join Date
    May 2007
    Posts
    7

    Default Re: Import Function in Custom Modules

    OK, I have a partial success!

    The import button shows up now, and the label correctly appears.

    However, clicking it still takes me to a blank screen. Do I have to write a custom module in the /modules/import folder? If so, where would I start?

    Thanks, so far!

  5. #5
    simpsond is offline Member
    Join Date
    May 2007
    Posts
    7

    Default Re: Import Function in Custom Modules

    Quote Originally Posted by ponce
    Now create an Import.php file in your module directory, and that's where you can start coding your own import script. I'm not familiar with using the default sugar import code, I can't help you there. I created my own import script.
    Whoops... I didn't notice this part of your reply. Going to take a look at the import.php structure.

  6. #6
    simpsond is offline Member
    Join Date
    May 2007
    Posts
    7

    Default Re: Import Function in Custom Modules

    Never mind. The index.php file already exists in the module folder. All it does is include the /import/index.php file, which in turn references the /import/config.php. I'm still getting a blank screen that says "Import Step 1: Select the Source".

    Looking at the /import/config.php file. It looks like this:

    PHP Code:
    #import_bean_map = array(
      
    'contacts' => 'ImportContact'
     
    ,'leads' => 'ImportLead' 
    etc., for several modules. I'm guessing I need to add something like

    PHP Code:
      'mymodules' => 'ImportMyModule' 
    but I'm not sure where to go from there. Is there a 'bean' reference I have to create elsewhere?

    Sorry for so many questions
    Last edited by simpsond; 2007-07-05 at 10:24 PM.

  7. #7
    ponce is offline Junior Member
    Join Date
    Jul 2007
    Location
    Blacksburg, VA, USA
    Posts
    4

    Default Re: Import Function in Custom Modules

    This may not be the best way to do it, but this is how I did it.

    I didn't use any of the modules/Import/ code at all. I removed the statements that included the Import/index.php file. Instead I created my own code to import my custom module's data. I imported a .csv file by creating a loop and using the fgetcsv function to read one line at a time. Then I created a new Bean (ex. $contactbean = new Contact(); ). Then I set the bean's fields one by one from the array returned by fgetcsv. Then simply call the Bean's save() function. If you have your vardefs set up correctly, that's all you need to do.

    Using Contacts as an example, this would look like:
    PHP Code:
    // First line in a CSV is usually the names of the fields.
    $headers = array();
    $record fgetcsv$csv5000 );
    foreach ( 
    $record as $idx => $fieldname )
    {
        
    // $record is an array with the index as the key, and the field name as the value.
        // Flipping the two around and storing in another array allows us to store future
        // values simply by referencing the field name, not the index.
        
    $headers[$fieldname] = $idx;
    }

    // Begin the main import loop
    $record fgetcsv$csv5000 );
    while ( 
    $record !== false )
    {
        
    /// Create bean
        
    $contactbean = new Contact();

        
    // Import each field
        
    $contactbean->first_name record[$headers['First Name']];
        
    $contactbean->last_name record[$headers['Last Name']];
        
    $contactbean->phone_home record[$headers['Home Phone #']];
        
    // repeat until all your fields are set


        
    $contactbean->save();

        
    // Get the next record
        
    $record fgetcsv$csv5000 );

    SugarCRM is not an easy software to develop for, at first. There is a steep learning curve, so there will be many proverbial brick walls. It is nice to have a PHP debugger, to help understand the Sugar code.

    I wish you the best of luck!

  8. #8
    simpsond is offline Member
    Join Date
    May 2007
    Posts
    7

    Default Re: Import Function in Custom Modules

    That's an interesting solution. So you put all that code in your import.php in the /modules/modulename folder? I'll give that a try -- you're right about Sugar being a difficult platform to code for. I'm not really a programmer, so I'm sort of learning this as I go.

    Thanks for your last post, ponce. I'll give it a try and let you know how it works.
    Last edited by simpsond; 2007-07-06 at 05:15 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Custom loadable modules relationships
    By mbarlow310 in forum Developer Help
    Replies: 1
    Last Post: 2007-07-02, 05:34 PM
  2. Import Custom fields... delete existing data
    By frb in forum Developer Help
    Replies: 2
    Last Post: 2006-12-07, 07:53 AM
  3. Replies: 0
    Last Post: 2006-08-31, 06:23 PM
  4. Asterisk Patch 1.1.0 Crash on logon
    By skyracer in forum Help
    Replies: 6
    Last Post: 2006-07-08, 06:30 AM
  5. Replies: 7
    Last Post: 2006-06-06, 07:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •