API payload question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SpeedBullet
    Senior Member
    • Mar 2016
    • 123

    API payload question

    I'm trying to add Accounts via the API.

    These are recognized and imported correctly:

    PHP Code:
    'name' => $data[7],
    'website' => $data[10],
    'type' => $data[32],
    'industry' => $data[19],
    'sicCode' => $data[11],
    ..... and a couple more 
    

    I can successfully create an account, but is it possible that the following payload keys/values aren't recognized?

    PHP Code:
    'createdAt' => $data[1],
    'modifiedAt' => $data[2],
    'createdById' => '1',
    'modifiedById' => '1',
    'assignedUserId' => '1' 
    
    Is there a solution to import accounts with their creation dates? How to define the modifiedById?
    Last edited by SpeedBullet; 08-31-2016, 07:05 PM.
  • yuri
    Member
    • Mar 2014
    • 8485

    #2
    Only inside framework it's possible to specify createById and createdAt

    PHP Code:
    $this->getEntityManager()->saveEntity($entity, array('import' => true, 'noStream' => true, 'noNotifications' => true)); 
    
    It's better to write script inside application and run.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • SpeedBullet
      Senior Member
      • Mar 2016
      • 123

      #3
      That's bad news... :s I was trying this as a proof of concept to import notes into accounts (with their original dates). I'm migrating data out of another CRM into espocrm and I need to move the "notes" / "meetings" / "calls" etc... from the old CRM to espocrm with their dates.

      I was hoping to do this via api calls via an external php script.

      How do I write custom code inside espocrm and run it?

      Comment

      • yuri
        Member
        • Mar 2014
        • 8485

        #4
        You can use api as well, but createdBy and createdAt will be lost.

        You create php file in root directory

        PHP Code:
        <?php
        
        $sapiName = php_sapi_name();
        if (substr($sapiName, 0, 3) != 'cli') {
            die("Cron can be run only via CLI"); // this for security to avoid running via http
        }
        
        
        include "bootstrap.php";
        
        $app = new \Espo\Core\Application();
        
        $app->setupSystemUser();
        
        $container = $app->getContainer();
        
        $entityManager = $container->get('entityManager');
        
        // here parse your import data, it can be CSV
        
        foreach ($rowList as $row) {
          $entity = $entityManager->getEntity('Account');
          $entity->set('field1', $row['field1'];
          $entity->set('field2', $row['field2'];
          $entityManager->saveEntity($entity, array('import' => true, 'noStream' => true, 'noNotifications' => true));  
        }
        then run your script
        php script_name.php
        Last edited by yuri; 09-02-2016, 08:47 AM.
        If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

        Comment

        • SpeedBullet
          Senior Member
          • Mar 2016
          • 123

          #5
          Thanks this is extremely helpful! I've managed to import the bulk of what needs to be done. Still left with the notes/mails but that will be for tomorrow. Kind regards.

          Comment

          • SpeedBullet
            Senior Member
            • Mar 2016
            • 123

            #6
            yuri : So what I managed to do is this: I adapted your code example to do the following:
            • Open CSV file
            • Transpose CSV data into php array
            • Process each array row and "create" entities.
            • DONE: Accounts, Contacts, Leads, Opportunities, Tasks
            • TODO: For each Account, Contact, Lead, Opportunity: Create note and attach to its relation/parent (account or contact or opportunity, etc...)

            It seems like the notes are a bit harder to import via the above code. Any pointers on how to do this? I checked the database and analysed the structure, but I don't really know which parameters to set. I need to add notes (I have them ready in CSV) to certain contacts. How to do this via entitymanager in the example code you provided?

            Comment

            • SpeedBullet
              Senior Member
              • Mar 2016
              • 123

              #7
              Oh yeah! I've finally imported the notes after some more study of the database structure and a test note to see the behaviour. Worked fine! One thing though, I have imported all my notes in alphabetical order. The notes are now displayed in the wrong order. Like this:

              Today: Todays note
              1jan2016: old imported note
              1feb2016: old imported note
              1aug2016: old imported note

              Today's note correctly displays at the top, but the rest are in the wrong order (I think it's sorted by ID and not by date).

              Today: Todays note
              1aug2016: old imported note
              1feb2016: old imported note
              1jan2016: old imported note

              Could this be fixed in a future version please?

              Comment

              Working...