Exemplo n.º 1
0
        private void AddOrUpdateThisRecordWithAzure(Entity queriedEntityRecord, Entity configurationRecord, string messageName)
        {
            tracingService.Trace("---------Send the Record to Azure---------");
            string text  = "DonorCommitment";
            string text2 = configurationRecord.GetAttributeValue <string>("msnfp_azure_webapiurl");

            tracingService.Trace("Got API URL: " + text2);
            if (!string.IsNullOrEmpty(text2))
            {
                tracingService.Trace("Getting Latest Info for Record: " + queriedEntityRecord.Id.ToString());
                MSNFP_DonorCommitment mSNFP_DonorCommitment = new MSNFP_DonorCommitment();
                mSNFP_DonorCommitment.DonorCommitmentId = queriedEntityRecord.Id;
                if (queriedEntityRecord.GetAttributeValue <EntityReference>("msnfp_appealid") != null)
                {
                    mSNFP_DonorCommitment.AppealId = queriedEntityRecord.GetAttributeValue <EntityReference>("msnfp_appealid").Id;
                    tracingService.Trace("Got msnfp_appealid.");
                }
                else
                {
                    mSNFP_DonorCommitment.AppealId = null;
                    tracingService.Trace("Did NOT find msnfp_appealid.");
                }
                if (queriedEntityRecord.GetAttributeValue <EntityReference>("msnfp_packageid") != null)
                {
                    mSNFP_DonorCommitment.PackageId = queriedEntityRecord.GetAttributeValue <EntityReference>("msnfp_packageid").Id;
                    tracingService.Trace("Got msnfp_packageid.");
                }
                else
                {
                    mSNFP_DonorCommitment.PackageId = null;
                    tracingService.Trace("Did NOT find msnfp_packageid.");
                }
                if (queriedEntityRecord.GetAttributeValue <EntityReference>("msnfp_commitment_campaignid") != null)
                {
                    mSNFP_DonorCommitment.CampaignId = queriedEntityRecord.GetAttributeValue <EntityReference>("msnfp_commitment_campaignid").Id;
                    tracingService.Trace("Got msnfp_commitment_campaignid.");
                }
                else
                {
                    mSNFP_DonorCommitment.CampaignId = null;
                    tracingService.Trace("Did NOT find msnfp_commitment_campaignid.");
                }
                if (queriedEntityRecord.GetAttributeValue <Money>("msnfp_totalamount") != null)
                {
                    mSNFP_DonorCommitment.TotalAmount = queriedEntityRecord.GetAttributeValue <Money>("msnfp_totalamount").Value;
                    tracingService.Trace("Got msnfp_totalamount.");
                }
                else
                {
                    mSNFP_DonorCommitment.TotalAmount = null;
                    tracingService.Trace("Did NOT find msnfp_totalamount.");
                }
                if (queriedEntityRecord.GetAttributeValue <EntityReference>("msnfp_customerid") != null)
                {
                    mSNFP_DonorCommitment.CustomerId = queriedEntityRecord.GetAttributeValue <EntityReference>("msnfp_customerid").Id;
                    if (queriedEntityRecord.GetAttributeValue <EntityReference>("msnfp_customerid").LogicalName == "contact")
                    {
                        mSNFP_DonorCommitment.CustomerIdType = 2;
                    }
                    else if (queriedEntityRecord.GetAttributeValue <EntityReference>("msnfp_customerid").LogicalName == "account")
                    {
                        mSNFP_DonorCommitment.CustomerIdType = 1;
                    }
                    tracingService.Trace("Got msnfp_customerid.");
                }
                else
                {
                    mSNFP_DonorCommitment.CustomerId     = null;
                    mSNFP_DonorCommitment.CustomerIdType = null;
                    tracingService.Trace("Did NOT find msnfp_customerid.");
                }
                if (queriedEntityRecord.GetAttributeValue <DateTime?>("msnfp_bookdate").HasValue)
                {
                    mSNFP_DonorCommitment.BookDate = queriedEntityRecord.GetAttributeValue <DateTime>("msnfp_bookdate");
                    tracingService.Trace("Got msnfp_bookdate.");
                }
                else
                {
                    mSNFP_DonorCommitment.BookDate = null;
                    tracingService.Trace("Did NOT find msnfp_bookdate.");
                }
                if (queriedEntityRecord.GetAttributeValue <Money>("msnfp_totalamount_balance") != null)
                {
                    mSNFP_DonorCommitment.TotalAmountBalance = queriedEntityRecord.GetAttributeValue <Money>("msnfp_totalamount_balance").Value;
                    tracingService.Trace("Got msnfp_totalamount_balance.");
                }
                else
                {
                    mSNFP_DonorCommitment.TotalAmountBalance = null;
                    tracingService.Trace("Did NOT find msnfp_totalamount_balance.");
                }
                if (messageName == "Create")
                {
                    mSNFP_DonorCommitment.CreatedOn = DateTime.UtcNow;
                }
                else if (queriedEntityRecord.Contains("createdon") && queriedEntityRecord["createdon"] != null)
                {
                    mSNFP_DonorCommitment.CreatedOn = (DateTime)queriedEntityRecord["createdon"];
                }
                mSNFP_DonorCommitment.SyncDate = DateTime.UtcNow;
                if (messageName == "Delete")
                {
                    mSNFP_DonorCommitment.Deleted     = true;
                    mSNFP_DonorCommitment.DeletedDate = DateTime.UtcNow;
                }
                else
                {
                    mSNFP_DonorCommitment.Deleted     = false;
                    mSNFP_DonorCommitment.DeletedDate = null;
                }
                if (queriedEntityRecord.Contains("statecode") && queriedEntityRecord["statecode"] != null)
                {
                    mSNFP_DonorCommitment.StateCode = ((OptionSetValue)queriedEntityRecord["statecode"]).Value;
                    tracingService.Trace("Got statecode.");
                }
                else
                {
                    mSNFP_DonorCommitment.StateCode = null;
                    tracingService.Trace("Did NOT find statecode.");
                }
                if (queriedEntityRecord.Contains("statuscode") && queriedEntityRecord["statuscode"] != null)
                {
                    mSNFP_DonorCommitment.StatusCode = ((OptionSetValue)queriedEntityRecord["statuscode"]).Value;
                    tracingService.Trace("Got statuscode.");
                }
                else
                {
                    mSNFP_DonorCommitment.StatusCode = null;
                    tracingService.Trace("Did NOT find statuscode.");
                }
                tracingService.Trace("JSON object created");
                if (messageName == "Create")
                {
                    text2 = text2 + text + "/Create" + text;
                }
                else if (messageName == "Update" || messageName == "Delete")
                {
                    text2 = text2 + text + "/Update" + text;
                }
                MemoryStream memoryStream = new MemoryStream();
                DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(MSNFP_DonorCommitment));
                dataContractJsonSerializer.WriteObject(memoryStream, mSNFP_DonorCommitment);
                byte[] array = memoryStream.ToArray();
                memoryStream.Close();
                string       @string      = Encoding.UTF8.GetString(array, 0, array.Length);
                WebAPIClient webAPIClient = new WebAPIClient();
                webAPIClient.Headers[HttpRequestHeader.ContentType] = "application/json";
                webAPIClient.Headers["Padlock"] = (string)configurationRecord["msnfp_apipadlocktoken"];
                webAPIClient.Encoding           = Encoding.UTF8;
                tracingService.Trace("---------Preparing JSON---------");
                tracingService.Trace("Converted to json API URL : " + text2);
                tracingService.Trace("JSON: " + @string);
                tracingService.Trace("---------End of Preparing JSON---------");
                tracingService.Trace("Sending data to Azure.");
                string text3 = webAPIClient.UploadString(text2, @string);
                tracingService.Trace("Got response.");
                tracingService.Trace("Response: " + text3);
                Plugins.PaymentProcesses.Utilities utilities = new Plugins.PaymentProcesses.Utilities();
                utilities.CheckAPIReturnJSONForErrors(text3, configurationRecord.GetAttributeValue <OptionSetValue>("msnfp_showapierrorresponses"), tracingService);
            }
            else
            {
                tracingService.Trace("No API URL or Enable Portal Pages. Exiting workflow.");
            }
        }
