Compartilhar via


Entity action tracking example

The following example retrieves the list of the changed item key objects for all of the items that have had actions performed in the last week. The ID value of the changed item key object is used to retrieve the full item object. Details about each item are displayed in a console window.

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

using System;
using System.Collections.Generic;
using System.Text;
using DynamicsGPWebServiceSample.DynamicsGPService;

namespace DynamicsGPWebServiceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            CompanyKey companyKey;
            Context context;
            ItemChangedKeyCriteria itemCriteria;
            ChangedItemKey[] changedItemKeyObjects;
            BetweenRestrictionOfNullableOfDateTime restriction;
            ItemKey itemKey;
            Item item;

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

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

            // Create a context with which to call the 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;

            // Create the restriction that defines the interval examined
            // The following restriction includes the last week
            restriction = new BetweenRestrictionOfNullableOfDateTime();
            restriction.From = DateTime.Today.Date.AddDays(-7);
            restriction.To = DateTime.Today.Date.AddDays(1);

            // Create the criteria to return item objects that were acted on
            itemCriteria = new ItemChangedKeyCriteria();
            itemCriteria.LastModifiedDate = restriction;

            // Retrieve the changed item key objects
            changedItemKeyObjects =
            wsDynamicsGP.GetChangedItemKeyList(itemCriteria, context);

            // Display the details about each item that acted on this week
            foreach (ChangedItemKey key in changedItemKeyObjects)
            {
                // Display the key
                Console.WriteLine("Item key: " + key.Id);

                // Display what the object knows about its changes
                Console.WriteLine("Last Modified Date: " +
                key.LastModifiedDate.ToString());

                // Get the item
                itemKey = new ItemKey();
                itemKey.Id = key.Id;
                item = wsDynamicsGP.GetItemByKey(itemKey, context);

                // Display the item description
                Console.WriteLine("Description: " + item.Description);

                // Display the operation performed
                if (key.Action == DataModificationAction.Created)
                    Console.WriteLine("Action: Created");
                if (key.Action == DataModificationAction.Updated)
                    Console.WriteLine("Action: Updated");
                if (key.Action == DataModificationAction.Deleted)
                    Console.WriteLine("Action: Deleted");

                Console.WriteLine();
            }
            Console.WriteLine("Press 'Enter' to continue...");
            Console.ReadLine();
        }
    }
}

Ff622767.NativeEndpoint(en-us,MSDN.10).gif** Native endpoint **

using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebServiceSample.DynamicsGPService;

namespace DynamicsGPWebServiceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            CompanyKey companyKey;
            Context context;
            ItemChangedKeyCriteria itemCriteria;
            ChangedItemKey[] changedItemKeyObjects;
            BetweenRestrictionOfNullableOfdateTime restriction;
            ItemKey itemKey;
            Item item;

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

            // Create a context with which to call the 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;

            // Create the restriction that defines the interval examined
            // The following restriction includes the last week
            restriction = new BetweenRestrictionOfNullableOfdateTime();
            restriction.From = DateTime.Today.Date.AddDays(-7);
            restriction.To = DateTime.Today.Date.AddDays(1);

            // Create the criteria to return item objects that were acted on
            itemCriteria = new ItemChangedKeyCriteria();
            itemCriteria.LastModifiedDate = restriction;

            // Retrieve the changed item key objects
            changedItemKeyObjects =
            wsDynamicsGP.GetChangedItemKeyList(itemCriteria, context);

            // Display the details about each item that acted on this week
            foreach (ChangedItemKey key in changedItemKeyObjects)
            {
                // Display the key
                Console.WriteLine("Item key: " + key.Id);

                // Display what the object knows about its changes
                Console.WriteLine("Last Modified Date: " +
                key.LastModifiedDate.ToString());

                // Get the item
                itemKey = new ItemKey();
                itemKey.Id = key.Id;
                item = wsDynamicsGP.GetItemByKey(itemKey, context);

                // Display the item description
                Console.WriteLine("Description: " + item.Description);

                // Display the operation performed
                if (key.Action == DataModificationAction.Created)
                    Console.WriteLine("Action: Created");
                if (key.Action == DataModificationAction.Updated)
                    Console.WriteLine("Action: Updated");
                if (key.Action == DataModificationAction.Deleted)
                    Console.WriteLine("Action: Deleted");

                Console.WriteLine();
            }
            Console.WriteLine("Press 'Enter' to continue...");
            Console.ReadLine();
        }
    }
}