public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext)
        {
            String        PayloadDetails      = PayLoad.Get(executionContext);
            int           ErrorCode           = 400; //400 -- bad request
            String        _ErrorMessage       = string.Empty;
            String        _ErrorMessageDetail = string.Empty;
            Guid          ContactId           = Guid.Empty;
            Guid          AccountId           = Guid.Empty;
            StringBuilder ErrorMessage        = new StringBuilder();
            String        UniqueReference     = string.Empty;

            try
            {
            }

            #region Catch Exception
            catch (Exception ex)
            {
                crmWorkflowContext.Trace("inside catch");
                ErrorCode           = 500;
                _ErrorMessage       = "Error occured while processing request";
                _ErrorMessageDetail = ex.Message;
                ErrorCode           = 400;
                this.ReturnMessageDetails.Set(executionContext, _ErrorMessageDetail);
            }
            #endregion

            #region Finally Block
            finally
            {
            }
            #endregion
        }
Beispiel #2
0
        public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext)
        {
            String        PayloadDetails = PayLoad.Get(executionContext);
            int?          optionSetValue;
            int           ErrorCode           = 400; //400 -- bad request
            String        _ErrorMessage       = string.Empty;
            String        _ErrorMessageDetail = string.Empty;
            Guid          ContactId           = Guid.Empty;
            Guid          CrmGuid             = Guid.Empty;
            StringBuilder ErrorMessage        = new StringBuilder();
            String        UniqueReference     = string.Empty;

            try
            {
                objCommon = new IdmNs.Helper(executionContext);
                objCommon.tracingService.Trace("Load CRM Service from context --- OK");
                Entity AccountObject = new Entity(Defra.CustMaster.D365.Common.schema.AccountContants.ENTITY_NAME);

                objCommon.tracingService.Trace("attempt to seriallised new");
                IdmNs.Organisation AccountPayload = JsonConvert.DeserializeObject <IdmNs.Organisation>(PayloadDetails);
                objCommon.tracingService.Trace("seriallised object working");
                var ValidationContext = new ValidationContext(AccountPayload, serviceProvider: null, items: null);
                ICollection <System.ComponentModel.DataAnnotations.ValidationResult> ValidationResults        = null;
                ICollection <System.ComponentModel.DataAnnotations.ValidationResult> ValidationResultsAddress = null;

                var     isValid        = objCommon.Validate(AccountPayload, out ValidationResults);
                Boolean isValidAddress = AccountPayload.address == null ? true :
                                         objCommon.Validate(AccountPayload.address, out ValidationResultsAddress);

                if (isValid & isValidAddress)
                {
                    objCommon.tracingService.Trace("length{0}", AccountPayload.name.Length);
                    if (AccountPayload.hierarchylevel != 0)
                    {
                        objCommon.tracingService.Trace("hierarchylevel level: {0}", AccountPayload.hierarchylevel);

                        if (!String.IsNullOrEmpty(Enum.GetName(typeof(defra_OrganisationHierarchyLevel), AccountPayload.hierarchylevel)))
                        {
                            objCommon.tracingService.Trace("before assinging value");

                            AccountObject[Defra.CustMaster.D365.Common.schema.AccountContants.HIERARCHYLEVEL] = new OptionSetValue(AccountPayload.hierarchylevel);
                            objCommon.tracingService.Trace("after assinging value");


                            if (!String.IsNullOrEmpty(Enum.GetName(typeof(defra_OrganisationType), AccountPayload.type)))
                            {
                                //check if crn exists

                                OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(objCommon.service);
                                var checkCRNExistis = from c in orgSvcContext.CreateQuery("account")
                                                      where (string)c[Defra.CustMaster.D365.Common.schema.AccountContants.COMPANY_HOUSE_ID] == AccountPayload.crn
                                                      select new { organisationid = c.Id };

                                if (checkCRNExistis.FirstOrDefault() == null)
                                {
                                    objCommon.tracingService.Trace("After completing validation 12" + AccountPayload.type);
                                    optionSetValue = AccountPayload.type;
                                    objCommon.tracingService.Trace("before assigning type  " + AccountPayload.type);
                                    objCommon.tracingService.Trace(optionSetValue.ToString());
                                    objCommon.tracingService.Trace("after  setting up option set value");
                                    OptionSetValueCollection BusinessTypes = new OptionSetValueCollection();
                                    BusinessTypes.Add(new OptionSetValue(optionSetValue.Value));
                                    AccountObject[Defra.CustMaster.D365.Common.schema.AccountContants.TYPE]             = BusinessTypes;
                                    AccountObject[Defra.CustMaster.D365.Common.schema.AccountContants.NAME]             = AccountPayload.name == null ? string.Empty : AccountPayload.name;
                                    AccountObject[Defra.CustMaster.D365.Common.schema.AccountContants.COMPANY_HOUSE_ID] = AccountPayload.crn == string.Empty ? null : AccountPayload.crn;
                                    AccountObject[Defra.CustMaster.D365.Common.schema.AccountContants.TELEPHONE1]       = AccountPayload.telephone == null ? string.Empty : AccountPayload.telephone;
                                    objCommon.tracingService.Trace("after  setting other fields");

                                    bool IsValidGuid;
                                    Guid ParentAccountId;
                                    if (AccountPayload.parentorganisation != null && String.IsNullOrEmpty(AccountPayload.parentorganisation.parentorganisationcrmid))
                                    {
                                        IsValidGuid = Guid.TryParse(AccountPayload.parentorganisation.parentorganisationcrmid, out ParentAccountId);
                                        if (IsValidGuid)
                                        {
                                            AccountObject[Defra.CustMaster.D365.Common.schema.AccountContants.PARENTACCOUNTID] = ParentAccountId;
                                        }
                                    }

                                    AccountObject[Defra.CustMaster.D365.Common.schema.AccountContants.EMAILADDRESS1] = AccountPayload.email;
                                    objCommon.tracingService.Trace("before createing guid:");
                                    CrmGuid = objCommon.service.Create(AccountObject);
                                    objCommon.tracingService.Trace("after createing guid:{0}", CrmGuid.ToString());
                                    Entity AccountRecord = objCommon.service.Retrieve("account", CrmGuid, new Microsoft.Xrm.Sdk.Query.ColumnSet(Defra.CustMaster.D365.Common.schema.AccountContants.UNIQUEREFERENCE));
                                    UniqueReference = (string)AccountRecord[Defra.CustMaster.D365.Common.schema.AccountContants.UNIQUEREFERENCE];
                                    objCommon.CreateAddress(AccountPayload.address, new EntityReference(Defra.CustMaster.D365.Common.schema.AccountContants.ENTITY_NAME, CrmGuid));
                                    ErrorCode = 200;
                                }
                                else
                                {
                                    ErrorCode    = 412;
                                    ErrorMessage = ErrorMessage.Append(String.Format("Company house id already exists."));
                                }
                            }
                        }
                        else
                        {
                            ErrorCode    = 400;
                            ErrorMessage = ErrorMessage.Append(String.Format("Option set value {0} for orgnisation hirarchy level not found.",
                                                                             Defra.CustMaster.D365.Common.schema.AccountContants.HIERARCHYLEVEL.ToString()));
                        }
                    }

                    else
                    {
                        ErrorCode    = 400;
                        ErrorMessage = ErrorMessage.Append(String.Format("Option set value {0} for orgnisation type does not exists.",
                                                                         AccountPayload.type));
                    }
                }
                else
                {
                    objCommon.tracingService.Trace("inside validation result");
                    ErrorMessage = new StringBuilder();
                    //this will throw an error
                    foreach (System.ComponentModel.DataAnnotations.ValidationResult vr in ValidationResults)
                    {
                        ErrorMessage.Append(vr.ErrorMessage + " ");
                    }
                    foreach (System.ComponentModel.DataAnnotations.ValidationResult vr in ValidationResultsAddress)
                    {
                        ErrorMessage.Append(vr.ErrorMessage + " ");
                    }
                    ErrorCode = 400;
                }
            }
            catch (Exception ex)
            {
                objCommon.tracingService.Trace("inside exception");

                ErrorCode           = 500;
                _ErrorMessage       = "Error occured while processing request";
                _ErrorMessageDetail = ex.Message;
                ErrorCode           = 400;
                this.ReturnMessageDetails.Set(executionContext, _ErrorMessageDetail);
                objCommon.tracingService.Trace(ex.Message);
            }
            finally
            {
                objCommon.tracingService.Trace("finally block start");
                AccountResponse responsePayload = new AccountResponse()
                {
                    code     = ErrorCode,
                    message  = ErrorMessage.ToString(),
                    datetime = DateTime.UtcNow,
                    version  = "1.0.0.2",

                    status = ErrorCode == 200 ? "success" : "failure",
                    data   = new AccountData()
                    {
                        accountid    = CrmGuid,
                        uniquerefere = UniqueReference,
                        error        = new ResponseErrorBase()
                        {
                            details = _ErrorMessageDetail
                        }
                    }
                };
                objCommon.tracingService.Trace("attempting to serialise");

                string json = JsonConvert.SerializeObject(responsePayload);

                ReturnMessageDetails.Set(executionContext, json);
                // OutputCode.Set(executionContext, _errorCode);

                objCommon.tracingService.Trace("json {0}", json);
                objCommon.tracingService.Trace("finally block end");
            }
        }
