/// <summary> /// Updates Salesforce object. /// </summary> /// <param name="entity">Entity.</param> /// <returns>Operation result.</returns> public bool UpdateObject(SalesforceEntity entity) { bool result = false; if (this.CheckConnected()) { // Check parameters if (entity == null) { throw (new ArgumentNullException("entity")); } // Extract current values of entity Dictionary <string, IConvertible> values = Extractor.ExtractFieldValues ( entity, new ExtractionOptions() { ExcludeUpdateIgnored = true } ); // Values should not be empty if (values.Count > 0) { // Construct Salesforce object if (values.ContainsKey(ID_FIELD_NAME)) { SaveResult[] saveResults = this._binding.update ( new sObject[] { Constructor.ConstructSObject(entity.GetType().Name, values) } ); result = ((saveResults.Length > 0) && (saveResults[0].success)); } else { throw (new InvalidOperationException(ERR_ID_FIELD_IS_NOT_SET)); } } else { throw (new InvalidOperationException(ERR_FIELDS_ARE_EMPTY)); } } return(result); }
/// <summary> /// Extracts entity's properties values. /// </summary> /// <param name="entity">Entity instance.</param> /// <param name="options">Extract options.</param> /// <returns>Dictionary of property name - property value.</returns> public static Dictionary <string, IConvertible> ExtractFieldValues(SalesforceEntity entity, ExtractionOptions options = null) { var result = new Dictionary <string, IConvertible>(); // Get public properties PropertyInfo[] properties = entity.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); // Get properties values foreach (PropertyInfo property in properties) { // Check for ignored properties bool ignoreProperty = false; if (options != null) { var ignoredAttributes = Extractor.GetIgnoreAttributes(property); if (options.ExcludeGetIgnored) { ignoreProperty = (ignoredAttributes.Any(x => x.GetType() == typeof(IgnoreForGetAttribute))); } else if ((options.ExcludeCreateIgnored) || (options.ExcludeUpdateIgnored)) { if (ignoredAttributes.Any(x => x.GetType() == typeof(IgnoreForCreateUpdateAttribute))) { ignoreProperty = true; } else if (options.ExcludeCreateIgnored) { ignoreProperty = (ignoredAttributes.Any(x => x.GetType() == typeof(IgnoreForCreateAttribute))); } else if (options.ExcludeUpdateIgnored) { ignoreProperty = (ignoredAttributes.Any(x => x.GetType() == typeof(IgnoreForUpdateAttribute))); } } } // Ignore property if needed if (!ignoreProperty) { // Get property value object propertyValue = property.GetValue(entity, null); if (propertyValue != null) // if value is not null { string resultValue = propertyValue.ToString(); Type underlyingType = Nullable.GetUnderlyingType(property.PropertyType); if (underlyingType != null) // if has nullable type { if (underlyingType == typeof(DateTime)) { resultValue = (propertyValue as DateTime?).Value.ToString("s"); } } else // if not nullable type { if (property.PropertyType == typeof(DateTime)) { resultValue = (propertyValue as DateTime?).Value.ToString("s"); } } // Add property name - value to result result.Add(property.Name, resultValue); } else { // Add null value to result result.Add(property.Name, null); } } } return(result); }