# Creating entity in other workspace

**URL:** <https://community.viktor.ai/t/creating-entity-in-other-workspace/1969>\
**Category:** Need help? Ask here!\
**Created:** [22 July 2024 12:05 UTC](https://community.viktor.ai/t/creating-entity-in-other-workspace/1969 "2024-07-22T12:05:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bart.vosslamber](https://community.viktor.ai/letter_avatar_proxy/v4/letter/b/3e96dc/32.png) [@bart.vosslamber](https://community.viktor.ai/u/bart.vosslamber)\
**Post date:** [22 July 2024 12:05 UTC](https://community.viktor.ai/t/creating-entity-in-other-workspace/1969/1 "2024-07-22T12:05:21Z")

</div>

Hi Support,  
We would like to use an existing application within a new application. Therefore we would like to use the extended API (new app is running on viktor 14.13.0). I can find the method `api.entity_compute()` but this method only works for entitys that are already created. I can also find the method `api.create_child_entity()` but I think this method can only create a child within the workspace that is used.

Is there a way by using the extended API to create a new child in an existing (external) workspace, fill it with params, and run a method from the application controller?

Kind regards,  
Bart Vosslamber

---

<div class="post-metadata">

**Author:** ![rdejonge](https://community.viktor.ai/letter_avatar_proxy/v4/letter/r/ba8739/32.png) [@rdejonge](https://community.viktor.ai/u/rdejonge)\
**Post date:** [22 July 2024 12:45 UTC](https://community.viktor.ai/t/creating-entity-in-other-workspace/1969/2 "2024-07-22T12:45:58Z")

</div>

Hi Bart,

You can use `workspace_id` in [create\_child\_entity()](https://docs.viktor.ai/sdk/api/api-v1/#API.create_child_entity) as an optional argument to create entities outside the context of the app.

The method `entity_compute()` is defined on the [Workspace](https://docs.viktor.ai/sdk/api/api-v1/#Workspace.entity_compute) class, not on the `API` class. You can retrieve workspace objects using [API().get\_workspace()](https://docs.viktor.ai/sdk/api/api-v1/#API.get_workspace).

Regards,

Raoul

---

<div class="post-metadata">

**Author:** ![bart.vosslamber](https://community.viktor.ai/letter_avatar_proxy/v4/letter/b/3e96dc/32.png) [@bart.vosslamber](https://community.viktor.ai/u/bart.vosslamber)\
**Post date:** [23 July 2024 07:17 UTC](https://community.viktor.ai/t/creating-entity-in-other-workspace/1969/3 "2024-07-23T07:17:56Z")

</div>

Hi Raoul,

Thanks for the reply. I have tried to run a correct computation. Therefore I have used the following test script within the application:

```auto
# initialize the viktor API()
api = API(
    token='TOKEN_CREATED_IN_VIKTOR_ENVIRONMENT',
    environment='heijmans.viktor.ai',
)

# section name
section_name = f'NEW_SECTION_TO_BE_CREATED'

# create an entity in the correct workspace with the section_name
api.create_child_entity(
    parent_entity_id=48417,
    workspace_id=7,
    entity_type_name='RectangularCrossSectionCalculator',
    name=section_name,
    params=set_with_params, # dict containing the correct params
)

# get the correct workspace 
cross_section_calculator = api.get_workspace(id_=7)

# search the correct entity within the workspace
for project in cross_section_calculator.get_entity(id_=9619).children():
    if project.id == 48416:
        for object in project.children():
            if object.id == 48417:
                for child in object.children():
                    if child.name == section_name:
                        results = cross_section_calculator.entity_compute(
                            entity_id=child.id,
                            method_name='update_m_n_tables',
                            params=set_with_params,
                        )
                        print(results)

```

When I run the script, a correct entity is created within the external application. I can go to that entity and run it manually without errors. But the `.entity_compute()` method doesn’t run properly. I recieve the following error message:

```auto
2024-07-23 09:06:45.096 ERROR : Exception is raised
Traceback (most recent call last):
  File "viktor_connector\\connector.pyx", line 295, in connector.Job.execute
  File "viktor\core.pyx", line 2006, in viktor.core._handle_job
  File "viktor\core.pyx", line 1943, in viktor.core._handle_job._handle_button
  File "C:\Users\bavo6\PycharmProjects\HI-LandhoofdApp\app\Abutment\controller.py", line 1681, in get_scia_results_from_scia_file_upload
    results = cross_section_calculator.entity_compute(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "viktor\api_v1.pyx", line 781, in viktor.api_v1.Workspace.entity_compute
  File "viktor\api_v1.pyx", line 1321, in viktor.api_v1._API.entity_compute
  File "viktor\api_v1.pyx", line 1111, in viktor.api_v1.Job.get_result
viktor.errors.ComputeError: Computation failed with an error (type: error_code, details: )

```

Can you help me?

---

<div class="post-metadata">

**Author:** ![kvangiessen](https://community.viktor.ai/user_avatar/community.viktor.ai/kvangiessen/32/14_2.png) [@kvangiessen](https://community.viktor.ai/u/kvangiessen)\
**Post date:** [23 July 2024 10:08 UTC](https://community.viktor.ai/t/creating-entity-in-other-workspace/1969/4 "2024-07-23T10:08:42Z")

</div>

Hi Bart,

I’m not yet sure what the error is that you are running into, but I can at least provide some tips on how to slightly simplify your code:

```python
# initialize the viktor API()
api = API(
    token='TOKEN_CREATED_IN_VIKTOR_ENVIRONMENT',
    environment='company.viktor.ai',
)

# create an entity in the correct workspace with the section_name
new_section_entity = api.create_child_entity(
    parent_entity_id=48417,
    workspace_id=7,
    entity_type_name='RectangularCrossSectionCalculator',
    name=f'NEW_SECTION_TO_BE_CREATED',
    params=set_with_params, # dict containing the correct params
)
cleaned_params = new_section_entity.last_saved_params # cleans params, including any missing values
results = new_section_entity.compute(method_name='update_m_n_tables', params=cleaned_params)
print(results)

```

The error basically means that an uncaught exception occurred during the computation. This could have a few potential sources:

- The provided params were incomplete, which could cause a Attribute error
- The provided params were incorrect, which lead to a processing error.
- Other resources needed in the computation were not available. (unlikely)

You could try debugging this set-up, by running the app of the other workspace in your development environment (don’t forget to update the `workspace_id` and `parent_entity_id` to your development workspace ids) and then running your above piece of code in a separate python script (not in the same app, as this could cause racing conditions).

I hope this helps.

---

<div class="post-metadata">

**Author:** ![bart.vosslamber](https://community.viktor.ai/letter_avatar_proxy/v4/letter/b/3e96dc/32.png) [@bart.vosslamber](https://community.viktor.ai/u/bart.vosslamber)\
**Post date:** [23 July 2024 10:58 UTC](https://community.viktor.ai/t/creating-entity-in-other-workspace/1969/5 "2024-07-23T10:58:33Z")

</div>

Hi Kevin,  
This works, lot of thanks!

---

<div class="post-metadata">

**Author:** ![kvangiessen](https://community.viktor.ai/user_avatar/community.viktor.ai/kvangiessen/32/14_2.png) [@kvangiessen](https://community.viktor.ai/u/kvangiessen)\
**Post date:** [23 July 2024 11:23 UTC](https://community.viktor.ai/t/creating-entity-in-other-workspace/1969/6 "2024-07-23T11:23:58Z")

</div>

Great to hear.

That probably means that the `set_with_params` that you initially provided in the `compute()` function was missing an attribute/value somewhere.

By using the ‘cleaned\_params’ as described above, we ensure that any missing values in the params get filled with their default or empty values.
