public GenericField createGenericField(RightNowCustomObjectFieldAttribute attribute, object value)
        {
            GenericField genericField = new GenericField();

            genericField.name      = attribute.Name;
            genericField.DataValue = new DataValue();

            ItemsChoiceType fieldType = ItemsChoiceType.StringValue;

            if (attribute.FieldType != null)
            {
                fieldType = attribute.FieldType;
            }

            if (fieldType == ItemsChoiceType.NamedIDValue)
            {
                NamedID accountID = new NamedID();
                accountID.ID = new ID();
                long id = 0;
                Int64.TryParse(value.ToString(), out id);
                accountID.ID.id              = id;
                accountID.ID.idSpecified     = true;
                genericField.DataValue.Items = new object[] { accountID };
            }
            else
            {
                genericField.DataValue.Items = new object[] { value };
            }
            //genericField.dataType = (ConnectService.DataTypeEnum)fieldType;
            genericField.DataValue.ItemsElementName = new ItemsChoiceType[] { fieldType };
            return(genericField);
        }
        public void DeleteObjects <T>(List <T> lstObj) where T : class, new()
        {
            List <GenericObject> lstgenericObject = new List <GenericObject>();

            foreach (T sam in lstObj)
            {
                ID autoID = new ID();

                Type objType      = typeof(T);
                var  objAttribute = objType.GetCustomAttributes(typeof(RightNowCustomObjectAttribute), true).FirstOrDefault() as RightNowCustomObjectAttribute;
                if (objAttribute == null)
                {
                    throw new InvalidOperationException("The type provided is not a RightNow custom object type. Please use the RightNowObjectAttribute to associate the proper metadata with the type");
                }

                GenericObject genericObject = new GenericObject();
                RNObjectType  rnObjType     = new RNObjectType()
                {
                    Namespace = objAttribute.PackageName, TypeName = objAttribute.ObjectName
                };
                genericObject.ObjectType = rnObjType;

                List <GenericField> genericFields = new List <GenericField>();
                PropertyInfo[]      properties    = objType.GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    RightNowCustomObjectFieldAttribute attribute = property.GetCustomAttributes(typeof(RightNowCustomObjectFieldAttribute), true).FirstOrDefault() as RightNowCustomObjectFieldAttribute;
                    if (attribute != null && attribute.CanUpdate)
                    {
                        var propValue = property.GetValue(sam, null);
                        //if (!string.IsNullOrWhiteSpace(propValue.ToString())) && !string.IsNullOrWhiteSpace(propValue.ToString())
                        if (propValue != null && !string.IsNullOrWhiteSpace(propValue.ToString()))
                        {
                            genericFields.Add(createGenericField(attribute, propValue));
                        }
                    }
                    else
                    {
                        if (attribute.Name == "ID")
                        {
                            var propValue = property.GetValue(sam, null);
                            autoID.id = Convert.ToInt64(propValue);
                        }
                    }
                }

                genericObject.GenericFields = genericFields.ToArray();

                autoID.idSpecified = true;
                genericObject.ID   = autoID;
                lstgenericObject.Add(genericObject);
            }
            DestroyProcessingOptions options = new DestroyProcessingOptions();

            options.SuppressExternalEvents = false;
            options.SuppressRules          = false;
            RNObject[]      rnobj           = lstgenericObject.ToArray <RNObject>();
            DestroyRequest  destroyRequest  = new DestroyRequest(getClientInfoHeader(), rnobj, options);
            DestroyResponse destroyResponse = getChannel().Destroy(destroyRequest);
        }
        /********************/


        public UpdateResponse UpdateObject <T>(T obj, long uniqueId) where T : class, new()
        {
            UpdateResponse updateResponse = null;

            Logger.Logger.Log.Debug(string.Format("Update : {0}", uniqueId));

            try
            {
                Type objType      = typeof(T);
                var  objAttribute = objType.GetCustomAttributes(typeof(RightNowCustomObjectAttribute), true).FirstOrDefault() as RightNowCustomObjectAttribute;
                if (objAttribute == null)
                {
                    throw new InvalidOperationException("The type provided is not a RightNow custom object type. Please use the RightNowObjectAttribute to associate the proper metadata with the type");
                }

                GenericObject genericObject = new GenericObject();
                RNObjectType  rnObjType     = new RNObjectType()
                {
                    Namespace = objAttribute.PackageName, TypeName = objAttribute.ObjectName
                };
                genericObject.ObjectType = rnObjType;

                List <GenericField> genericFields = new List <GenericField>();
                PropertyInfo[]      properties    = objType.GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    RightNowCustomObjectFieldAttribute attribute = property.GetCustomAttributes(typeof(RightNowCustomObjectFieldAttribute), true).FirstOrDefault() as RightNowCustomObjectFieldAttribute;
                    if (attribute != null && attribute.CanUpdate)
                    {
                        var propValue = property.GetValue(obj, null);
                        //if (!string.IsNullOrWhiteSpace(propValue.ToString())) && !string.IsNullOrWhiteSpace(propValue.ToString())
                        if (propValue != null && !string.IsNullOrWhiteSpace(propValue.ToString()))
                        {
                            genericFields.Add(createGenericField(attribute, propValue));
                        }
                    }
                }

                genericObject.GenericFields = genericFields.ToArray();
                ID autoID = new ID();
                autoID.id          = uniqueId;
                autoID.idSpecified = true;
                genericObject.ID   = autoID;

                UpdateProcessingOptions options = new UpdateProcessingOptions();
                options.SuppressExternalEvents = false;
                options.SuppressRules          = false;
                UpdateRequest updateRequest = new UpdateRequest(getClientInfoHeader(), new RNObject[] { genericObject }, options);
                updateResponse = this.getChannel().Update(updateRequest);
            }
            catch (Exception ex)
            {
                Logger.Logger.Log.Error("RightNowObject:", ex);
            }
            return(updateResponse);
        }
        public CreateResponse CreateObjects <T>(List <T> lstobj)
        {
            List <GenericObject> lstgenericObject = new List <GenericObject>();

            foreach (T sam in lstobj)
            {
                Type objType      = typeof(T);
                var  objAttribute = objType.GetCustomAttributes(typeof(RightNowCustomObjectAttribute), true).FirstOrDefault() as RightNowCustomObjectAttribute;
                if (objAttribute == null)
                {
                    throw new InvalidOperationException("The type provided is not a RightNow custom object type. Please use the RightNowObjectAttribute to associate the proper metadata with the type");
                }

                GenericObject genericObject = new GenericObject();
                RNObjectType  rnObjType     = new RNObjectType()
                {
                    Namespace = objAttribute.PackageName, TypeName = objAttribute.ObjectName
                };
                genericObject.ObjectType = rnObjType;

                List <GenericField> genericFields = new List <GenericField>();
                PropertyInfo[]      properties    = objType.GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    object attribute = property.GetCustomAttributes(typeof(RightNowCustomObjectFieldAttribute), true).FirstOrDefault();
                    if (attribute != null)
                    {
                        RightNowCustomObjectFieldAttribute rightNowAttribute = attribute as RightNowCustomObjectFieldAttribute;
                        if (rightNowAttribute.CanUpdate)
                        {
                            var propValue = property.GetValue(sam, null);
                            if (propValue != null && !string.IsNullOrWhiteSpace(propValue.ToString()))
                            {
                                genericFields.Add(createGenericField(rightNowAttribute, propValue));
                            }
                        }
                    }
                }

                genericObject.GenericFields = genericFields.ToArray();
                lstgenericObject.Add(genericObject);
            }

            CreateProcessingOptions options = new CreateProcessingOptions();

            options.SuppressExternalEvents = false;
            options.SuppressRules          = false;
            RNObject[] rnobj = lstgenericObject.ToArray <RNObject>();

            CreateRequest  createRequest   = new CreateRequest(getClientInfoHeader(), rnobj, options);
            CreateResponse createResponcse = this.getChannel().Create(createRequest);

            return(createResponcse);
        }