12 Aralık 2016 Pazartesi

AX 2012 - Run Questionnaires with X++ and get Answer table answer Id

There are lots of pages about questionnaires at the web. So I won't tell anything about using and run
questionnaires from menus.

KMCollection - Questionnaire records table. kmCollectionId is key field.

KMVirtualNetworkAnswerTable - Answers table.  kmVirtualNetworkAnswerTableId is key field. Result is saved at evaluation field as failed/passed or empty.
KMVirtualNetworkAnswerLine - Answers lines table.

Run Questionnaire with code:
  
KMQuestionnaireRun       o = new CrsKMQuestionnaireRun();
 

o.set(KMQuestionnaireRunMode::All,myKMCollectionId, DirPersonUser::find(curUserId()).party());
o.run();
Run Answer form with code:

KMVirtualNetworkAnswerTable answerTable;
Args                        args = new Args();
RecId                       recId = DirPersonUser::find(curUserId()).party();
 

select firstOnly answerTable
        where answerTable.kmVirtualNetworkAnswerTableId == this.AnswerId;

args.record(answerTable);args.caller(o);
    new MenuFunction(menuItemDisplayStr(KMKnowledgeCollectorUserResults), MenuItemType::Display).run(args);


I extended KMQuestionnaireRun_Win32 class for save KMVirtualNetworkAnswerTable's ID field to my table at my caller form:

class myKMQuestionnaireRun extends KMQuestionnaireRun_Win32
{
    recId   myRecId;
}


I added a parm method for save caller table RecId:

RecId  parmMyRecId(RecId _recId = myRecId)
{
    myRecId =_recId;
    return myRecId;
}


Derived end method of MQuestionnaireRun_Win32:

public void end(KMVirtualNetworkAnswerTableId _kmVirtualNetworkAnswerTableId)
{
    myCallerTable    tbl; //Questionnaries formunu çağıran tablo

    super(_kmVirtualNetworkAnswerTableId);
    if (_kmVirtualNetworkAnswerTableId == "")
        return;
  

      ttsBegin;
        update_recordSet tbl setting AnswerId = _kmVirtualNetworkAnswerTableId
            where tbl.RecId == myRecId;
        ttsCommit;
}

 
And call from my table:
...
myKMQuestionnaireRun       o = new myKMQuestionnaireRun();
myQTable            c;


    o.parmMyRecId(this.RecId);
    o.set(KMQuestionnaireRunMode::All,this.KMCollectionId, DirPersonUser::find(curUserId()).party());

    o.run();

...