Fork me on GitHub

Checking if a Specific Record Exists in a Table [AX2012]

As a Method

Class declaration

class YourClass
{
    InventSerial inventSerial;
}

Method

public static boolean existsSerial(str 20 _serial)
{
    return _serial
        && (select firstOnly RecId from inventSerial
            /* index hint GroupIdx //index hint is IGNORED in AX 2012. */
            where inventSerial.InventSerialId == _serial).RecId != 0;
}

As a Service

Service data contract attribute

[DataContractAttribute]
class YourServiceDataContract
{
    boolean existSerial;
}

Service data member attribute

[DataMemberAttribute('Exist AX Serial')]
public boolean parmExistSerial(boolean _existSerial = existSerial)
{
    existSerial = _existSerial;
    return existSerial;
}

Class declaration

class YourService
{
    InventSerial inventSerial;
    YourServiceDataContract dataContract;
}

Service operation

[SysEntryPointAttribute(true), AifCollectionTypeAttribute('return', Types::Enum)]
public YourServiceDataContract existSerials(str 20 _serial)
{
    dataContract = new YourServiceDataContract();

    try
    {
        select firstOnly InventSerialId from inventSerial
        where inventSerial.InventSerialId == _serial;
    }
    catch(Exception::error)
    {
        error("There was an error getting the record.");
    }

    if(inventSerial.RecId != 0)
    {
        dataContract.parmExistSerial(true);
    }
    else
    {
        dataContract.parmExistSerial(false);
    }

    return dataContract;
}

A Job to test them both

static void checkSerial(Args _args)
{
    YourService yourService = new YourService();
    YourClass yourClass = new YourClass();

    // Testing method
    info(strFmt("%1", yourClass::existSerial("0000009745")));

    // Testing service
    info(strFmt("%1", yourService.existSerial("0000009745").parmExistSerial()));
}

Comments !

links

social