Beispiel #3
0
        protected override void Execute(CodeActivityContext executionContext)
        {
            #region "Read Parameters"

            //Account AccountPayload = JsonConvert.DeserializeObject<Account>(PayLoad.Get(executionContext));

            String Payload = PayLoad.Get(executionContext);
            DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(Account));

            int?   optionSetValue;
            Int64  ErrorCode           = 400; //400 -- bad request
            String _ErrorMessage       = string.Empty;
            String _ErrorMessageDetail = string.Empty;
            Guid   ContactId           = Guid.Empty;
            Guid   CrmGuid;
            #endregion

            #region "Load CRM Service from context"
            try
            {
                Common objCommon = new Common(executionContext);
                objCommon.tracingService.Trace("Load CRM Service from context --- OK");

                using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(Payload)))
                {
                    Account AccountPayload = (Account)deserializer.ReadObject(ms);


                    if (AccountPayload.type == 0)
                    {
                        objCommon.tracingService.Trace("checking business type");

                        _ErrorMessage = "Business type cannot be empty.";
                    }
                    else if (!String.IsNullOrEmpty(AccountPayload.name) && AccountPayload.name.Length > 160)
                    {
                        objCommon.tracingService.Trace("checking org name");

                        _ErrorMessage = "Organiation name is more than 160 characters.";
                    }
                    else if (!String.IsNullOrEmpty(AccountPayload.crn) && AccountPayload.crn.Length > 8)
                    {
                        objCommon.tracingService.Trace("checking house id");

                        _ErrorMessage = "Company House Id cannot be more than 8 characters.";
                    }

                    /*
                     * else if (!(String.IsNullOrWhiteSpace(AccountPayload.)) && (_ValidatedWithCompanyHouse == "y" || _ValidatedWithCompanyHouse == "n"))
                     * {
                     *  objCommon.tracingService.Trace("checking validated with company house id");
                     *
                     *  _ErrorMessage = "Validated with company house should have y or n";
                     *
                     * }*/
                    /*
                     * else if (!String.IsNullOrEmpty(_Email) && _Email.Length > 100)
                     * {
                     *  _ErrorMessage = "Email address cannot be more than 100 characters long.";
                     *
                     * }*/

                    else
                    {
                        objCommon.tracingService.Trace("After completing validation");

                        optionSetValue = AccountPayload.type;
                        Entity Account = new Entity("account");
                        objCommon.tracingService.Trace("before assigning");
                        objCommon.tracingService.Trace(optionSetValue.ToString());

                        OptionSetValueCollection BusinessTypes = new OptionSetValueCollection();
                        BusinessTypes.Add(new OptionSetValue(optionSetValue.Value)); //
                        Account["defra_type"] = BusinessTypes;

                        //Account["defra_businesstype"] = BusinessTypes;
                        Account["name"] = AccountPayload.name;
                        Account["defra_companyhouseid"] = AccountPayload.crn == null ? null : AccountPayload.crn;
                        Account["telephone"]            = AccountPayload.telephone == null ? null : AccountPayload.telephone;
                        Account["defra_hierarchylevel"] = AccountPayload.hierarchylevel == null ? null : AccountPayload.hierarchylevel;

                        if (AccountPayload.parentorganisation != null && AccountPayload.parentorganisation.parentorganisationcrmid != Guid.Empty)
                        {
                            Entity ParentAccount = objCommon.service.Retrieve("account", AccountPayload.parentorganisation.parentorganisationcrmid, new Microsoft.Xrm.Sdk.Query.ColumnSet("accountid"));
                            Account["parentaccountid"] = AccountPayload.parentorganisation.parentorganisationcrmid;
                        }
                        objCommon.tracingService.Trace("after assigning");

                        if (AccountPayload.validatedwithcompanieshouse == "y")
                        {
                            Account["defra_validatedwithcompanyhouse"] = 0;
                        }
                        else if (AccountPayload.validatedwithcompanieshouse == "n")
                        {
                            Account["defra_validatedwithcompanyhouse"] = 1;
                        }

                        if (AccountPayload.email != null)
                        {
                            Account["emailaddress1"] = AccountPayload.email;
                        }
                        objCommon.tracingService.Trace("before createing guid:");
                        try
                        {
                            CrmGuid = objCommon.service.Create(Account);
                            objCommon.tracingService.Trace("after createing guid:{0}", CrmGuid.ToString());
                            this.CrmGuid.Set(executionContext, CrmGuid.ToString());
                            Entity AccountRecord = objCommon.service.Retrieve("account", CrmGuid, new Microsoft.Xrm.Sdk.Query.ColumnSet("defra_uniquereference"));
                            this.Code.Set(executionContext, ErrorCode.ToString());
                            this.UniqueReference.Set(executionContext, AccountRecord["defra_uniquereference"]);
                            objCommon.CreateAddress(AccountPayload.address, new EntityReference("account", CrmGuid));
                        }
                        catch (Exception ex)
                        {
                            objCommon.tracingService.Trace("error message:" + ex.Message);
                        }
                        ErrorCode = 200; //success
                    }

                    objCommon.tracingService.Trace("outside 1");

                    this.Code.Set(executionContext, ErrorCode.ToString());
                    this.Message.Set(executionContext, _ErrorMessage);
                    this.MessageDetail.Set(executionContext, _ErrorMessageDetail);
                    if (_ErrorMessage != string.Empty)
                    {
                        this.Message.Set(executionContext, _ErrorMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorCode           = 500;
                _ErrorMessage       = "Error occured while processing request";
                _ErrorMessageDetail = ex.Message;
                //throw ex;
                this.Code.Set(executionContext, ErrorCode.ToString());
                this.Message.Set(executionContext, _ErrorMessage);
                this.MessageDetail.Set(executionContext, _ErrorMessageDetail);
            }
            #endregion
        }