Tuesday, March 1, 2011

Set the MarkupGroup from the SalesCreateQuotation Form - Microsoft Dynamics Ax 2009

Today I had a requirement to automatically set the MarkupGroup at the moment a user creates a new Sales Quote in AX.


In the past the user would have had to do this manually but with the following changes in my code this happens in the background.


The place where we want to change this is in the salesQuotationTable.writeCreateQuotation Method. This method can be accessed from:


SalesCreateQuotation Form > DataSources > SalesQuotationTable > Methods > Write



The code is as follows:


void write()
{
    ;

    if (!element.closedOk())
        return;

    try
    {
        salesQuotationTable.writeCreateQuotation(salesQuotationTableType, element, salesQuotationTable_ds);
    }
    catch(Exception::Error)
    {
        salesQuotationTable.RecId = 0;
        element.close();
        throw Exception::Error;
    }

    salesQuotationTable_ds.reread();
    salesQuotationTable_ds.refresh();

    salesQuotationTableForm.newQuotationId(salesQuotationTable.QuotationId);

    cancel.enabled(false);
}




Then, in the WriteCreateQuotation method I had to assign the GroupId from the MarkupGroup table to my local variable called MarkupGroup by using the following code:

this.MarkupGroup = MarkupGroup::find(ModuleInventCustVend::Cust, this.Dimension[1]).GroupId;

The complete method code is shown below:

server void writeCreateQuotation(SalesQuotationTableType salesQuotationTableType, FormRun element, FormDataSource salesQuotationTable_ds)
{
    smmActivities               smmActivities;
    SalesQuotationTable         salesQuotationCRM;
    smmOpportunityTable         smmOpportunityTable;
    smmActivityParentLinkTable  smmActivityParentLinkTable;
    ;

    salesQuotationCRM = SalesQuotationTable::find(this.QuotationId, true);

    ttsbegin;
    //Earias - 3/1/2011
    this.DlvResponsibility = DlvResponsibility::find('Prepaid & Add', false).Code;
    this.MarkupGroup = MarkupGroup::find(ModuleInventCustVend::Cust, this.Dimension[1]).GroupId;
    salesQuotationTableType.formMethodDataSourceWrite(element, salesQuotationTable_ds);
    if (!this.RecId)
    {
        this.insert();
    }

    ttscommit;

    // Create CRM activity
    if (!this.QuotationFollowupActivity && this.SalesResponsible && this.QuotationExpiryDate)
    {
        smmActivities = smmActivities::createActivity(this);
        this.QuotationFollowupActivity = smmActivities.ActivityNumber;
    }

    // Create opportunity
    if (smmLicense::CRM())
    {
        if (!this.OpportunityId)
        {
            smmOpportunityTable = smmCreateEntity::createOpportunityFromQuotation(this);
        }
        else // Existing opportunity has been selected
        {
            if (smmActivities)
            {
                // Attach the activity created to the current stage since activity is not saved yet during the insert of parent link
                smmActivityParentLinkTable = smmActivityParentLinkTable::findParentLink(smmActivities.ActivityNumber, smmActivityParentType::Opportunity);
                smmProcessInstance::insertActivityToStage(smmActivityParentLinkTable);
            }
        }
    }

    if (smmActivities || smmOpportunityTable)
    {
        ttsbegin;

        //Earias - 3/1/2011
        salesQuotationCRM = SalesQuotationTable::find(this.QuotationId, true);

        if (smmActivities)
            salesQuotationCRM.QuotationFollowupActivity = smmActivities.ActivityNumber;

        if (smmOpportunityTable)
            salesQuotationCRM.OpportunityId = smmOpportunityTable.OpportunityId;

        salesQuotationCRM.doUpdate();

        ttscommit;
    }
}

No comments:

Post a Comment

Thank you for your thoughts. Your comment will appear in my blog shortly after review.

Have a great day!