Announcement

Collapse
No announcement yet.

Creating multiple entities in one API call

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating multiple entities in one API call

    Hello devs!

    I have installed Espo version 7.5.5 and set up a node JS implementation of API calls with HMAC authorisation as described here. I can now create leads, contacts, etc as well as custom entities defined in entity manager via the API. However I need to create a few entities in one API call, so I've written a custom module as described here.

    In Custom\Espo\Modules\MyModule I have:

    Api\MyAction
    Controllers\MyController
    Binding
    MyService1

    I'm using the Binding class to attach the MyService1 service to the container. The MyAction API is successfully calling the MyService1 where I am going to put the application logic. (I think this is the right way to do it)

    I want the custom API to create a contact, then create a product registration record containing a Serial number and another ID and link the two records. I think I need to do something like what is described in CRUD actions:

    PHP Code:
    <?php

    namespace Espo\Custom\Controllers;

    use 
    Espo\Core\Api\Request;
    use 
    Espo\Core\Api\Response;

    class 
    MyController {

         
    // Creates a record.
         
    public function postActionCreate(Request $requestResponse $response): void {}

    }
    (the above is pulled straight from the documentation but I was going to put this into my service class as I believe we are supposed to keep controllers lean).

    My problem is how to tell postActionCreate what kind of record I want to create. I assume this method decides the record type from the the path section of the URL (eg /contact), but mine is a custom URL with something like ContactProductRegistration. Is there a way that I can tell the postActionCreate to create a contact and then call it again to create the custom product registration entity and then call something to link the two together, or do I need to do something more custom?

    Many thanks,
    Clare

  • #2
    Hi Clare,

    You can pass parameters in JSON payload and then read it from the response: $response->getParsedBody()->myParameter. This assumes that you will use a single API endpoint.

    BTW, it's not necessary to use Binding when passing a service class to a controller. DI can resolve a dependency automatically if it's defined as a class rather an interface.

    Comment


    • #3
      Hi Yuri,

      Thanks for the response. I am already passing data in the payload but I don't think this can tell postActionCreate what kind of record to create can it?

      I want to call postActionCreate once to create a contact, a second time to create a product registration record and then create a relationship between the two records. Is there a way I can specify the record type to the function? Or do I need to do it some other way?

      Cheers,
      Clare

      Comment


      • #4
        It's OK, I figured it out. I need to use the entity manager.

        Cheers,
        Clare

        Comment

        Working...
        X