web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Create New record (with code) after Workflow Submit

(0) ShareShare
ReportReport
Posted on by

Hello everyone, som i'm trying to create a new record in Planned answer Session after i submit my workflow i used these code but it doesn't work 

  public void completed(WorkflowElementEventArgs _workflowElementEventArgs)
	{
		// TODO:  Write code to execute once the workflow is completed.
        RecId documentRecId = _workflowElementEventArgs.parmWorkflowContext().parmRecId();
        NouvelleMesureTable::updateWorkflowStatus(documentRecId, GD_WFDocumentStatus::Valide);
        FormControl sender;
        FormRun formRun;
        Args args = new Args();
        KMVirtualNetworkAnswerTable kMVirtualNetworkAnswerTable;
        NouvelleMesureTable nouvelleMesureTable;
        FormDataSource nouvelleMesureTable_ds  = sender.formRun().dataSource(formDataSourceStr(NouvelleMesureDisciplinaire,NouvelleMesureTable))as
            FormDataSource;
       
        
       
        ttsbegin;
        kMVirtualNetworkAnswerTable.ExecutedBy=nouvelleMesureTable.Person;
        kMVirtualNetworkAnswerTable.EvaluationOf=nouvelleMesureTable.Person;
        kMVirtualNetworkAnswerTable.kmCollectionId=nouvelleMesureTable.Questionnaire;

        ttscommit;

        args.name(formStr(NouvelleMesureDisciplinaire));
        formRun = classFactory.formRunClass(args);
        formRun.init();
        formRun.run();
	}

thank you.

  • Suggested answer
    Komi Siabi Profile Picture
    13,253 Most Valuable Professional on at

    Bonjour wassim77,

    Your code seems a bit confusing with ttsbegin while you are trying to insert a record and no insert() method anywhere.

    If I get you right, you want to pick record from your custom table and insert it into kMVirtualNetworkAnswerTable when the current document is fully approved.

    A better way, I would achieve this is by having a method createkMVirtualNetworkAnswerTable() on NouvelleMesureTable where I will define the fields and mapping for insertion.

    On the workflowEventHandler, I would just call the insertIntokMVirtualNetworkAnswerTable and pass the documentId.

    /// 
    /// 
    /// 
    public void createkMVirtualNetworkAnswerTable(RecId _recId)
    {
        
        kMVirtualNetworkAnswerTable kMVirtualNetworkAnswerTable;
        NouvelleMesureTable nouvelleMesureTable = NouvelleMesureTable::find(_recId);
        
        kMVirtualNetworkAnswerTable.clear();
        kMVirtualNetworkAnswerTable.ExecutedBy=nouvelleMesureTable.Person;
        kMVirtualNetworkAnswerTable.EvaluationOf=nouvelleMesureTable.Person;
        kMVirtualNetworkAnswerTable.kmCollectionId=nouvelleMesureTable.Questionnaire;
        .......
        .......
        kMVirtualNetworkAnswerTable.insert();
    }

    public void completed(WorkflowElementEventArgs _workflowElementEventArgs)
    {
    	// TODO:  Write code to execute once the workflow is completed.
        RecId documentRecId = _workflowElementEventArgs.parmWorkflowContext().parmRecId();
        NouvelleMesureTable::updateWorkflowStatus(documentRecId, GD_WFDocumentStatus::Valide);
        
        // Here we call the method for inserting record into KMVirtualNetworkAnswerTable
        NouvelleMesureTable.createkMVirtualNetworkAnswerTable(documentRecId);
    }

  • Suggested answer
    GirishS Profile Picture
    27,845 Moderator on at

    Hi wassim77,

    You are saying that you need to create a record in table after submission, But you have added the code in completed event handler. Completed event handler will be called once workflow is completed.

    You need to write the logic in the started event handler which will be called once you submit the workflow.

    Thanks,

    Girish S.

  • Community Member Profile Picture
    on at

    thank you sir but i'm these error

    "Cannot create a record in Plan a questionnaire (KMVirtualNetworkAnswerTable). The record already exists."

    when i try to submit

  • GirishS Profile Picture
    27,845 Moderator on at

    This error is throwing because you have unique index on a table. You need to validate the record before inserting.

    If KMColectionId is unique index field in the table, then you need to add select statement to check whether the KMCollectionId is already there in the table.

    Thanks,

    Girish S.

  • Community Member Profile Picture
    on at

    how do i do that?

  • GirishS Profile Picture
    27,845 Moderator on at

    Can you tell me the unique index on the KMVirtualNetworkAnswerTable table?

    Thanks,

    Girish S.

  • Community Member Profile Picture
    on at

    okey so the index for the table is IdIdx and the Allow Duplicate is set to No

  • GirishS Profile Picture
    27,845 Moderator on at

    No  IdIdx is index name. You need to expand that and see what are all the fields added inside.

    Thanks,

    Girish S.

  • Suggested answer
    Komi Siabi Profile Picture
    13,253 Most Valuable Professional on at

    Hello wassim77,

    Here is what Girish is talking about.

    KMVirtualNetworkAnswerTable.png

    Before inserting, you need to check if the record exist.

    select firstonly kMVirtualNetworkAnswerTable
            where kMVirtualNetworkAnswerTable.kmVirtualNetworkAnswerTableId =""
    
    if(!kMVirtualNetworkAnswerTable.RecId) 
    { 
      //insert here
    }
  • Community Member Profile Picture
    on at

    i'm getting an error (join expected)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 347 Super User 2026 Season 2

#3
CU-1234529-002 Profile Picture

CU-1234529-002 197

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans