Share via


Creating XSLT for the Create operation

To add a new document to the Dynamics GP database, use XSLT to transform the service framework document into an eConnect XML input document. To create the XSLT, complete the following steps:

  1. Create the XSLT file.

    Start Visual Studio. In the File menu, point to New, and then click File. In the New File window, select XSLT File and click Open. Visual Studio creates the file.

    If the file contains an <xsl:template /> example node, delete that node from the file.

  2. Add required namespaces.

    Add the following namespaces as attributes to the xsl:stylesheet node:

    • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    • xmlns:xsd="http://www.w3.org/2001/XMLSchema"

    • xmlns:ms ="urn:schemas-microsoft-com:xslt"

    • xmlns:gpxslt="uri://GreatPlainsTransformLibrary"

    • xmlns:gputil="urn:Microsoft.Dynamics.GP.TransformUtilities"

    • xmlns:mbs="https://schemas.microsoft.com/dynamics/2006/01"

    The following code sample shows the xsl:stylesheet node from the LeadCreate.xslt file.

    <xsl:stylesheet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ms ="urn:schemas-microsoft-com:xslt"
    xmlns:mbs="https://schemas.microsoft.com/dynamics/2006/01"
    xmlns:gpxslt="uri://GreatPlainsTransformLibrary"
    xmlns:gputil="urn:Microsoft.Dynamics.GP.TransformUtilities"
    

version="1.0">

  1. Import required libraries.

    Add an xsl:import node for the Microsoft.Dynamics.GP.StandardLibrary. The file is in the service "XSLT" folder, typically found in the following location:

    C:\Program Files\Microsoft Dynamics\GPWebServices\XSLT

    Notice how the following XSLT sample uses an xsl:import node to add the library. Also notice that the import does not include the file path for the .xslt file. The XSLT file being created must be located in the same folder as the StandardLibrary.xslt file.

    <xsl:import href="Microsoft.Dynamics.GP.StandardLibrary.xslt"/>
    
  1. Specify the templates.

    Add an xsl:template node to the stylesheet. Use the match attribute of the template node to specify the nodes in the document to transform.

    The following code sample shows the template used with a lead. Notice how the match attribute specifies to apply the template beginning at the document root. Also, the name attribute allows this template to be used in other transforms.

    <xsl:template name="Lead" match = "/">
    
    

</xsl:template>

  1. Add the document nodes.

    Add XML to the template that reflects the nodes of the eConnect XML document.

    The following example shows the XML that eConnect requires to create a lead. Notice the following:

    • The XML creates an eConnect document.

    • The document contains a SampleCreateUpdateLead node.

    • The sampleLeadCreateUpdate node specifies the name of the eConnect SQL stored procedure.

    • The child nodes of sampleLeadCreateUpdate specify the names of the input parameters of the SQL stored procedure.

    <eConnect xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SampleCreateUpdateLead>
        <sampleLeadCreateUpdate>
            <LeadID>
            </LeadID>
            <LeadName>
            </LeadName>
            <SLPRSNID>
            </SLPRSNID>
            <CITY>
            </CITY>
            <STATE>
            </STATE>
            <ZIP>
            </ZIP>
            <ADDRESS1>
            </ADDRESS1>
            <ADDRESS2>
            </ADDRESS2>
            <PHONE1>
            </PHONE1>
            <PHONE2>
            </PHONE2>
            <FAX>
            </FAX>
            <LeadBusinessCategory>
            </LeadBusinessCategory>
            <COUNTRY>
            </COUNTRY>
            <CONTACT>
            </CONTACT>
            <PotentialRevenue>
            </PotentialRevenue>
            <QualifiedLead>
            </QualifiedLead>
            <LeadSource>
            </LeadSource>
            <QualificationDate>
            </QualificationDate>
        </sampleLeadCreateUpdate>
    </SampleCreateUpdateLead>
    

