Example #1
0
        /// <summary>
        /// Update existing record or create new if doesn't already exist, based on QueryBase given
        /// </summary>
        /// <param name="entity">Record to create or update</param>
        /// <param name="organizationService">IOrganization reference</param>
        /// <param name="query">QueryBase implementation to determine if the record already exists. To update, method expects a single result to be returned</param>
        /// <returns>EntityReference of updated or created record</returns>
        public static EntityReference CreateOrUpdate(this Entity entity, IOrganizationService organizationService,
                                                     QueryBase query)
        {
            var result = query.RetrieveSingleRecord(organizationService);

            if (result == null)
            {
                entity.Id = entity.Create(organizationService).Id;
            }
            else
            {
                entity.Id = result.Id;
                entity.Update(organizationService);
            }

            return(entity.ToEntityReference());
        }
        /// <summary>
        /// Deletes a single record returned from this Query
        /// </summary>
        /// <param name="query">QueryBase implementation (QueryExpression, QueryByAttribute, FetchQuery</param>
        /// <param name="organizationService">IOrganization implementation</param>
        /// /// <exception cref="QueryBaseException">Throws if the query returns more than one record</exception>
        public static void DeleteSingleRecord(this QueryBase query, IOrganizationService organizationService)
        {
            var result = query.RetrieveSingleRecord(organizationService);

            result?.Delete(organizationService);
        }