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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Create new agreementlines

(0) ShareShare
ReportReport
Posted on by

Hi

I want to create a new agreementline. But when I run my job´, nothing happens.

   SalesAgreementHeader h;
    AgreementClassification c;
    AgreementLineQuantityCommitment a;

while select forupdate h
    where h.CustAccount == '123456'
    join c
        where h.AgreementClassification == c.RecId
        &&  c.NCSalesOriginId == 'ML'
        
    join a
        where a.Agreement == h.RecId
    {
        ttsBegin;
        select forUpdate a;
        a.Agreement = h.RecId;
        a.NCServiceId = 'M5CT021';
        a.ItemId = '160022';
        a.PriceUnit = 1;
        a.PricePerUnit = 0;
        a.EffectiveDate = 01\01\2016;
        a.ExpirationDate = 31\12\2016;

        a.insert();

        ttsCommit;
        info(strFmt('%1',h.SalesNumberSequence));
    }


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Brandon Wiese Profile Picture
    17,790 on at

    You have used the AgreementLineQuantityCommitment variable a in your outer query, and then tried to do a select forUpdate a within the while select block.  Create a new variable to another instance of AgreementLineQuantityCommitment and use that new variable to do your insert.

    I also doubt very much once you fix that problem whether your code will work or not.  There are more records involved in creating agreement lines than what you're working with there.  Review the various forms that create those records through the UX and see what they do.  Ultimately you need to do the same work as those forms in pretty much the same way, just with code.

  • Community Member Profile Picture
    on at

    Hi Brandon

    I will try to do that and come with the result here.

  • Community Member Profile Picture
    on at

    I tried a different job for inserting the agreementlines. But I get this error:  Cannot create a record in agreementLine. The record already exists.

    static void NcInsertNewAgreementLine(Args _args)
    {
        AgreementLine agreementLine;
        SalesAgreementHeader     salesAgreementHeader = SalesAgreementHeader::findAgreementId('DK12345');
    
        select firstOnly agreementLine where agreementLine.Agreement == salesAgreementHeader.RecId;
    
                agreementLine.Agreement = salesAgreementHeader.RecId;
                agreementLine.NCServiceId = 'ID2';
                agreementLine.ItemId = '1600';
                //agreementLineQuantity.PriceUnit = 1;
               // agreementLineQuantity.PricePerUnit = 0;
                agreementLine.EffectiveDate = 01\01\2016;
                agreementLine.ExpirationDate = 31\12\2016;
        agreementLine.reread();
        agreementLine.insert();
    }


  • Suggested answer
    Brandon Wiese Profile Picture
    17,790 on at

    You have violated the AgreementHeaderIdx by creating another record with the same Agreement and LineNumber fields as another record.

    I don't understand why you're reading an existing record, changing a few fields, and then doing another .insert() of that buffer.  That's a terrible practice that is only going to cause you problems.  If you intend to insert a new record, start with .clear(), set all of the required fields, and then .insert() it.

    Ultimately if you're going to create records through code, you have to mimic what the UX would do very closely, or the records won't work the same or at all.

  • Brandon Wiese Profile Picture
    17,790 on at

    Further, the .reread() erases all of your field changes.  So essentially you're just re-inserting the same record you just read from the database without any changes.

  • Verified answer
    Community Member Profile Picture
    on at

    Hi Brandon

    I use this job to create the the agreementline I need.

    static void NcInsertNewAgreementLineOrkide(Args _args)
    {
    
    
        SalesAgreementHeader            lSalesAgreementHeader;
        AgreementClassification         agreementClassification;
        SalesAgreementHeader            salesAgreementHeader;
        boolean 			    serviceIdExists;
    
        AgreementLineQuantityCommitment lAgreementLine;
        LineNum                         lineNum;
    
    
    
        
        while select salesAgreementHeader
            join agreementClassification
            where
            salesAgreementHeader.AgreementClassification == agreementClassification.RecId
            && agreementClassification.Name == 'someThing...'
             && salesAgreementHeader.CusTypeID == ''
    
    
        {
    
    
    
            if (salesAgreementHeader.validateWrite())
    
            {
                
                    lAgreementLine.clear();
    
                    lAgreementLine.initValue();
    
                    lAgreementLine.initFromAgreementHeader(salesAgreementHeader);
    
                    select forupdate lAgreementLine where lAgreementLine.Agreement == salesAgreementHeader.RecId;
    
                    lAgreementLine.Agreement = salesAgreementHeader.RecId;
    
                    lAgreementLine.ItemId = '142';
    
                    lAgreementLine.NCServiceId = 'TEST1';
    
                    lAgreementLine.EffectiveDate = 01\01\2016;
    
                    lAgreementLine.ExpirationDate = 31\12\2016;
    
                    lAgreementLine.CommitedQuantity = 1;
                    lAgreementLine.ProductUnitOfMeasure = 'stk';
    
                    lAgreementLine.LineNumber = lineNum;
    
    
    
    
    
                    if (lAgreementLine.validateWrite())
    
                    {
                        ttsbegin;
                        serviceIdExists = AgreementLine::ncExistsAgreementServiceItem(salesAgreementHeader.RecId, 'TEST1', '142');
    
    
                        if(!serviceIdExists)
                        {
                            {
    
                                lAgreementLine.insert();
                                lAgreementLine.salesAgreementHeader().update();
                                 info(strFmt('%1, %2, %3, %4', salesAgreementHeader.SalesNumberSequence, salesAgreementHeader.CusTypeID, lAgreementLine.ServiceId, agreementClassification.SalesOriginId    ));
    
    
                            }
                        }
                        ttscommit;
    
                    }
    
                    else
    
                    {
    
                        throw error("@SYS18447");
    
                    }
    
    
    
                }
    
        }
    
    
    
    }


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 Launch!

Jump in, show your community spirit, and win prizes!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 21

#2
dserp Profile Picture

dserp 4

#2
dekion Profile Picture

dekion 4

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans