Tuesday, August 25, 2015

Create .doc file from template in AX 2009

Requirement: Create a probationary complete letter (.doc file) on click of check box “Probation completed” from employee details.
Process:
Step 1: Create template and save it to location name: “D:\TestDocTemplate\ProbationaryCompleteTemplate.dotx” with few book marks


 Step 2: Create a field (Boolean: NoYes) in EmplTable and added to field group “Administration”
                Label: Probation completed
On click of check box a doc file will open with info log of file location and name

Technical approach:
Create a class with name CreateProbationLetter with below methods
public class CreateProbationLetter
{

    EmplTable   emplTable;

    FileName    template;
    FileName    probationLetter;

    str         DocumentDate;

    COM         word;//Application
    COM         wordDocuments;
    COM         document;
    COM         bookmarks;

    #define.word('Word.Application')

}
public static CreateProbationLetter construct()
{
    CreateProbationLetter  createProbationLetter;
;
createProbationLetter = new CreateProbationLetter();
    return createProbationLetter;
}
EmplTable parmEmplTable(EmplTable _emplTable = emplTable)
{
    ;
    emplTable = _emplTable;

    return  emplTable;
}
FileName parmTemplateName(FileName _template = template)
{
    ;
    template = _template;
    return template;
}
void openWord()
{
    COM documents;
    ;
    try
    {
        word = new COM(#Word);
    }
    catch (Exception::Internal)
    {
        if (word == null)
        {
            throw error("Microsoft Word is not installed.");
        }
    }
    documents = word.documents();
    document = documents.add(template);
}
void processBookmark(str _name, anytype _value)
{
    COM bookmark;
    COM range;
    ;
    if(!bookmarks.exists(_name))
    {
        return;
    }
    bookmark = bookmarks.item(_name);
    range = bookmark.range();
    range.insertAfter(_value);

}
str setDocumentDate()
{
    str day, mth,yr, rptDate;
    ;
    //----- Date display------ begin
    day = int2str(dayOfMth(systemdateget()));
    yr = int2str(year(systemdateget()));
    //----- Date display------ end

    DocumentDate = day+' '+mthName(mthOfyr(systemdateget()))+' '+yr;
    return DocumentDate;
}
FileName setfileName()
{
    container fileNameContainer;
    ;
    fileNameContainer = Global::fileNameSplit(template);
    probationLetter = conpeek(fileNameContainer,1)+
DirPartyTable::find(emplTable.PartyId).FirstName+
DirPartyTable::find(emplTable.PartyId).MiddleName+
'_ProbationCompletionConfirmation_'+
this.setDocumentDate()+
'.doc';
    return probationLetter;
}
void run()
{
    ;
    this.openWord();
    bookmarks = document.bookmarks();
    this.processBookmark('EMPLOYEETITLE',emplTable.Title);
    this.processBookmark('CompanyName',CompanyInfo::find().Name);
    this.processBookmark('DocumentDate',this.setDocumentDate());
    this.processBookmark('EmployeeFirstName',' '+DirPartyTable::find(emplTable.PartyId).FirstName);
    this.processBookmark('EmployeeFullName',DirPartyTable::find(emplTable.PartyId).Name);
    word.visible(true);
    wordDocuments = word.Documents();
    document.saveas(this.setfileName());
    document.save();
    //document.close();
    //word.quit();
    info(strfmt('Please check file at: %1',this.setfileName()));
}

From EmplTable: create a method with name: createProbationLetter and call this new method in modified field for newly created Boolean field “smlProbationCompleted
//Created by HS for probation complete letter generation
void createProbationLetter()
{
    CreateProbationLetter  createProbationLetter = new CreateProbationLetter();
    ;
    createProbationLetter.parmEmplTable(this);
    createProbationLetter.parmTemplateName(@D:\TestDocTemplate\ProbationaryCompleteTemplate.dotx ');
    createProbationLetter.run();
}
/// <summary>
///    When fields are modified this method is called. The fields are updated using the AxEmplTable class. Note: super-method is never called.
///    The map HRMPersonNameMap is used to update name fields in
///    HRMApplicantTable.
/// </summary>
/// <param name="_fieldId">
///    Id of the field being modified.
/// </param>
public void modifiedField(fieldId _fieldId)
{
    AxEmplTable     axEmplTable;
    Object          formDataSource;
    ;

    super(_fieldId);

    if (this.isFormDataSource())
    {
        if (formDataSourceHasMethod(this.dataSource(),classstr(AxEmplTable)))
        {
            formDataSource  = this.dataSource();
            axEmplTable     = formDataSource.axEmplTable();
        }
    }
    else
    {
        axEmplTable = this.axEmplTable();
    }

    if (axEmplTable)
    {
        axEmplTable.setFieldAsTouched(_fieldId);
        axEmplTable.modify();
    }
    else
    {
        // HRM-start
        switch(_fieldId)
        {
            case fieldnum(EmplTable, PartyId) :
                this.initFromDirParty(DirParty::constructFromPartyId(this.PartyId));
                break;
            //Created by Harshita on 21stAugust 2015- begin
            case fieldnum( EmplTable, smlProbationCompleted):
                            if(this.smlProbationCompleted == Noyes::Yes)
                                this.createProbationLetter();
            break;
            //Created by Harshita on 21stAugust 2015- end
        }
        // HRM-end

    }
}

Result:
On click of the check box “Probation completed” a doc file will generate based on template with info log
Info log:
File: Name “Charlie_ProbationCompletionConfirmation_25 August 2015.doc”

No comments:

Post a Comment