1

I'm using Manufacturing Cloud and I need to write a test class using Sales Agreement in Activated Status. Unfortunately, Salesforce returns DML exceptions if I try to make an update to Status field.

  • Approved - UNKNOWN_EXCEPTION, Save other edits before changing the status.: [Status]
  • Activated - INVALID_STATUS, You can't change the status from Draft to Activated.: [Status]

Approval Process are disabled in Sales Agreement Settings enter image description here

Sample code in test class to attempt creating activated Sales Agreement:

@IsTest
    static void approveSalesAgreement(){
        Account acc = new Account(Name='Test');
        insert acc;


        Product2 product = new Product2(Name='productTest');
        insert product;
        PricebookEntry pe = new PricebookEntry(Product2Id = product.Id, Pricebook2Id = Test.getStandardPricebookId(), UnitPrice = 1);
        insert pe;
        
        SalesAgreement salesAgreement = new SalesAgreement(ProductLevel = 'Product',ScheduleFrequency = 'Monthly', StartDate = System.today().addMonths(-1), ScheduleCount = 5, Name = 'any', PricebookId = Test.getStandardPricebookId(), AccountId = acc.Id);
        insert salesAgreement;

        SalesAgreementProduct sap = new SalesAgreementProduct(SalesAgreementId = salesAgreement.Id, PricebookEntryId = pe.Id, InitialPlannedQuantity = 1, Name = product.Name);
        insert sap;


        update new SalesAgreement(Status = 'Approved', Id = salesAgreement.Id);   // System.DmlException: Update failed. First exception on row 0 with id 0YA8E0000004CJwWAM; first error: UNKNOWN_EXCEPTION, Save other edits before changing the status.: [Status]
//        update new SalesAgreement(Status = 'Activated', Id = salesAgreement.Id);    //System.DmlException: Update failed. First exception on row 0 with id 0YA8E0000004CK1WAM; first error: INVALID_STATUS, You can't change the status from Draft to Activated.: [Status]

    }

1 Answer 1

0

Your Issue is because of missing "ActualsCalculationMode" on the Sales Agreement.

Set it to either "Orders" or "Manual".

ActualsCalculationMode  = 'Orders'

Now you are able to activate the Sales Agreement.

My Question would be how to recalculate Sales Agreements through Apex?! Did not found a Method to invoke.....

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .