Exemple #1
0
        void SendPOSTRequestToRestAPI(AccountObject2 account, ITracingService tracingObject, string webAddr)
        {
            tracingObject.Trace("Inside: " + "SendPOSTRequestToRestAPI");
            //var webAddr = "https://endpoint.scribesoft.com/v1/orgs/1725/requests/1897?accesstoken=20bd3d04-a02f-47f4-b09c-81b43ffed37e";
            //var webAddr = "https://endpoint.scribesoft.com/v1/orgs/1725/requests/2080?accesstoken=20bd3d04-a02f-47f4-b09c-81b43ffed37e";
            // var webAddr = "https://demoendpoint.scribeqa.net/v1/orgs/3718/requests/383?accesstoken=5eaa6452-ba58-4695-b397-2915c46bded0";
            //webAddr = "https://endpoint.scribesoft.com/v1/orgs/1725/requests/2238?accesstoken=20bd3d04-a02f-47f4-b09c-81b43ffed37e";
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);

            httpWebRequest.KeepAlive   = false;
            httpWebRequest.ContentType = "application/json; charset=utf-8";
            httpWebRequest.Method      = "POST";
            tracingObject.Trace("HTTP Request URL: " + httpWebRequest.RequestUri);

            try
            {
                tracingObject.Trace("Request Sent: " + "SendPOSTRequestToRestAPI");
                //tracingObject.Trace("INSIDE SendPOSTRequestToRestAPI" );
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = "{\"Name\":\"" + account.AccountName + "\",\"addStreet\":\"" + account.Street + "\",\"AddRoles\":\"" + account.AddressType + "\",\"Addstate\":\"" + account.State + "\",\"AddCity\":\"" + account.City + "\",\"AddZipCode\":\"" + account.ZipCode + "\",\"AddCountry\":\"" + account.Country + "\",\"Currency\":\"" + account.CurrencyName + "\",\"CustGroupId\":\"" + account.CustGrpId + "\",\"AccNo\":\"" + account.AccountNumber + "\",\"AddrName\":\"" + account.AddressName + "\",\"AccId\":\"" + account.AccountId + "\",\"RelType\":\"" + account.RelationshipType + "\",\"Phone\":\"" + account.Phone + "\",\"Email\":\"" + account.Email + "\",\"Website\":\"" + account.Website + "\",\"CompName\":\"" + account.CompanyName + "\",\"AddressNumber\":\"" + account.addressNumber + "\" }";
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AccountObject2));
                    MemoryStream stream = new MemoryStream();
                    serializer.WriteObject(stream, account);
                    stream.Position = 0;
                    StreamReader streamReader = new StreamReader(stream);

                    //string json = streamReader.ReadToEnd();

                    tracingObject.Trace("Request Data: " + json);
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    //tracingObject.Trace("JSON:- " + json);
                }


                //DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ResultSet));
                //ResultSet res = (ResultSet)serializer.ReadObject(httpWebRequest.GetResponse().GetResponseStream());
                //throw new InvalidPluginExecutionException(OperationStatus.Failed, "status in GP:" + res.data[0].status);
                //if (res.data.Count > 0 && res.data[0].status != null && res.data[0].status == "1")
                //    return true;

                var data = httpWebRequest.GetResponse().GetResponseStream();
                data.Close();
                data.Dispose();
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(OperationStatus.Failed, ex.Message + " " + ex.InnerException);
            }
            //tracingObject.Trace("Request Send");
        }
Exemple #2
0
        public void Execute(IServiceProvider serviceProvider)
        {
            IOrganizationService    service;
            IPluginExecutionContext context;
            Entity currentRecord = null;
            Entity postImage;;

            ITracingService tracingObject;

            try
            {
                tracingObject = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                //this.SetOrganizationService(serviceProvider);
                //this.SetCurrentRecord();
                //this.SetPostImage();

                #region SetOrganizationService

                context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                service = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(context.UserId);

                tracingObject = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                #endregion

                #region SetCurrentRecord
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    currentRecord = context.InputParameters["Target"] as Entity;
                }

                #endregion

                #region SetPostImage

                if (context.PostEntityImages.Contains("postimage"))
                {
                    postImage = (Entity)context.PostEntityImages["postimage"];
                }
                else
                {
                    throw new Exception("No Post Image found");
                }
                #endregion



                #region Call Scribe Service to Create Customer in Cloud AX

                //tracingObject.Trace("INSIDE Execute");
                AccountObject2 account = new AccountObject2();

                account.addressNumber = ReadAddress(postImage.GetAttributeValue <Guid>("address1_addressid"), service);

                account.AccountId        = currentRecord.Id;
                account.AccountName      = postImage.GetAttributeValue <string>("name");
                account.AccountNumber    = postImage.GetAttributeValue <string>("accountnumber");
                account.CurrencyName     = GetCurrencyISOCode(service, postImage);
                account.AddressName      = postImage.GetAttributeValue <string>("address1_name");
                account.AddressType      = postImage.FormattedValues.Contains("address1_addresstypecode") ? postImage.FormattedValues["address1_addresstypecode"].ToString() : string.Empty;
                account.Street           = postImage.GetAttributeValue <string>("address1_line1");
                account.City             = postImage.GetAttributeValue <string>("address1_city");
                account.State            = postImage.GetAttributeValue <string>("address1_stateorprovince");
                account.Country          = postImage.GetAttributeValue <string>("address1_country");
                account.ZipCode          = postImage.GetAttributeValue <string>("address1_postalcode");
                account.Phone            = postImage.GetAttributeValue <string>("telephone1");
                account.Email            = postImage.GetAttributeValue <string>("emailaddress1");
                account.Website          = postImage.GetAttributeValue <string>("websiteurl");
                account.RelationshipType = (postImage.GetAttributeValue <OptionSetValue>("customertypecode") == null) ? 3 : postImage.GetAttributeValue <OptionSetValue>("customertypecode").Value;
                account.CompanyName      = postImage.FormattedValues.Contains("cf_companyname") ? postImage.FormattedValues["cf_companyname"].ToString() : "USMF";


                string webAddr = GetAccountIntegrationURL(service); /// this will be the Scribe Endpoint URL
                tracingObject.Trace("Scribe Endpoint URL - " + webAddr);

                if (webAddr != string.Empty)
                {
                    SendPOSTRequestToRestAPI(account, tracingObject, webAddr);
                }
                else
                {
                    throw new InvalidPluginExecutionException(OperationStatus.Failed, "Cannot find Scribe Real time Integration URL from Integration configurations. Configuration Name: 'Account Integration Scribe URL'");
                }
                #endregion
            }

            catch (InvalidPluginExecutionException ex)
            {
                //tracingObject.Trace(ex.Message + "<--Execute--->" + ex.StackTrace);
                throw ex;
            }
            catch (TimeoutException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                //tracingObject.Trace(ex.Message + "<--Execute--->" + ex.StackTrace);
                //CreateErrorLog(LogError.LogErrors(ex.Message, ex.StackTrace, currentRecord.LogicalName, MethodBase.GetCurrentMethod().Name, this.GetType().Name, currentRecord.Id.ToString(), context.UserId));
            }
        }