5 Replies Latest reply on Nov 5, 2019 12:00 AM by Fiona Guy-Oracle

    How to create an entity instance programmatically

    AnnieFisher

      Does anyone know how to create an entity instance programmatically?  Based on selection on a form I need instances of and entity created, the data of which will come from an AJAX call.

       

      I can see we can do CustomEntityCollect controls (see previous post) but this is not exactly what I want to do.  I just want to know the syntax for instance.add method, or whatever it is.

       

      I can see drilling into the interview object that there is a method called generateEntityInstance but there is not documentation anywhere on this, or any other way to add an entity instance via code.  It must be able to be done surely?

        • 2. Re: How to create an entity instance programmatically
          AnnieFisher

          Hi Fiona,

          Apologies if I wasn't clear - I want to retrieve a set of values via another data source i.e not known at run time so plain language rules cannot apply here.  I have to do it using AJAX and our products endpoint.

           

          The use case is this:

           

          User is presented with a list of products. This list comes from a specific endpoint we query and is stuffed into a drop down list in the interview.  This first part is working fine.

           

          The second part is when the user selects 1 or more products and clicks a shopping cart image, an entity instance with a relationship to the Global entity (in this case the Shopper) should be created.  More than 1 entity instance will be created. 

           

          How do I create an entity instance in code?  Something like:

           

          interview.setValue("product", "shopper" "{'id' : '123456' 'name' : 'baked beans'};)

           

          in the C# object world this might look like

           

          foreach (Product p in selectedProducts)

          {

             Product product = new Product();

             product.id = "123456";

             product.name = "baked beans";

           

             shoppingBasket.add(product);

          }

           

          Please help, I've googled and I've searched and theres nothing to be found.  There must be a way to do this otherwise what would be the point of the customEntityCollect extension??

           

          Thanks

          • 3. Re: How to create an entity instance programmatically
            Fiona Guy-Oracle

            Ah - to paraphrase: you want to have a multi-select list which creates an entity instance for each selection in that list.

             

            OPA doesn't have the concept of a multi-select list. Entity instances are usually added one at a time in a tabular or portrait layout (add one child, add the next child etc), and you can control that layout using the customEntityCollect interview extension. Usually people use the extension to change the styling slightly. I don't know whether it can be used the way you are asking. Hopefully one of the gurus out there can suggest if it can.

             

            Otherwise;

            • If you have flexibility to redesign the screen, present it as a single-select list which is repeated for each entity instance; or
            • Create instances for all products, then list all products on screen and use a standard control or an interview extension to set an attribute on each instance (the product is selected) or a relationship (the product is a member of the selected products). Similar to the Product Recommendation example project 'Suitable Products' screen but make it look more like a multi-select list.
            • Use a more standard tool to manage your shopping interface (OPA is not typically used for this) and call OPA behind the scenes to do any order validation.

             

            Hope that gets you a bit closer. 

             

            Fiona

            • 4. Re: How to create an entity instance programmatically
              AnnieFisher

              Hi Fiona,

              The multi select wasn't really the problem, it was more that I wanted to load in a dynamic list of products and have a user select from a table which they'd like to buy.  I have a 3rd party JS library to do that, I just needed the code to be able to add the entity instances and the relationships.

               

              I nearly gave up but I finally got there and I posted in this thread Here's my working example of how to create an entity instance programmatically  what I did and the example project.  Please feel free to take a look, it was a labor of love!

               

              Cheers,

              Annie

               

              p.s you had it right with the Product Recommendation example, except that used a known list of products.  I did not have that luxury, my list was retrieved from an API endpoint at the beginning of the interview (quite a common thing I would have thought)

              • 5. Re: How to create an entity instance programmatically
                Fiona Guy-Oracle

                Well I'm glad you got it working! Thanks for sharing your learnings

                 

                Just looking at your example, which is great, thank you, I have heard of partners building similar user experiences to this; one did it through duplicating the data in the connected application (SvC in this case), and another built a custom connector (which can connect to multiple endpoints), and then using standard mid-interview load / filtered lists controls.

                I can't remember anyone else keeping the standard connector and fetching entity instances from a 3rd location - so you may well be the first!  

                 

                I'd love other people to share their experiences though - if this is a common scenario we can certainly add an example to the documentation.

                 

                Fiona