private void GetStateStatusPicklist(ref ServiceObject so)
        {
            SourceCode.SmartObjects.Services.ServiceSDK.Objects.Method meth = so.Methods[0];
            //K2CRMHelper helper = new K2CRMHelper();
            WizardFunctions helper = new WizardFunctions();
            CRMPicklist picklist = new CRMPicklist();
            picklist.Config = crmconfig;

            try
            {
                picklist.EntityLogicalName = NotNull(so.Properties["EntityLogicalName"].Value);

                RestResponse<CRMPicklist> response = helper.CRMGetStateStatus(picklist, config);

                so.Properties.InitResultTable();
                foreach (CRMPicklistOption ret in response.Data.Picklist)
                {
                    for (int c = 0; c < meth.ReturnProperties.Count; c += 1)
                    {
                        Property prop = so.Properties[meth.ReturnProperties[c]];
                        prop = SetGetPicklistOptionProperties(prop, ret);
                    }
                    so.Properties.BindPropertiesToResultTable();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private void RetrieveMultiple(ref ServiceObject so)
        {
            SourceCode.SmartObjects.Services.ServiceSDK.Objects.Method meth = so.Methods[0];
            //K2CRMHelper helper = new K2CRMHelper();
            WizardFunctions helper = new WizardFunctions();
            CRMRetrieveMultiple multiple = new CRMRetrieveMultiple();
            multiple.Config = crmconfig;

            try
            {
                multiple.LinkFromEntityName = NotNull(so.Properties["LinkFromEntityName"].Value);
                multiple.LinkFromAttributeName = NotNull(so.Properties["LinkFromAttributeName"].Value);
                multiple.LinkToEntityName = NotNull(so.Properties["LinkToEntityName"].Value);
                multiple.LinkToAttributeName = NotNull(so.Properties["LinkToAttributeName"].Value);
                multiple.ConditionAttributeName = NotNull(so.Properties["ConditionAttributeName"].Value);
                multiple.ConditionOperator = NotNull(so.Properties["ConditionOperator"].Value);
                multiple.ConditionValue = NotNull(so.Properties["ConditionValue"].Value);
                multiple.ReturnAttributes = NotNull(so.Properties["ReturnAttributes"].Value);

                RestResponse<CRMRetrieveMultiple> response = helper.RetrieveMultiple(multiple, config);

                so.Properties.InitResultTable();

                foreach (CRMRetrieveMultipleReturn ret in response.Data.Results)
                { 
                    for (int c = 0; c < meth.ReturnProperties.Count; c += 1)
                    {
                        Property prop = so.Properties[meth.ReturnProperties[c]];
                        prop = SetRetrieveMultipleProperties(prop, response);
                        prop = SetRetrieveMultipleReturnProperties(prop, ret);
                    }
                    so.Properties.BindPropertiesToResultTable();
                }
                

            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private void GetAllEntities(ref ServiceObject so)
        {
            SourceCode.SmartObjects.Services.ServiceSDK.Objects.Method meth = so.Methods[0];
            WizardFunctions helper = new WizardFunctions();
            CRMEntityList entitymetadata = new CRMEntityList();
            entitymetadata.Config = crmconfig;

            try
            {
                RestResponse<CRMEntityList> response = helper.GetAllEntities(entitymetadata, config);

                so.Properties.InitResultTable();
                foreach (CRMEntityMetadata ret in response.Data.Entities)
                {
                    for (int c = 0; c < meth.ReturnProperties.Count; c += 1)
                    {
                        Property prop = so.Properties[meth.ReturnProperties[c]];
                        prop = SetEntityMetadataProperties(prop, ret);
                    }
                    so.Properties.BindPropertiesToResultTable();
                }
            }
            catch (Exception ex)
            {
                throw;
            }                       
        }
        private void GetEntityAttributes(ref ServiceObject so)
        {
            SourceCode.SmartObjects.Services.ServiceSDK.Objects.Method meth = so.Methods[0];
            //K2CRMHelper helper = new K2CRMHelper();
            WizardFunctions helper = new WizardFunctions();
            CRMEntityMetadata entitymetadata = new CRMEntityMetadata();
            entitymetadata.Config = crmconfig;

            try
            {
                entitymetadata.LogicalName = NotNull(so.Properties["EntityLogicalName"].Value);
                entitymetadata.IncludeAttributes = true;
                RestResponse<CRMEntityMetadata> response = helper.GetEntityMetadata(entitymetadata, config);

                so.Properties.InitResultTable();
                foreach (CRMAttribute ret in response.Data.Attributes)
                {
                    for (int c = 0; c < meth.ReturnProperties.Count; c += 1)
                    {
                        Property prop = so.Properties[meth.ReturnProperties[c]];
                        prop = SetGetEntityAttributeProperties(prop, ret);
                    }
                    so.Properties.BindPropertiesToResultTable();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private void GetCRMUser(ref ServiceObject so)
        {
            SourceCode.SmartObjects.Services.ServiceSDK.Objects.Method meth = so.Methods[0];
            WizardFunctions helper = new WizardFunctions();
            CRMUser user = new CRMUser();
            user.Config = crmconfig;

            try
            {
                user.UserFQN = NotNull(so.Properties["UserFQN"].Value);

                RestResponse<CRMUser> response = helper.GetCRMUser(user, config);

                so.Properties.InitResultTable();

                for (int c = 0; c < meth.ReturnProperties.Count; c += 1)
                {
                    Property prop = so.Properties[meth.ReturnProperties[c]];
                    prop = SetCRMUserProperties(prop, response);
                }

                so.Properties.BindPropertiesToResultTable();

            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private void StartCRMWorkflow(ref ServiceObject so)
        {
            SourceCode.SmartObjects.Services.ServiceSDK.Objects.Method meth = so.Methods[0];
            WizardFunctions helper = new WizardFunctions();
            CRMWorkflow crmWF = new CRMWorkflow();
            crmWF.Config = crmconfig;

            try
            {
                crmWF.EntityId = NotNull(so.Properties["EntityId"].Value);
                crmWF.WorkflowName = NotNull(so.Properties["WorkflowName"].Value);

                RestResponse<CRMWorkflow> response = helper.StartWorkflow(crmWF, config);

                so.Properties.InitResultTable();

                for (int c = 0; c < meth.ReturnProperties.Count; c += 1)
                {
                    Property prop = so.Properties[meth.ReturnProperties[c]];
                    prop = SetCRMWorkflowProperties(prop, response);
                }

                so.Properties.BindPropertiesToResultTable();

            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private void CreateTask(ref ServiceObject so)
        {
            SourceCode.SmartObjects.Services.ServiceSDK.Objects.Method meth = so.Methods[0];
            WizardFunctions helper = new WizardFunctions();

            bool async = false;
            bool.TryParse(NotNull(so.Properties["ExecuteAsync"].Value), out async);
            config.Async = async;
            

            CRMTask task = new CRMTask();
            task.Config = crmconfig;

            task.Category = NotNull(so.Properties["Category"].Value);
            task.Description = NotNull(so.Properties["Description"].Value);
            task.DueDate = DateTime.Parse(NotNull(so.Properties["DueDate"].Value));
            task.Duration = int.Parse(NotNull(so.Properties["Duration"].Value));
            task.OwnerFQN = NotNull(so.Properties["OwnerFQN"].Value);
            task.OwnerId = NotNull(so.Properties["OwnerId"].Value);
            task.Owner = NotNull(so.Properties["Owner"].Value);
            task.Priority = int.Parse(NotNull(so.Properties["Priority"].Value));
            task.Regarding = NotNull(so.Properties["Regarding"].Value);
            task.RegardingId = NotNull(so.Properties["RegardingId"].Value);
            task.State = int.Parse(NotNull(so.Properties["State"].Value));
            task.Status = int.Parse(NotNull(so.Properties["Status"].Value));
            task.Subcategory = NotNull(so.Properties["Subcategory"].Value);
            task.Subject = NotNull(so.Properties["Subject"].Value);
            task.K2SerialNumber = NotNull(so.Properties["K2SerialNumber"].Value);
            task.K2ActivityName = NotNull(so.Properties["K2ActivityName"].Value);
            task.K2ProcessName = NotNull(so.Properties["K2ProcessName"].Value);
            task.K2ProcessInstanceId = int.Parse(NotNull(so.Properties["K2ProcessInstanceId"].Value));

            try
            {
                RestResponse<CRMTask> response = helper.CreateTask(task, config);

                if (response != null)
                {
                    so.Properties.InitResultTable();

                    for (int c = 0; c < meth.ReturnProperties.Count; c += 1)
                    {
                        Property prop = so.Properties[meth.ReturnProperties[c]];
                        prop = SetTaskProperties(prop, response);
                    }
                    so.Properties.BindPropertiesToResultTable();
                }
                {
                    // do something for async call
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private void BulkActionTasksSetCriteria(ref ServiceObject so)
        {
            SourceCode.SmartObjects.Services.ServiceSDK.Objects.Method meth = so.Methods[0];
            WizardFunctions helper = new WizardFunctions();

            CRMBulkActionTask fetch = new CRMBulkActionTask();
            fetch.Config = crmconfig;

            try
            {
                string fetchstring = string.Empty;
                if (!string.IsNullOrEmpty(NotNull(so.Properties["K2ActivityName"])))
                {
                    fetchstring = Resources.K2CRMTaskCleanUpFinishRule.Replace("[entityid]", NotNull(so.Properties["RegardingId"].Value)).Replace("[entityname]", NotNull(so.Properties["Regarding"].Value));
                    fetchstring = fetchstring.Replace("[processname]", NotNull(so.Properties["K2ProcessName"].Value));
                    fetchstring = fetchstring.Replace("[statecode]", NotNull(so.Properties["FromState"].Value));
                    fetchstring = fetchstring.Replace("[processinstanceid]", NotNull(so.Properties["K2ProcessInstanceId"].Value));
                }
                else
                {
                    fetchstring = Resources.K2CRMTaskCleanUpState0FetchXML.Replace("[entityid]", NotNull(so.Properties["RegardingId"].Value)).Replace("[entityname]", NotNull(so.Properties["Regarding"].Value));
                    fetchstring = fetchstring.Replace("[activityname]", NotNull(so.Properties["K2ActivityName"].Value));
                    fetchstring = fetchstring.Replace("[processname]", NotNull(so.Properties["K2ProcessName"].Value));
                    fetchstring = fetchstring.Replace("[statecode]", NotNull(so.Properties["FromState"].Value));
                    fetchstring = fetchstring.Replace("[processinstanceid]", NotNull(so.Properties["K2ProcessInstanceId"].Value));   
                }
                
                fetch.FetchXML = fetchstring;
                fetch.FromState = int.Parse(NotNull(so.Properties["FromState"].Value));
                fetch.FromStatus = int.Parse(NotNull(so.Properties["FromStatus"].Value));
                fetch.ToState = int.Parse(NotNull(so.Properties["ToState"].Value));
                fetch.ToStatus = int.Parse(NotNull(so.Properties["ToStatus"].Value));

                RestResponse<CRMBulkActionTask> response = helper.BulkActionTasks(fetch, config);

                so.Properties.InitResultTable();

                if (response.Data != null && response.Data.Entities != null)
                {
                    foreach (CRMState state in response.Data.Entities)
                    {
                        for (int c = 0; c < meth.ReturnProperties.Count; c += 1)
                        {
                            Property prop = so.Properties[meth.ReturnProperties[c]];
                            prop = SetGetEntitiesProperties(prop, state);
                            prop = SetBulkActionTasksRESTProperties(prop, response);
                        }
                        so.Properties.BindPropertiesToResultTable();
                    }
                }
                else
                {
                    for (int c = 0; c < meth.ReturnProperties.Count; c += 1)
                    {
                        Property prop = so.Properties[meth.ReturnProperties[c]];
                        prop = SetBulkActionTasksRESTProperties(prop, response);
                    }
                    so.Properties.BindPropertiesToResultTable();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private void BulkActionTasks(ref ServiceObject so)
        {
            SourceCode.SmartObjects.Services.ServiceSDK.Objects.Method meth = so.Methods[0];
            WizardFunctions helper = new WizardFunctions();

            CRMBulkActionTask fetch = new CRMBulkActionTask();
            fetch.Config = crmconfig;

            try
            {
                fetch.FetchXML = NotNull(so.Properties["FetchXML"].Value);
                fetch.FromState = int.Parse(NotNull(so.Properties["FromState"].Value));
                fetch.FromStatus = int.Parse(NotNull(so.Properties["FromStatus"].Value));
                fetch.ToState = int.Parse(NotNull(so.Properties["ToState"].Value));
                fetch.ToStatus = int.Parse(NotNull(so.Properties["ToStatus"].Value));

                RestResponse<CRMBulkActionTask> response = helper.BulkActionTasks(fetch, config);

                so.Properties.InitResultTable();
                
                if (response.Data != null && response.Data.Entities != null)
                {
                    foreach (CRMState state in response.Data.Entities)
                    {
                        for (int c = 0; c < meth.ReturnProperties.Count; c += 1)
                        {
                            Property prop = so.Properties[meth.ReturnProperties[c]];
                            prop = SetGetEntitiesProperties(prop, state);
                            prop = SetBulkActionTasksRESTProperties(prop, response);
                        }
                        so.Properties.BindPropertiesToResultTable();
                    }
                }
                else
                {
                    for (int c = 0; c < meth.ReturnProperties.Count; c += 1)
                    {
                        Property prop = so.Properties[meth.ReturnProperties[c]];
                        prop = SetBulkActionTasksRESTProperties(prop, response);
                    }
                    so.Properties.BindPropertiesToResultTable();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private void SetStateStatus(ref ServiceObject so)
        {
            SourceCode.SmartObjects.Services.ServiceSDK.Objects.Method meth = so.Methods[0];
            WizardFunctions helper = new WizardFunctions();

            CRMState state = new CRMState();
            state.Config = crmconfig;

            try
            {
                state.Entity = NotNull(so.Properties["Entity"].Value);
                state.EntityId = NotNull(so.Properties["EntityId"].Value);
                state.State = int.Parse(NotNull(so.Properties["State"].Value));
                state.Status = int.Parse(NotNull(so.Properties["Status"].Value));

                RestResponse<CRMState> response = helper.SetStateStatus(state, config);

                so.Properties.InitResultTable();

                for (int c = 0; c < meth.ReturnProperties.Count; c += 1)
                {
                    Property prop = so.Properties[meth.ReturnProperties[c]];
                    prop = SetStateStatusProperties(prop, response);
                }

                so.Properties.BindPropertiesToResultTable();

            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private void ChangeOwner(ref ServiceObject so)
        {
            SourceCode.SmartObjects.Services.ServiceSDK.Objects.Method meth = so.Methods[0];
            //K2CRMHelper helper = new K2CRMHelper();
            WizardFunctions helper = new WizardFunctions();
            CRMEntityOwnership owner = new CRMEntityOwnership();
            owner.Config = crmconfig;

            try
            {
                owner.Assignee = NotNull(so.Properties["Assignee"].Value);
                owner.AssigneeId = NotNull(so.Properties["AssigneeId"].Value);
                owner.Target = NotNull(so.Properties["Target"].Value);
                owner.TargetId = NotNull(so.Properties["TargetId"].Value);

                RestResponse<CRMEntityOwnership> response = helper.ChangeOwner(owner, config);

                so.Properties.InitResultTable();

                for (int c = 0; c < meth.ReturnProperties.Count; c += 1)
                {
                    Property prop = so.Properties[meth.ReturnProperties[c]];
                    prop = SetChangeOwnerProperties(prop, response);
                }

                so.Properties.BindPropertiesToResultTable();

            }
            catch (Exception ex)
            {
                throw;
            }
        }