Post Details

How can I check if a record for Worker (WHSWorker) with the ID xxxx xxxx, 14272 already exists in D365FO using X++?

You can check if a record for a Worker with a specific ID already exists in D365FO using X++ by creating a query using the Worker data entity, filtering based on the Worker's ID, and then checking the count of records returned by the query. Here is an example of how you can do this:


                            
static void CheckWorkerRecordExists(Args _args)
{
    WHSWorker worker;
    Query query;
    QueryRun queryRun;
    
    // Define the Worker ID to check for
    str workerId = 'xxxx xxxx';
    
    // Create a query for the WHSWorker table
    query = new Query();
    query.dataSourceTable(tableNum(WHSWorker));
    query.addRange(fieldNum(WHSWorker, WorkerRecId)).value(SysQuery::value(workerId));
    
    // Execute the query
    queryRun = new QueryRun(query);
    
    // Check if any records were returned
    if (queryRun.prompt())
    {
        info(strFmt("Worker record with ID '%1' already exists", workerId));
    }
    else
    {
        info(strFmt("Worker record with ID '%1' does not exist", workerId));
    }
}

                        

` You can run this code in the X++ development environment in D365FO to see if a record for the Worker with the specified ID already exists in the system.


Leave A Reply


Name*

E-mail*