Compartilhar via


Individual objects

A "GetByKey" method returns a single object specified by a key. You will create an instance of the key object for the type of object you want to retrieve. The key object's identification property must be set to the value that uniquely identifies the object to be retrieved.

If the object specified cannot be found, the Dynamics GP service will raise a "Business object not found." exception. You may want to trap for this exception in in your code and display an appropriate message for the user.

The following example demonstrates retrieving a customer object. Notice how the customer key object is created and its Id property populated with a specific customer ID value. The customer key object is then passed as the first parameter to the GetCustomerByKey method. The GetCustomerByKey method also requires a context object to be created and passed as the method's second parameter.

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

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

namespace DynamicsGPWebServiceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            CompanyKey companyKey;
            Context context;
            Customer customer;
            CustomerKey customerKey;

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

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

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

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

            // Set up the context
            context.OrganizationKey = (OrganizationKey)companyKey;

            // Create a customer key
            customerKey = new CustomerKey();
            customerKey.Id = "AARONFIT0001";

            // Retrieve the customer object
            customer = wsDynamicsGP.GetCustomerByKey(customerKey, context);

            MessageBox.Show(customer.Name);
        }
    }
}

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

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

namespace DynamicsGPWebServiceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            CompanyKey companyKey;
            Context context;
            Customer customer;
            CustomerKey customerKey;

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

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

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

            // Set up the context
            context.OrganizationKey = (OrganizationKey)companyKey;

            // Create a customer key
            customerKey = new CustomerKey();
            customerKey.Id = "AARONFIT0001";

            // Retrieve the customer object
            customer = wsDynamicsGP.GetCustomerByKey(customerKey, context);

            MessageBox.Show(customer.Name);

            // Close the service
            if (wsDynamicsGP.State != CommunicationState.Faulted)
            {
                wsDynamicsGP.Close();
            }
        }
    }
}