/// <summary> /// Creates an entity in Exact Online /// </summary> /// <param name="entity">Entity to create</param> /// <returns>True if succeed</returns> public async Task <Tuple <Boolean, T> > CreateAsync(T entity) { var supportedActions = GetSupportedActions(entity); if (!supportedActions.CanCreate) { throw new Exception("Cannot create entity. Entity does not support creation. Please see the Reference Documentation."); } // Get Json code var created = false; var converter = new EntityConverter(); var emptyEntity = Activator.CreateInstance <T>(); var json = converter.ConvertObjectToJson(emptyEntity, entity, _entityControllerDelegate); // Send to API var response = await _conn.PostAsync(json); if (!response.Contains("error")) { created = true; // Set values of API in account entity (to ensure GUID is set) response = ApiResponseCleaner.GetJsonObject(response); var ec = new EntityConverter(); entity = ec.ConvertJsonToObject <T>(response); // Try to add the entity to the managed entities collections if (!AddEntityToManagedEntitiesCollection(entity)) { throw new Exception("This entity already exists"); } // Check if the endpoint supports a read action. Some endpoints such as PrintQuotation only support create (POST). if (supportedActions.CanRead) { // Get entity with linked entities (API Response for creating does not return the linked entities) entity = GetEntity(GetIdentifierValue(entity), _expandfield); } } return(new Tuple <bool, T>(created, entity)); }