/// <summary> /// Gets Salesforce objects. /// </summary> /// <param name="queryString">Query string.</param> /// <returns>List of Salesforce objects.</returns> public List <T> GetObjects <T>(string queryString) { var result = new List <T>(); if (CheckConnected()) { QueryResult qr = _binding.queryAll(queryString); while (qr.size > 0) { foreach (sObject sObject in qr.records.ToList()) { result.Add(Constructor.ConstructEntity(typeof(T), sObject.Any)); } qr.size = 0; if (!qr.done) { qr = _binding.queryMore(qr.queryLocator); } } } return(result); }
/// <summary> /// Gets Salesforce objects. /// </summary> /// <param name="fieldNames">Fields for selection.</param> /// <param name="whereClause">Where clause.</param> /// <returns>List of entities.</returns> public List <T> GetObjects <T>(List <string> fieldNames, string whereClause = null) where T : SalesforceEntity { var result = new List <T>(); if (this.CheckConnected()) { // Check parameters if (fieldNames == null) { throw (new ArgumentNullException("fieldNames")); } // Field names should not be empty if (fieldNames.Count > 0) { // Add ID field to query if it not exists if (!fieldNames.Exists(q => q.Equals(ID_FIELD_NAME, StringComparison.InvariantCultureIgnoreCase))) { fieldNames.Insert(0, ID_FIELD_NAME); } // Build query string string queryString = String.Format("select {0} from {1}", String.Join(", ", fieldNames), typeof(T).Name); // Append where clause if (!String.IsNullOrEmpty(whereClause)) { queryString = String.Format("{0} where {1}", queryString, whereClause); } // Try to get query result QueryResult qr = this._binding.queryAll(queryString); if ((qr.done) && (qr.size > 0)) { foreach (sObject sObject in qr.records.ToList()) { // Convert sObject to enitity result.Add(Constructor.ConstructEntity(typeof(T), sObject.Any)); } } } else { throw (new InvalidOperationException(ERR_FIELDS_ARE_EMPTY)); } } return(result); }
/// <summary> /// Recursively constructs entity from sObejct. /// </summary> /// <param name="entityType"></param> /// <param name="sObject"></param> /// <returns></returns> public static dynamic ConstructEntity(Type entityType, XmlElement[] any) { // Check parameters if (entityType == null) { throw (new ArgumentNullException("entityType")); } if (any == null) { throw (new ArgumentNullException("any")); } // Create instance of specified type dynamic entity = Activator.CreateInstance(entityType); // Convert sObject to entity of specified type foreach (XmlElement node in any) { // Try to get property by sObject fieldname PropertyInfo propertyInfo = entity.GetType().GetProperty(node.LocalName); if ((propertyInfo != null) && (!node.IsEmpty && node.NodeType == XmlNodeType.Element)) { // Get property type Type propertyType = propertyInfo.PropertyType; // Resovle node type (simple value or complex object) if (node.HasAttributes) // complex field with type (ex - sf:sObject) attribute { propertyInfo.SetValue(entity, Constructor.ConstructEntity(propertyType, node.ChildNodes.Cast <XmlElement>().ToArray()), null); } else // simple field with no type (ex - sf:sObject) attribute { Type underlyingType = Nullable.GetUnderlyingType(propertyType); if (underlyingType != null) { propertyInfo.SetValue(entity, Convert.ChangeType(node.InnerText, underlyingType), null); } else { propertyInfo.SetValue(entity, Convert.ChangeType(node.InnerText, propertyType), null); } } } } return(entity); }
/// <summary> /// Gets Salesforce objects. /// </summary> /// <param name="whereClause">Where clause.</param> /// <returns>List of entities.</returns> public List <T> GetObjects <T>(string whereClause = null) where T : SalesforceEntity { var result = new List <T>(); if (this.CheckConnected()) { // Extract current field names var fieldNames = Extractor.ExtractFieldNames(typeof(T)); // Field names should not be empty if (fieldNames.Count > 0) { // Build query string string queryString = String.Format("select {0} from {1}", String.Join(", ", fieldNames), typeof(T).Name); // Append where clause if (!String.IsNullOrEmpty(whereClause)) { queryString = String.Format("{0} where {1}", queryString, whereClause); } // Try to get query result QueryResult qr = this._binding.queryAll(queryString); if ((qr.done) && (qr.size > 0)) { // Convert sObjects to entities foreach (sObject sObject in qr.records.ToList()) { result.Add(Constructor.ConstructEntity(typeof(T), sObject.Any)); } } } else { throw (new InvalidOperationException(ERR_FIELDS_ARE_EMPTY)); } } return(result); }