Share via


Defining the document type

Create a class that defines the data members of your document. To add the class to your class library, complete the following steps:

  1. Create a class.

    In the Visual Studio Project menu, choose Add New Item. Select Class from the list of Templates in the Add New Item window. Enter your document type in the Name box, and then click Add. Visual Studio adds the class to the project.

    For example, you would enter Lead.cs in the Name box to create the Lead class for the Lead document type. The following sample code shows the Lead class. Notice how the namespace and class name specify the Lead document type.

    using System;
    

using System.Collections.Generic; using System.Text

namespace Microsoft.Dynamics.GP.Samples.Leads { class Lead {

}

}

  1. Add namespace references.

    To simplify the use of several classes and methods, add using statements for the following namespaces:

    • System.Runtime.InteropServices

    • System.Xml.Serialization

    • Microsoft.Dynamics.Common

    • Microsoft.Dynamics.GP

  2. Add class attributes.

    Use attributes to mark your class as serializable and a data contract. Use a GUID attribute to make your class uniquely identifiable.

    Hint: To generate a GUID for the attribute, open the Tools menu in Visual Studio and choose Create GUID. In the GUID Format list, mark Registry Format. Click New GUID and then copy the GUID to your class attribute.

    The following sample code shows the attributes for the Lead class. Notice how the GuidAttribute does not include the braces for the GUID.

    [Serializable, DataContract]
    

[GuidAttribute("2852BB26-3BA8-4663-9613-033327D7F3C2")] class Lead {

}

  1. Inherit from the BusinessObject class.

    Add Microsoft.Dynamics.Common.BusinessObject as the base class for your document class. Also, specify the class as public.

    The following sample code shows how the Lead class inherits from the BusinessObject class.

    public class Lead : BusinessObject
    

{

}

  1. Add the class data members.

    Add private data members to your document class for each data field in your document. Each data member must be a data type that is used in the Dynamics GP service. For more information on data types, see Data types.

    The following sample code adds the data members for lead documents to the Lead class. While most of the data members use standard data types, a lead document also includes the following data types:

    • The key data member uses the LeadKey class to uniquely identify the lead document. For more information about how to create a key, see Defining the key.

    • The phone1, phone2, and fax data members are PhoneNumber data types from the the Microsoft.Dynamics.Common namespace.

    • The leadBusinessCategory data member uses an enumeration to identify the business category for the lead. For more information about how to create an enumeration, see Adding an enumeration.

    • The potentialRevenue data member is a MoneyAmount data type from the Microsoft.Dynamics.Common namespace.

    Warning: The potentialRevenue data member is not private. To allow updates to the Currency and DecimalDigit properties of a MoneyAmount data member, the data member cannot be private.

    private LeadKey key;
    

private string name; private string salespersonID; private string city; private string state; private string zip; private string address1; private string address2; private PhoneNumber phone1; private PhoneNumber phone2; private PhoneNumber fax; private LeadCategory leadBusinessCategory; private string country; private string contact; MoneyAmount potentialRevenue; private bool qualifiedLead; private string leadSource; private DateTime qualificationDate; private string workflowApprovalStatus; private string workflowPriority; private string approvedSalespersonID; private DateTime modifiedDate;

  1. Add a class property for each data member.

    After you define the data members of the class, you must add a class property that provides access to each data member. The property allows you to set or retrieve the value of each data member. Add the properties to your document class immediately after the data member definitions.

    Use the DataMember attribute to identify each property as a data member. In addition, set the EmitDefaultValue property to false for all reference types and non-nullable enumeration types.

    The following sample code adds class properties for leads:

    [DataMember(EmitDefaultValue = false)]
    

public LeadKey Key { get{return key} set{key = value; BaseKey = value; } } [DataMember(EmitDefaultValue = false)] public string Name { get{return name;} set{name = value;} } [DataMember(EmitDefaultValue = false)] public string SalespersonID { get{return salespersonID;} set{salespersonID = value;} } [DataMember(EmitDefaultValue = false)] public string City { get{return city;} set{city = value;} } [DataMember(EmitDefaultValue = false)] public string State { get{return state;} set{ state = value;} } [DataMember(EmitDefaultValue = false)] public string Zip { get{return zip;} set{zip = value;} } [DataMember(EmitDefaultValue = false)] public string Address1 { get{return address1;} set{address1 = value;} } [DataMember(EmitDefaultValue = false)] public string Address2 { get{return address2;} set{address2 = value;} } [DataMember(EmitDefaultValue = false)] public PhoneNumber Phone1 { get{return phone1;} set{phone1 = value;} } [DataMember(EmitDefaultValue = false)] public PhoneNumber Phone2 { get{return phone2;} set{phone2 = value;} } [DataMember(EmitDefaultValue = false)] public PhoneNumber Fax { get{return fax;} set{fax = value;} } [DataMember(EmitDefaultValue = false)] public LeadCategory LeadBusinessCategory { get{return leadBusinessCategory;} set{leadBusinessCategory = value;} } [DataMember(EmitDefaultValue = false)] public string Country { get{return country;} set{country = value;} } [DataMember(EmitDefaultValue = false)] public string Contact { get{return contact;} set{contact = value;} } [DataMember(EmitDefaultValue = false)] public MoneyAmount PotentialRevenue { get{return potentialRevenue;} set{potentialRevenue = value;} } [DataMember(EmitDefaultValue = false)] public bool QualifiedLead { get{return qualifiedLead;} set{qualifiedLead = value;} } [DataMember] public string LeadSource { get{return leadSource;} set{leadSource = value;} } [DataMember] public DateTime QualificationDate { get{return qualificationDate;} set{qualificationDate = value;} } [DataMember(EmitDefaultValue = false)] public string WorflowApprovalStatus { get{return workflowApprovalStatus;} set{workflowApprovalStatus = value;} } [DataMember(EmitDefaultValue = false)] public string WorkflowPriority { get{return workflowPriority;} set{workflowPriority = value;} } [DataMember(EmitDefaultValue = false)] public string ApprovedSalespersonID { get{return approvedSalespersonID;} set{approvedSalespersonID = value;} } [DataMember] public DateTime ModifiedDate { get{return modifiedDate;} set{modifiedDate = value;} }

  1. Add a private method to initialize data fields.

    Add a method to specify the initial value of specified data field. Typically, the initialize method provides values for the data fields that cannot be null. The constructor and OnDeserializing methods use this method.

    The following sample code shows the Initialize method for the Lead class. Notice how the Key and PotentialRevenue properties are initialized. The Key is an object that identifies the document. For more information about how to create a key, see Defining the key.

    The GetInstance method of the MoneyAmount class creates a new MoneyAmount object. In this example, the object has a value of zero, a currency of US dollars, and supports two decimal places.

    private void Initialize()
    

{ Key = new LeadKey();

PotentialRevenue = MoneyAmount.GetInstance(0m, "USD", 2);

}

  1. Add a constructor and OnDeserializing method.

    Create a class constructor and OnDeserializing method. The OnDeserializing method enables your class to be serialized by the Dynamics GP Service framework. Call the Initialize method from both of these methods.

    The following sample code shows the constructor and OnDeserializing method for the Lead class. Notice how the OnDeserializing attribute is added to the OnDeserializing method.

    public Lead()
    

{ Initialize(); } [OnDeserializing] public new void OnDeserializing(StreamingContext context) { Initialize(); }

  1. Save the class.

    From the File menu, choose Save.