Compartilhar via


Create policy

A Create method requires an instance of the "create" policy for the object being created. The create policy contains a set of behaviors that control various aspects of the create operation, such as how certain properties will be set, or whether additional actions will be performed. The web service administrator controls all of the internal behavior options throught the Dynamics Security console. The external behaviors can be set by the application that is calling the create method.

When you call a Create method for the Dynamics GP service, you must retrieve an instance of the policy used for that method. Do this using either the GetPolicyByOperation web method or the GetPolicyByKey web method. If the context object you pass to either method specifies a valid role for the user, the policy that is configured for that role will be returned. Otherwise, the default policy for the operation will be returned.

Once the policy for the create operation has been retrieved, any external behaviors for the policy can be changed, if needed. The policy object will be passed to the web service Create method.

In the following C# example, a new GL transaction is created. The policy for the CreateGLTransaction web method is retrieved. One behavior for this policy controls whether a reversing transaction is also created. The ID (GUID) for this behavior is looked up from the Dynamics GP Web Service Reference and used to locate the behavior. The behavior option specifying that a reversing transaction should be created is also looked up in the reference. The parameter for this behavior option is set to the reversing transaction date. Finally, the selected option for the behavior is set to the option to create the reversing transaction.

Cc508729.LegacyEndpoint(en-us,MSDN.10).gif** Legacy endpoint**

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using DynamicsGPWebServiceSample.DynamicsGPService;
using System.Xml;

namespace DynamicsGPWebServiceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            CompanyKey companyKey;
            Context context;
            BatchKey batchKey;
            GLTransactionKey transactionKey;
            GLTransaction transaction;
            GLTransactionLineKey glTransactionLineKey;
            GLTransactionLine transactionDebitLine;
            GLTransactionLine transactionCreditLine;
            GLAccountNumberKey debitAccountKey;
            GLAccountNumberKey creditAccountKey;
            MoneyAmount debitAmount;
            MoneyAmount creditAmount;
            MoneyAmount zeroAmount;
            Policy transactionCreatePolicy;
            Behavior reversingTrxBehavior;
            BehaviorOption reversingTrxBehaviorOption;
            Guid behaviorGUID;
            int behaviorOptionID;

            // Create an instance of the web service
            DynamicsGP wsDynamicsGP = new DynamicsGP();

            // Be sure the default credentials are used
            wsDynamicsGP.UseDefaultCredentials = true;

            // Create a context with which to call the web service
            context = new Context();

            // Specify which company to use (sample company)
            companyKey = new CompanyKey();
            companyKey.Id = (-1);

            // Set up the context object
            context.OrganizationKey = (OrganizationKey)companyKey;
            context.CultureName = "en-US";

            // Create a batch key object to specify the batch
            batchKey = new BatchKey();
            batchKey.Id = "RMCSH00000011";

            // Create a GL transaction key object to identify the transaction
            transactionKey = new GLTransactionKey();
            transactionKey.JournalId = 3372;
            transactionKey.Date = new DateTime(2017, 4, 12);

            // Create the transaction object
            transaction = new GLTransaction();

            // Populate the GL transaction object's key property
            transaction.Key = transactionKey;

            // Populate the batch key and reference properties
            transaction.BatchKey = batchKey;
            transaction.Reference = "Receivables Cash Receipts";

            // Create a GL transaction line key
            glTransactionLineKey = new GLTransactionLineKey();

            // Create two GL transaction lines:
            // Create the debit transaction line
            transactionDebitLine = new GLTransactionLine();

            // Populate the transaction line key
            transactionDebitLine.Key = glTransactionLineKey;

            // Create a GL account number key object to specify the account
            debitAccountKey = new GLAccountNumberKey();
            debitAccountKey.Id = "000-1100-00";

            // Populate the debit line GL account key property
            transactionDebitLine.GLAccountKey = debitAccountKey;

            // Create a money object
            zeroAmount = new MoneyAmount();
            zeroAmount.Value = 0m;
            zeroAmount.Currency = "USD";

            // Create a money object for the debit amount
            debitAmount = new MoneyAmount();
            debitAmount.Value = 500m;
            debitAmount.Currency = "USD";

            // Populate the transaction line debit and credit amounts
            transactionDebitLine.DebitAmount = debitAmount;
            transactionDebitLine.CreditAmount = zeroAmount;

            // Create the credit transaction line
            transactionCreditLine = new GLTransactionLine();

            // Populate the transaction line key
            transactionCreditLine.Key = glTransactionLineKey;

            // Create a GL account number key object to specify the account
            creditAccountKey = new GLAccountNumberKey();
            creditAccountKey.Id = "000-1200-00";

            // Populate the credit line GL account key property
            transactionCreditLine.GLAccountKey = creditAccountKey;

            // Create a money amount object for the credit
            creditAmount = new MoneyAmount();
            creditAmount.Value = 500m;
            creditAmount.Currency = "USD";

            // Populate the transaction line debit and credit amounts
            transactionCreditLine.DebitAmount = zeroAmount;
            transactionCreditLine.CreditAmount = creditAmount;

            // Create an array to hold the two GL transaction line objects
            GLTransactionLine[] lines = { transactionDebitLine,
            transactionCreditLine };

            // Add the array of GL transaction line objects to the transaction
            transaction.Lines = lines;

            // Get the create policy for GL transactions
            transactionCreatePolicy = wsDynamicsGP.GetPolicyByOperation(
            "CreateGLTransaction", context);

            // Find the behavior in the list (from Web Service Reference)
            behaviorGUID = new Guid("b0c9fe57-c4f0-4d6a-b0aa-ab4d6d7596c8");
            reversingTrxBehavior = new Behavior();
            foreach (Behavior b in transactionCreatePolicy.Behaviors)
            {
                if (b.Key.Id == behaviorGUID)
                {
                    // Behavior was found
                    reversingTrxBehavior = b;
                    break;
                }
            }

            // Set the parameter (reversing date) for the behavior option
            DateTime reversingTrxDate;
            reversingTrxDate = new DateTime(2017, 5, 31);

            // Find the behavior option (from Web Service Reference)
            behaviorOptionID = 2;
            reversingTrxBehaviorOption = new BehaviorOption();
            foreach (BehaviorOption bo in reversingTrxBehavior.Options)
            {
                if (bo.Key.Id == behaviorOptionID)
                {
                    // Behavior option was found
                    reversingTrxBehaviorOption = bo;
                    break;
                }
            }

            // Set the reversing date for the behavior option
            reversingTrxBehaviorOption.Parameters[0].Value =
            reversingTrxDate.ToShortDateString();

            // Set selected behavior option to create a reversing transaction
            reversingTrxBehavior.SelectedOption = reversingTrxBehaviorOption;

            // Create the GL transaction
            wsDynamicsGP.CreateGLTransaction(transaction, context,
            transactionCreatePolicy);
        }
    }
}