private void CreateEntry(Transaction transaction)
        {
            ApplicationObject salesInvoice = ApplicationObject.Create();

            try
            {
                // Set the customer for the Sales Invoice
                string customerId = transaction.GetValueNullReturn("Customer") as string;
                if (!_wrapper.IsExistingCustomer(customerId))
                {
                    throw new Exception(string.Format("The customer [{0}] is invalid", customerId), null);
                }

                _wrapper.SetField(salesInvoice, "Customer", transaction);
                _wrapper.SetField(salesInvoice, "InstrumentNo", transaction);
                _wrapper.SetField(salesInvoice, "SecondReference", transaction);
                _wrapper.SetField(salesInvoice, "InstrumentDate", transaction);
                _wrapper.SetField(salesInvoice, "DueDate", transaction);

                // In this section the detail lines of the invoice are iterated.
                // Firstly check the iterator contains any child records.
                if (_iterator.HasChild(ENTITY_DETAIL))
                {
                    // Now move to the child record.
                    if (_iterator.MoveChild(ENTITY_DETAIL))
                    {
                        int detailCnt = 0;

                        // The iterator is now positioned on the child record.
                        // Create a detail record for each of the child records.
                        while (_iterator.Read())
                        {
                            detailCnt++;
                            CreateDetailEntry(salesInvoice, _iterator.Item());
                        }

                        // Now move back to the parent.
                        _iterator.MoveParent();
                    }
                }

                // Set any remaing fields that may need to be set after the detail records.
                _wrapper.SetFieldDecimal(salesInvoice, "DicountPercent", transaction, 2);
                _wrapper.SetField(salesInvoice, "DiscountDays", transaction);

                // Update the invoice.
                salesInvoice.Update();

                _wrapper.AuditController.OnInsert(_transformId, transaction);

                // Post update we may need to update the recordset with a number of fields.
                if (_tranHasURNFld)
                {
                    transaction.SetFieldValue("PostingReference", salesInvoice.GetFieldValue("PostRef"), true);
                }

                // Write the posted document total onto the transaction.
                if (_tranHasPostedDocTotalFld)
                {
                    transaction.SetFieldValue("DocumentTotal", salesInvoice.GetFieldValue("DocTotal"), true);
                }
            }
            catch (Exception ex)
            {
                string errorText = string.Format("Error whilst processing {0} record.", ENTITY_DESC);

                // There's an error based on the error action.
                switch (_errAction)
                {
                // If the error action is set to Abort: Log the error and re-raise the exception.
                case eTransformErrorAction.eTransformErrorActionAbort:
                {
                    _wrapper.LogError(transaction, ex, string.Format("{0}  The entry will be discarded.", errorText));

                    throw;
                }

                // If the error action is set to Continue: CHAOS mode.
                // Try to save/update again and see what happens.
                // An exception will probably be generated, causing the transform to terminate unexpectedly.
                case eTransformErrorAction.eTransformErrorActionContinue:
                {
                    _wrapper.LogError(transaction, ex, string.Format("{0}  The entry will attempt to re-save.", errorText));
                    salesInvoice.Update();

                    // Post update we may need to update the recordset with a number of fields.
                    if (_tranHasURNFld)
                    {
                        transaction.SetFieldValue("PostingReference", salesInvoice.GetFieldValue("PostRef"), true);
                    }

                    //Write the posted document total onto the transaction.
                    if (_tranHasPostedDocTotalFld)
                    {
                        transaction.SetFieldValue("DocumentTotal", salesInvoice.GetFieldValue("DocTotal"), true);
                    }

                    _wrapper.AuditController.OnInsert(_transformId, transaction);

                    break;
                }

                // If the error action is Reject Record: Log the error and no update will occur.
                // Exit the method gracefully so the next record can be processed.
                case eTransformErrorAction.eTransformErrorActionRejectRecord:
                {
                    _wrapper.LogError(transaction, ex, string.Format("{0}  The entry will be discarded.", errorText));

                    break;
                }
                }
            }
            finally
            {
                // Check the iterator is back at the header; move the parent if it is not.
                if (_iterator.Item().TransactionId != ENTITY)
                {
                    _iterator.MoveParent();
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Updates/Inserts the customer record
        /// </summary>
        /// <param name="transaction">The transaction being processed.</param>
        private void CreateEntry(Transaction transaction)
        {
            bool isNew = false;

            // A mock call to whatever API.
            // Essentially get an object so the values of the dataset can be set to it.
            ApplicationObject customer = ApplicationObject.Create();

            try
            {
                // Use a common function in your wrapper/helper class to determine if a customer exists or not.
                isNew = !_wrapper.IsExistingCustomer(transaction.GetValueNullReturn("Reference") as string);

                // Now check/compare the updateflags, if the relevant flag is not set, return.
                if (isNew)
                {
                    if ((_updateOp & eERPUpdateOperation.eInsert) != eERPUpdateOperation.eInsert)
                    {
                        return;
                    }
                }
                else
                {
                    if ((_updateOp & eERPUpdateOperation.eUpdate) != eERPUpdateOperation.eUpdate)
                    {
                        return;
                    }
                }

                // Set the fields from the transaction onto the customer/application object.
                _wrapper.SetField(customer, "Name", transaction);
                _wrapper.SetField(customer, "ShortName", transaction);
                _wrapper.SetFieldDecimal(customer, "CreditLimit", transaction, 2);
                _wrapper.SetField(customer, "PaymentTermsDays", transaction);
                _wrapper.SetField(customer, "PaymentTermsBasis", transaction);
                _wrapper.SetField(customer, "CountryCode", transaction);
                _wrapper.SetField(customer, "AddressLine1", transaction);
                _wrapper.SetField(customer, "AddressLine2", transaction);
                _wrapper.SetField(customer, "AddressLine3", transaction);
                _wrapper.SetField(customer, "AddressLine4", transaction);
                _wrapper.SetField(customer, "City", transaction);
                _wrapper.SetField(customer, "County", transaction);
                _wrapper.SetField(customer, "Postcode", transaction);
                // Continue for each field until done.

                // Use the wrapper/helper class to set user defined/dynamic fields.
                _wrapper.SetUserDefinedFields(customer, transaction);

                // Now update the record...obviously it's going to be more compex than this.
                customer.Update();

                // In this example the customer reference is generated by the application...we may want to allow
                // this auto-generated reference to be recorded back onto the dataset to allow either:
                // a. Further processing i.e. updating a system with a foreign reference or;
                // b. Allow the value to be put onto the audit report.
                //
                // So, if the transaction object has a Reference field update the field with the value.
                // Note, if you attempt to set a field which is not defined an exception is generated.
                if (_tranHasReferenceFld)
                {
                    transaction.SetFieldValue("Reference", customer.GetFieldValue("Reference"), true);
                }

                // Now update the AuditController
                if (isNew)
                {
                    _wrapper.AuditController.OnInsert(_transformId, transaction);
                }
                else
                {
                    _wrapper.AuditController.OnUpdate(_transformId, transaction);
                }
            }
            catch (Exception ex)
            {
                string errorText = string.Format("Error whilst processing {0} record.", ENTITY_DESC);

                // There's an error based on the error action.
                switch (_errAction)
                {
                // If the error action is set to Abort: Log the error and re-raise the exception.
                case eTransformErrorAction.eTransformErrorActionAbort:
                {
                    _wrapper.LogError(transaction, ex, string.Format("{0}  The entry will be discarded.", errorText));

                    throw;
                }

                // If the error action is set to Continue: CHAOS mode.
                // Try to save/update again and see what happens.
                // An exception will probably be generated, causing the transform to terminate unexpectedly.
                case eTransformErrorAction.eTransformErrorActionContinue:
                {
                    _wrapper.LogError(transaction, ex, string.Format("{0}  The entry will attempt to re-save.", errorText));
                    //customer.Update();

                    if (isNew)
                    {
                        _wrapper.AuditController.OnInsert(_transformId, transaction);
                    }
                    else
                    {
                        _wrapper.AuditController.OnUpdate(_transformId, transaction);
                    }

                    break;
                }

                // If the error action is Reject Record: Log the error and no update will occur.
                // Exit the method gracefully so the next record can be processed.
                case eTransformErrorAction.eTransformErrorActionRejectRecord:
                {
                    _wrapper.LogError(transaction, ex, string.Format("{0}  The entry will be discarded.", errorText));

                    break;
                }
                }
            }
        }