prodcut item can have only one active document of each type in ax 2012

Hello!,

I am using Ax2012!.

I want to attach a document for a particular item. While attaching I have to check whether the item already has an active(External) document of document type I am attaching.

If the item already has an active document then I have to throw an error.

For further reference please see the below Image,

I tried the following code,

if(docuTypeId)
    {
        while select TypeId,Restriction from docuRef
        where docuRef.TypeId == docuTypeId &&
        docuRef.Restriction == DocuRestriction::External &&
        docuRef.RecId
        {
         throw error("Already Exist");
        }
    }

In the DocuAction class - NewArgs Method. It works fine for an item . But if i add document for some other item it also throws an error stating that the type already exist because it checks the Docuref table on whole. So i dont know how to mention it for a particular item alone . Cos i couldnt able get the RefRecId field in docuref table.

Helpl me with the solution.

Thanks

You also have to check RefTableId and RefRecId in your select (in this case it would be EcoRedProduct)
You don’t need a while select, check for the existence of single record(use firstonly).
You can also have your code in \Data Dictionary\Tables\DocuRef\Methods\validateWrite

But I dont know how to get the values of RefRecId or RefTableId. can u please help me with how to get the values?

Thanks

If you have your code in validateWrite then you can get from current buffer (this.refRecId).

Huh yeah !. I ll try and get back to you.
Thanks

Hello

I tried writing the code in validate write method. Its working fine, But I got anther problem. When i try to update an external document I couldn’t able to update it as it check for the code and throws the same error.

Please check the following image for better clarification,

This is the code I used,

 select firstonly TypeId,Restriction from docuRef
        where docuRef.TypeId == this.TypeId &&
        docuRef.Restriction == DocuRestriction::External &&
        docuRef.TableId == this.TableId &&
        docuRef.RefRecId == a;
        if(docuRef)
    {
        info(strFmt('%1',docuRef.RefRecId));
         throw error("Already Exist");

    }

Thanks

You should be running for the specific scenario only.
Example: for products - check the tableId

if (this.RefTableId == tableNum(EcoResProdcut)
{
select firstonly RecId from docuRef
where docuRef.RefCompanyId== this.RefCompanyId &&
docuRef.Restriction == DocuRestriction::External &&
docuRef.RefTableId== this.RefTableId &&
docuRef.RefRecId == this.RefRecId;

if (docuRef.RecId)
{
info(strFmt(’%1’,docuRef.RefRecId));
throw error(“Already Exist”);

}
}

I am still getting the same problem, when i try to update the field from external to internal as the if statement is getting satisfied.
Thanks.

if (!this.RecId) → add this condition before executing your code.

Thanks, Its working now.