Update questionnaire answers from X++ code

Change \Classes\KMQuestionnaireAnswerControl_Win32\getControlValue methods from private to protected
Create new class which looks like:
class KMQuestionnaireAnswerControl_X extends KMQuestionnaireAnswerControl_Win32
{
    KMVirtualNetworkAnswerAnswer answer[];
}
protected KMVirtualNetworkAnswerAnswer getControlValue(Counter _counter)
{
    return answer[_counter];
}
public void setValue(int _seq, str _value)
{
    answer[_seq] = _value;
}
public static KMQuestionnaireAnswerControl_X construct()
{
    return new KMQuestionnaireAnswerControl_X();
}
How to save questionnaire answers from X++ code
static void testKMQuestionnaireSave(Args _args)
{
    // \Data Dictionary\Tables\KMCollection\Fields\kmCollectionId
    KMCollectionId                      collectionId;   
    // \Data Dictionary\Tables\KMVirtualNetworkAnswerLine\Fields\kmQuestionId
    // \Data Dictionary\Tables\KMCollectionQuestion\Fields\kmQuestionId
    KMQuestionId                        questionId;     
    // \Data Dictionary\Tables\KMVirtualNetworkAnswerTable\Fields\kmVirtualNetworkAnswerTableId
    // Might be empty if it is new answer
    KMVirtualNetworkAnswerTableId       tableAnswerId;
    
    KMVirtualNetworkAnswerLine          kMVirtualNetworkAnswerLine;
    KmQuestionnaireSave                 kmQuestionnaireSave;
    KMQuestionnaireIterator             kmQuestionnaireIterator;
    KMQuestionnaireAnswerControl_X      answerControl;
    KMCollectionQuestion                kMCollectionQuestion;
    HcmPersonRecId                      person;
    str                                 answer;
    int                                 seqCounter;

    person = DirPersonUser::current().PersonParty;
    if (!person)
    {
        throw warning(strfmt("@SYS32944", curuserid()));
    }
    
    // create as much answers as you Questionnaire requires
    answerControl = KMQuestionnaireAnswerControl_X::construct();
    answerControl.setQuestion(questionId);
    answerControl.setValue(seqCounter, answer);
    
    // delete all previous answers if any
    delete_from kMVirtualNetworkAnswerLine
        where kMVirtualNetworkAnswerLine.kmVirtualNetworkAnswerTableId == tableAnswerId;
    
    kmQuestionnaireIterator = new KMQuestionnaireIterator(collectionId);
    kmQuestionnaireIterator.generateArray();
    kmQuestionnaireSave = KmQuestionnaireSave::construct(
            kmQuestionnaireIterator.mandatoryQuestions(),
            tableAnswerId,
            collectionId,
            person);

    kMCollectionQuestion = KMCollectionQuestion::find(collectionId, questionId);
    // Add all your answers. It saves to TMP table
    kmQuestionnaireSave.save(kMCollectionQuestion, answerControl, kMCollectionQuestion.mandatory, false);

    
    // Actual write to DB.
    kmQuestionnaireSave.close();
}

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics AX.

Authors are allowed to set their own AdSense units.
Join us.

Blog Tags