Showing posts with label sourceDocumentLine. Show all posts
Showing posts with label sourceDocumentLine. Show all posts

Friday, May 26, 2017

The state of the source document or source document line could not be updated Ax2012

Hi Guys,

Today we faced this in our production environment. A particular PO had developed this issue wherein whenever a user clicks the Invoice button system would throw the error "The state of the source document or source document line could not be updated.".

While debugging, I came to know that this had something to do with Source Document Line records. So I tried looking for a solution on the web and got the below job which works perfectly in deleting the orphan records from SourceDocumentLine:

static void aks_fixOrphanedSourceDocumentsHeader(Args _args)
{
    SourceDocumentLine sline;
    SysDictTable table;
    PurchTable header;
    PurchLine purchline;
    PurchId purchId = "WPFO1617-0001218";
    boolean fix;
    Common rec;
    int fieldId, found, notfound;

    if (purchId)
    {
        while select purchLine where purchLine.PurchId == purchId
        {
            while select forUpdate sline where sline.ParentSourceDocumentLine == purchLine.SourceDocumentLine
            {
                table = new SysDictTable(sline.SourceRelationType);
                rec = table.makeRecord();
                fieldId = fieldName2id(sline.SourceRelationType, "SourceDocumentLine");
                select rec where rec.(fieldId) == sline.RecId;

                if (rec.RecId)
                {
                    info(strFmt("Record Match Found %1 %2", table.name(),rec.caption()));
                    found++;
                }
                else
                {
                    ttsBegin;
                    sline.doDelete();
                    ttsCommit;

                    info(strFmt("Orphan Found %1", table.name()));
                    notfound++;
                }
            }
            info(strFmt("Found %1", found));
            info(strFmt("Orphans found and deleted %1",notfound));

            found = 0;
            notfound = 0;
        }
    }
}


Credit: https://community.dynamics.com/ax/f/33/t/144316