</eConnect>

  1. Add values to the document nodes.

    To complete the XSLT transform, populate each node with a value from the service document.

    The Dynamics GP service framework produces a serialized version of your document. Your XSLT needs to copy the values from the nodes in your service document to the nodes of the eConnect XML document for the business object that creates records.

    The following example shows XSLT to create a lead. Notice the use of the following XSLT nodes:

    • The <xsl:value-of /> node to get the value of the node. The select= attribute specifies the name of the document property that maps to each eConnect XML document node.

    • The LeadBusinessCategory node uses <xsl:choose \> to convert the LeadBusinessCategory enumeration string value to the enumeration integer value.

    • The QualifiedLead property maps the boolean value to the value stored in the database.

    • The <xsl:if /> that ensures the QualificationDate is a valid date for Dynamics GP.

    <eConnect xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SampleCreateUpdateLead>
        <sampleLeadCreateUpdate>
            <LeadID>
                <xsl:value-of select="Lead/Key/Id"/>
            </LeadID>
            <LeadName>
                <xsl:value-of select="Lead/Name"/>
            </LeadName>
            <SLPRSNID>
                <xsl:value-of select="Lead/SalespersonID"/>
            </SLPRSNID>
            <CITY>
                <xsl:value-of select="Lead/City"/>
            </CITY>
            <STATE>
                <xsl:value-of select="Lead/State"/>
            </STATE>
            <ZIP>
                <xsl:value-of select="Lead/Zip"/>
            </ZIP>
            <ADDRESS1>
                <xsl:value-of select="Lead/Address1"/>
            </ADDRESS1>
            <ADDRESS2>
                <xsl:value-of select="Lead/Address2"/>
            </ADDRESS2>
            <PHONE1>
                <xsl:value-of select="Lead/Phone1"/>
            </PHONE1>
            <PHONE2>
                <xsl:value-of select="Lead/Phone2"/>
            </PHONE2>
            <FAX>
                <xsl:value-of select="Lead/Fax"/>
            </FAX>
            <LeadBusinessCategory>
                <xsl:choose>
                    <xsl:when test="Lead/LeadBusinessCategory=
                        'RealEstate'">1</xsl:when>
                    <xsl:when test="Lead/LeadBusinessCategory=
                        'Wholesale'">2</xsl:when>
                    <xsl:when test="Lead/LeadBusinessCategory=
                        'Retail'">3</xsl:when>
                    <xsl:when test="Lead/LeadBusinessCategory=
                        'Contractor'">4</xsl:when>
                    <xsl:when test="Lead/LeadBusinessCategory=
                        'Educational'">5</xsl:when>
                    <xsl:when test="Lead/LeadBusinessCategory=
                        'Media'">6</xsl:when>
                    <xsl:when test="Lead/LeadBusinessCategory=
                        'Software'">7</xsl:when>
                    <xsl:when test="Lead/LeadBusinessCategory=
                        'Restaurant'">8</xsl:when>
                </xsl:choose>
            </LeadBusinessCategory>
            <COUNTRY>
                <xsl:value-of select="Lead/Country"/>
            </COUNTRY>
            <CONTACT>
                <xsl:value-of select="Lead/Contact"/>
            </CONTACT>
            <PotentialRevenue>
                <xsl:value-of select="Lead/PotentialRevenue/Value"/>
            </PotentialRevenue>
            <QualifiedLead>
                <xsl:if test="Lead/QualifiedLead != ''">
                    <xsl:choose>
                        <xsl:when test="Lead/QualifiedLead=
                            'true'">2</xsl:when>
                        <xsl:otherwise>1</xsl:otherwise>
                    </xsl:choose>
                </xsl:if>
            </QualifiedLead>
            <LeadSource>
                <xsl:value-of select="Lead/LeadSource"/>
            </LeadSource>
            <QualificationDate>
                <xsl:choose>
                    <xsl:when test="Lead/QualificationDate=
                        '0001-01-01T00:00:00'">1/1/1900</xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="gputil:GetDate(
                            Lead/QualificationDate)"/>
                    </xsl:otherwise>
                </xsl:choose>
            </QualificationDate>
        </sampleLeadCreateUpdate>
    </SampleCreateUpdateLead>
    

