Assistance regarding API results?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abidoss
    Senior Member
    • Mar 2023
    • 228

    Assistance regarding API results?

    Hello friends, I have a JSON file with this information. When I received the file, I wanted to create leads with the information from this JSON content, but I see that it only created a single line with the first ID

    json

    PHP Code:
    
    [
      {
        "userId": "4",
        "lastname": "TEST",
        "firstname": "Ahmed"
      },
      {
        "userId": "7",
        "lastname": "TEST",
        "firstname": "Mehdi"
      },
      {
        "userId": "16",
        "lastname": "TEST",
        "firstname": "SARA",
    
      },
    ]
    Attached Files
  • yuri
    Member
    • Mar 2014
    • 8440

    #2
    I added the ability to parse the whole JSON with empty path for the next hotfix release https://github.com/espocrm/espocrm/issues/2880.

    You will be able to put the JSON into a variable. It will be an array. Then by using while statement, you can call record\create for every item. You will also need to use object\get, example: $id = object\get($item, 'id');

    If you need to call an API endpoint to create records in a different instance, you can utilize BPM with multi-instance sub-processes (instances per each parsed item).
    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

    • abidoss
      Senior Member
      • Mar 2023
      • 228

      #3
      I used this but I get error

      $data = json\encode($_lastHttpResponseBody, true);

      $userId = object\get($data, 'userId');

      description = $userId


      [2023-10-27 19:41:38] ERROR: Workflow[6535a3db68e8691bd]: Action failed [executeFormula] with cid [3], details: function: object\get, index: 1, should be: object. ​
      Last edited by abidoss; 10-27-2023, 07:44 PM.

      Comment

      • yuri
        Member
        • Mar 2014
        • 8440

        #4
        On v8.0.4

        Code:
        $list = json\retrieve(workflow\lastHttpResponseBody());
        
        $i = 0;
        
        while ($i < array\length($list)) {
            $item = array\at($list, $i);
        
            $userId = object\get($item, 'userId'); 
            $lastName = object\get($item, 'lastname');
            $firstName = object\get($item, 'firstname');
        
            record\create('Lead', 'firstName', $firstName, 'lastName', $lastName, 'userId', $userId);
         
            $i = $i + 1;
        }
        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

        • abidoss
          Senior Member
          • Mar 2023
          • 228

          #5
          Thanks Yuri, it works now

          Comment

          • abidoss
            Senior Member
            • Mar 2023
            • 228

            #6
            Let's say I have 8000 IDs in my JSON file that I initially created using "record/create." After 2 days, my JSON file contains 8200 IDs. How can I add only the additional 200 IDs without it recreating all 8200 IDs and ensuring that it checks for existing records with the same name?

            Comment

            • rabii
              Active Community Member
              • Jun 2016
              • 1250

              #7
              You just have to loop through the ids and compare them to existing ones in your db and create new ones, below is an example:

              PHP Code:
              $list = json\retrieve(workflow\lastHttpResponseBody());
              
              $i = 0;
              
              while ($i < array\length($list)) {
                  
                  $item = array\at($list, $i);
                  
                  $id = object\get($item, 'id');
                  
                  if (record\exists('YOUR_ENTITY_TYPE', 'id=', $id)) {
                      continue;
                  }
                      
                  record\create('YOUR_ENTITY_TYPE', 'id', $id);
               
                  $i = $i + 1;
              }​​ 
              
              Rabii
              Web Dev

              Comment


              • abidoss
                abidoss commented
                Editing a comment
                Good news, I tried this code, and the workflow doesn't give me any errors, and it doesn't recreate all the identifiers again. So far, that's good, but it doesn't update the rest of the identifiers. If I have 8000, it still stays at 8000 with your code.
            • rabii
              Active Community Member
              • Jun 2016
              • 1250

              #8
              Hey,

              Try this instead:

              PHP Code:
              $list = json\retrieve(workflow\lastHttpResponseBody());
              
              $i = 0;
              
              while ($i < array\length($list)) {
                  
                  $item = array\at($list, $i);
                  
                  $id = object\get($item, 'id');
                  
                  if (!record\exists('YOUR_ENTITY_TYPE', 'id=', $id)) {
                      record\create('YOUR_ENTITY_TYPE', 'id', $id);
                  }
               
                  $i = $i + 1;
              }
              Rabii
              Web Dev

              Comment


              • abidoss
                abidoss commented
                Editing a comment
                Thank you, Rabii. I will try.
            Working...