Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

Post Operation Create Plugin Updating two entities using updateObject()

$
0
0

Hello Everyone, 

I have have three entities. A contract that has many scheduled payments (due payments that a customer should pay), and received payments (the payments that a customer pays to close the scheduled payments). 
On the on-create of received payments, I want to update both the contract and the scheduled payments. 
I wrote code to update both. The code works well when each part is written alone (when I update the scheduled payments alone, or the contract alone). However, when both updateobject() statements are included, the code only updates the contract. It also displays null values in the fields I want to update in scheduled payments even if they contain data. What is wrong? I tried debugging the plugin and there are no problems except in the last statement of updating the object of contract: an exception appears. 

 edm_receivedpayment payment = entity.ToEntity<edm_receivedpayment>();
            using (GeneratedEntities orgContext = new GeneratedEntities(localContext.OrganizationService))
            {

                //get the scheduled payments that belong to the same contract as the received payment being now made
                var payments = (from p in orgContext.edm_paymentSet
                                where p.GetAttributeValue<EntityReference>("edm_contract") == payment.edm_Contract
                                orderby p.GetAttributeValue<string>("edm_name") ascending
                                select p);

            decimal totalRemainingAmount= 0;
            decimal remainingAmountofLine = 0;


                if (payments != null)
                {

                    if (payment.GetAttributeValue<OptionSetValue>("edm_paidby").Value == 757580001)
                    {
                        Money moneyValue = new Money() { Value = 0 };

                        var paymentOfCompany = (from p in payments
                                                where p.GetAttributeValue<Money>("edm_remainingamount") != moneyValue
                                                select p).FirstOrDefault();
                        paymentOfCompany.edm_SettledbyCompany = true;
                        orgContext.UpdateObject(paymentOfCompany);
                        orgContext.SaveChanges();
                    }
                    else if (payment.GetAttributeValue<OptionSetValue>("edm_paidby").Value == 757580000)
                    {



                        decimal amountTobePaid = ((Money)payment.edm_Amount).Value;
                        DateTime date = (DateTime)payment.edm_PaymentDate;

                        foreach (var scheduledPayment in payments)
                        {

                            decimal newPaidAmount = 0;
                            decimal paidAmount = 0;
                            if (scheduledPayment.Contains("edm_paidamount"))
                            {
                                paidAmount = ((Money)scheduledPayment.edm_PaidAmount).Value;
                            }
                            decimal amount = ((Money)scheduledPayment.edm_Amount).Value;
                            decimal remainingAmount = amount - paidAmount;
                            decimal value = 0;



                            if (remainingAmount != value)
                            {

                                   //contractCode 1
                                if (Int32.Parse(scheduledPayment.edm_name) < 47)
                                {
                                    //  remainingAmountofLine = ((Money)scheduledPayment.edm_RemainingAmount).Value;
                                    totalRemainingAmount = remainingAmount + totalRemainingAmount;
                                    if (Int32.Parse(scheduledPayment.edm_name) == 47 && remainingAmount > amountTobePaid)
                                    {
                                        totalRemainingAmount = totalRemainingAmount - paidAmount;

                                    }

                                    else if (Int32.Parse(scheduledPayment.edm_name) == 47 && remainingAmountofLine <= amountTobePaid)
                                    {
                                        totalRemainingAmount = totalRemainingAmount - remainingAmount;
                                    }
                                }

                             //end of contract code 1

                                decimal amountLeft = remainingAmount - amountTobePaid;



                                if (amountLeft < 0)
                                {
                                    newPaidAmount = amount;
                                    amountTobePaid = -amountLeft;

                                    scheduledPayment["edm_settlementdate"] = date;
                                    scheduledPayment["edm_statusofpayment"] = true;
                                    scheduledPayment["edm_settledbyclient"]=true;
                                }
                                else if (amountLeft >= 0)
                                {
                                    if (amountLeft == 0)
                                    {
                                        scheduledPayment["edm_settlementdate"] = date;
                                        scheduledPayment["edm_statusofpayment"] = true;
                                        scheduledPayment["edm_settledbyclient"] = true;
                                    }
                                    newPaidAmount = paidAmount + amountTobePaid;
                                    amountTobePaid = 0;

                                }


                                scheduledPayment.edm_PaidAmount = new Money() { Value = newPaidAmount };

                                orgContext.UpdateObject(scheduledPayment);
                                orgContext.SaveChanges();
                            }
                        }
                    }


                    //contract Code 2



                  Guid contractId = ((EntityReference)payment["edm_contract"]).Id;
                   var contract = ( from c in orgContext.SalesOrderSet
                                  where c.GetAttributeValue<Guid>("salesorderid") == contractId
                                  select c).FirstOrDefault();
                        contract.edm_TotalRemainingAmount = new Money() { Value = totalRemainingAmount };

                                      orgContext.UpdateObject(contract);
                                     orgContext.SaveChanges();

                   //End of contract code 2
                }

            }
        }
    }
}



Viewing all articles
Browse latest Browse all 46379

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>