</eConnect>

  1. Implement policy for specified nodes.

    Use the policy information that the framework adds to the document to identify behaviors that apply to a node or set of nodes. Add XSLT code that implements each behavior option for each behavior.

    The following example shows XSLT for the Lead Create Policy. The configuration of the Lead Create Policy specified by the service administrator indicates whether to use default Qualified and LeadSource values for leads created using the service.

    The transform uses the following XSLT when the "Qualified" behavior option is selected. Note the use of XSLT in the following areas:

    • The use of an xsl:if statement to identify whether the policy configuration specifies the "Qualified" behavior option. The XSLT uses the GUID of the behavior key to identify the behavior. The value of the SelectedOption/Key/Id property specifies the configured behavior option.

    • When configured to use the "Qualified" behavior option, the value of the QualifiedLead property defaults to "true".

    • The use of the Value property of the behavior parameter for the LeadSource.

    • The use of the current date to specify the QualificationDate.

    <xsl:if test="Lead/Policy/Behaviors/Behavior[
    Key/Id = '10099a58-64bf-4fa3-b35f-ed43d1b6ff9c']
    /SelectedOption/Key/Id=1">
    <QualifiedLead>2</QualifiedLead>
    <LeadSource>
        <xsl:value-of select="Lead/Policy/Behaviors/Behavior[
            Key/Id = '10099a58-64bf-4fa3-b35f-ed43d1b6ff9c']
            /SelectedOption/Parameters/Parameter[Key/Id= 1]/Value " />
    </LeadSource>
    <QualificationDate>
        <xsl:value-of select="gputil:GetCurrentDate()"/>
    </QualificationDate>
    

</xsl:if>

The transform uses the following XSLT when the "Not Qualified" behavior option is selected. When configured to use the "Not Qualified" behavior option, the QualifiedLead, LeadSource, and QualificationDate values are taken from the lead document.

<pre class="checklistscript" IsFakePre="true" xmlns="http://www.w3.org/1999/xhtml">&lt;xsl:if test="Lead/Policy/Behaviors/Behavior[
Key/Id = '10099a58-64bf-4fa3-b35f-ed43d1b6ff9c']
/SelectedOption/Key/Id=2"&gt;
&lt;QualifiedLead&gt;
    &lt;xsl:if test="Lead/QualifiedLead != ''"&gt;
        &lt;xsl:choose&gt;
            &lt;xsl:when test="Lead/QualifiedLead='true'"&gt;2&lt;/xsl:when&gt;
            &lt;xsl:otherwise&gt;1&lt;/xsl:otherwise&gt;
        &lt;/xsl:choose&gt;
    &lt;/xsl:if&gt;
&lt;/QualifiedLead&gt;
&lt;LeadSource&gt;
    &lt;xsl:value-of select="Lead/LeadSource"/&gt;
&lt;/LeadSource&gt;
&lt;QualificationDate&gt;
    &lt;xsl:choose&gt;
        &lt;xsl:when test="Lead/QualificationDate=
            '0001-01-01T00:00:00'"&gt;1/1/1900&lt;/xsl:when&gt;
        &lt;xsl:otherwise&gt;
            &lt;xsl:value-of select=
                "gputil:GetDate(Lead/QualificationDate)"/&gt;
        &lt;/xsl:otherwise&gt;
    &lt;/xsl:choose&gt;
&lt;/QualificationDate&gt;

</xsl:if>

  1. Save the file.

    In the File menu, choose Save As. In the Save File As window, enter a filename that complies with Dynamics GP Service XSLT naming requirements. Review the Save in folder, and then click Save.