Exemplo n.º 2
0
        protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }
            localContext.TracingService.Trace("---------Triggered DonorCommitmentCreate.cs---------");
            IPluginExecutionContext    pluginExecutionContext     = localContext.PluginExecutionContext;
            IOrganizationService       organizationService        = localContext.OrganizationService;
            OrganizationServiceContext organizationServiceContext = new OrganizationServiceContext(organizationService);
            string empty       = string.Empty;
            Guid   empty2      = Guid.Empty;
            string messageName = pluginExecutionContext.MessageName;
            Entity entity      = null;

            if (!pluginExecutionContext.InputParameters.Contains("Target"))
            {
                return;
            }
            Entity configurationRecordByUser = Plugins.PaymentProcesses.Utilities.GetConfigurationRecordByUser(pluginExecutionContext, organizationService, localContext.TracingService);

            tracingService = localContext.TracingService;
            ColumnSet columnSet = new ColumnSet("msnfp_customerid", "msnfp_totalamount", "msnfp_totalamount_paid", "msnfp_totalamount_balance", "msnfp_parentscheduleid", "msnfp_appealid", "msnfp_packageid", "msnfp_commitment_campaignid", "statecode", "statuscode", "msnfp_bookdate");

            localContext.TracingService.Trace("---------Entering DonorCommitmentCreate.cs Main Function---------");
            localContext.TracingService.Trace("Message Name: " + messageName);
            Plugins.PaymentProcesses.Utilities utilities = new Plugins.PaymentProcesses.Utilities();
            if (pluginExecutionContext.InputParameters["Target"] is Entity)
            {
                Entity targetRecord = (Entity)pluginExecutionContext.InputParameters["Target"];
                entity = organizationService.Retrieve("msnfp_donorcommitment", targetRecord.Id, columnSet);
                if (messageName == "Create")
                {
                    if (targetRecord.Contains("msnfp_name") && targetRecord["msnfp_name"] != null)
                    {
                        localContext.TracingService.Trace("Pledge's title: " + (string)targetRecord["msnfp_name"]);
                        if (targetRecord.Contains("msnfp_commitment_campaignid"))
                        {
                            Entity entity2 = organizationService.Retrieve("campaign", ((EntityReference)targetRecord["msnfp_commitment_campaignid"]).Id, new ColumnSet("name"));
                            if (entity2.Contains("name"))
                            {
                                string str     = (string)entity2["name"];
                                Entity entity3 = organizationService.Retrieve(targetRecord.LogicalName, targetRecord.Id, new ColumnSet("msnfp_name"));
                                if (entity3.Contains("msnfp_name") && entity3["msnfp_name"] != null)
                                {
                                    entity3["msnfp_name"] = entity3["msnfp_name"]?.ToString() + " - " + str;
                                    organizationService.Update(entity3);
                                }
                            }
                        }
                    }
                    if (targetRecord.Contains("msnfp_customerid"))
                    {
                        localContext.TracingService.Trace("See if this donor has any pledge matches.");
                        List <Entity> list = (from c in organizationServiceContext.CreateQuery("msnfp_pledgematch")
                                              where ((EntityReference)c["msnfp_customerfromid"]).Id == ((EntityReference)targetRecord["msnfp_customerid"]).Id
                                              select c).ToList();
                        localContext.TracingService.Trace("Pledge Matches found: " + list.Count);
                        if (list != null)
                        {
                            foreach (Entity item in list)
                            {
                                localContext.TracingService.Trace("Pledge Match ID to process next: " + item.Id.ToString());
                                AddPledgeFromPledgeMatchRecord(targetRecord, item, localContext, organizationService, pluginExecutionContext);
                            }
                        }
                    }
                    if (targetRecord.Contains("msnfp_commitment_defaultdesignationid"))
                    {
                        CreateDesignationRecords(localContext, organizationService, targetRecord);
                    }
                    localContext.TracingService.Trace("Before checking if the Payment Schedule is present");
                    if (entity.Contains("msnfp_parentscheduleid") && entity["msnfp_parentscheduleid"] != null)
                    {
                        localContext.TracingService.Trace("Before updating the Payment Schedule");
                        utilities.UpdatePaymentScheduleBalance(organizationServiceContext, organizationService, entity, (EntityReference)entity["msnfp_parentscheduleid"], "create");
                        localContext.TracingService.Trace("After updating the Payment Schedule");
                    }
                    if (!string.IsNullOrEmpty(configurationRecordByUser.GetAttributeValue <string>("msnfp_apipadlocktoken")))
                    {
                        AddOrUpdateThisRecordWithAzure(entity, configurationRecordByUser, messageName);
                    }
                    Plugins.PaymentProcesses.Utilities.UpdateHouseholdOnRecord(organizationService, entity, "msnfp_householdid", "msnfp_customerid");
                }
                if (messageName == "Update")
                {
                    localContext.TracingService.Trace("Before checking if the Payment Schedule is present");
                    if (targetRecord.Contains("statuscode") && targetRecord["statuscode"] != null && entity.GetAttributeValue <EntityReference>("msnfp_parentscheduleid") != null && (localContext.PluginExecutionContext.Depth < 2 || (localContext.PluginExecutionContext.ParentContext != null && localContext.PluginExecutionContext.ParentContext.PrimaryEntityName.ToLower() == "msnfp_donorcommitment")))
                    {
                        localContext.TracingService.Trace("Before updating the Payment Schedule");
                        utilities.UpdatePaymentScheduleBalance(organizationServiceContext, organizationService, entity, (EntityReference)entity["msnfp_parentscheduleid"], "update");
                        localContext.TracingService.Trace("After updating the Payment Schedule");
                    }
                    if (!string.IsNullOrEmpty(configurationRecordByUser.GetAttributeValue <string>("msnfp_apipadlocktoken")))
                    {
                        AddOrUpdateThisRecordWithAzure(entity, configurationRecordByUser, messageName);
                    }
                    Plugins.PaymentProcesses.Utilities.UpdateHouseholdOnRecord(organizationService, targetRecord, "msnfp_householdid", "msnfp_customerid");
                }
            }
            else if (pluginExecutionContext.InputParameters["Target"] is EntityReference && string.Compare(messageName, "delete", StringComparison.CurrentCultureIgnoreCase) == 0)
            {
                EntityReference entityReference = (EntityReference)pluginExecutionContext.InputParameters["Target"];
                entity = organizationService.Retrieve("msnfp_donorcommitment", entityReference.Id, columnSet);
            }
            if (entity != null)
            {
                AddOrUpdateThisRecordWithAzure(entity, configurationRecordByUser, messageName);
                Plugins.Common.Utilities.CallYearlyGivingServiceAsync(entity.Id, entity.LogicalName, configurationRecordByUser.Id, organizationService, localContext.TracingService);
            }
            localContext.TracingService.Trace("---------Exiting DonorCommitmentCreate.cs---------");
        }