# Add-ERPObject Cmdlet to create a new entity and transfers it to an ERP-System. ## Syntax ```{code-block} powershell :linenos: true Add-ERPObject [[-EntitySet] ] [[-Properties] ] [] ``` ## Parameters | Type | Name | Description | Optional | |----------------------|------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------| | String | EntitySet | The EntitySet name where the item is located.
It is also possible to specify additional namespaces or the whole url (e.g MaterialService/Materials, ) | no | | Hashtable / PsObject | Properties | The properties for the entity being created | no | ## Return type [Entity](/code_reference/commandlets/objects/entity) ← on success\ **empty** ← on failure. Exception/ErrorMessage can be accessed using [\$Error](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-5.1#error).\ If the cmdlet fails due to error responses returned by the ERP system, the \$Error variable provides a [WebRequestException](/code_reference/net_library/webrequestexception) . ## Remarks This commandlet is used to create an entity in the ERP-System. ## Examples In the following examples we are using the public OData Services () for demonstration purposes: **Create an orange Juice** ```{code-block} powershell :linenos: true Connect-Erp -Service "http://services.odata.org/V4/OData/(S(fa2g1e4kjczcfnog0asxmvhh))/OData.svc" $entity = Add-ERPObject -EntitySet "Products" -Properties @{ "ID "= 5; "Name" = "Orange Juice"; "Description" = "The original Orange Juice. Refreshing!"; "ReleaseDate" = "2006-08-04T00 =00 =00Z"; "DiscontinuedDate" = $null; "Rating" = 3; "Price" = 22.8 } ``` **Create a person with details (deep create)** ```{code-block} powershell :linenos: true Connect-Erp -Service "http://services.odata.org/V4/OData/(S(fa2g1e4kjczcfnog0asxmvhh))/OData.svc" $entity = Add-ERPObject -EntitySet "Products" -Properties @{ "ID" = 3, "Name" = "Augustin Hodorsson", "PersonDetail" = @{ "PersonID" = 3, "Age" = 23, "Gender" = $true, "Phone" = "(307) 555-4680123" } } ``` **Create a Product entity using** [New-ErpObject](/code_reference/commandlets/new-erpobject) ```{code-block} powershell :linenos: true Connect-Erp -Service "http://services.odata.org/V4/OData/(S(fa2g1e4kjczcfnog0asxmvhh))/OData.svc" $product = New-ERPObject -EntityType 'Product'-Properties @{"ID"=11;"Name"="Forst beer"} Add-ERPObject -EntitySet "Products" -Properties $product <# ID : 11 Name : Forst beer Description : ReleaseDate : 01.01.0001 00:00:00 +00:00 DiscontinuedDate : Rating : 0 Price : 0 #> ``` **Error handling, analyze the** [AggregateException](https://docs.microsoft.com/en-us/dotnet/api/system.aggregateexception?view=netframework-4.5.1) **why the entity could not be created due to several errors, by using \$Error** ```{code-block} powershell :linenos: true Connect-ERP "https://services.odata.org/V3/(S(fa2g1e4kjczcfnog0asxmvhh))/OData/OData.svc" $entity = Add-ERPObject -EntitySet "PersonDetails" -Properties @{"Age"=$true;"Gender"="Female"; "Photo"="This is a Photo"} if(-not $entity){ $Error[0].Exception #System.AggregateException: "One or more errors occurred." $Error[0].Exception.InnerExceptions <# Following mandatory properties in entity 'PersonDetail' are missing: 'PersonID' Passed value for entity 'PersonDetail' for property with name 'Photo' has a wrong data type 'String', but should be of type 'Stream'. Passed value for entity 'PersonDetail' for property with name 'Gender' has a wrong data type 'String', but should be of type 'Boolean'. #> } ```