Create sales order with Changecompany

HollandPolandHollandPoland Member Posts: 62
Merry Christmas to all!
I've a problem about sales order creation by C/AL code using changecompany function.
An external program load 2 tables in Nav database (the tables are not system tables), in the first one it loads the sales header and second the lines. So, I created a code unit that creates a sales order in the right company (the customer sets up 8 companies), the code unit is good for the header and the lines of my current company, but when I try to write the Sales Line using SalesLine.changecompany('myCompany'); Nav 5.0 returns an error "The item XXX is not found", but if I tried to write Item.changecompany('myCompany') and Item.get('XXX') the item is exist. The code I wrote for Sales Line is:


EVALUATE(evalItemCode,myOrderLines.ItemCode);
EVALUATE(evalOldItemCode,myOrderLines.OldItemCode);
EVALUATE(evalOrderCode,myOrderLines.OrderCode);


CLEAR(Item);
Item.CHANGECOMPANY(myOrderLines.CompanyName);
IF NOT Item.GET(evalItemCode) THEN ERROR(DGText013,evalItemCode);


SalesLine.CHANGECOMPANY(myOrderLines.CompanyName);
SalesLine.LOCKTABLE;
SalesLine.INIT;

SalesLine."Document Type" := SalesLine."Document Type"::Order;
SalesLine."Document No." := evalOrderCode;
SalesLine.VALIDATE("Sell-to Customer No.",evalCustNo);
SalesLine2.CHANGECOMPANY(myOrderLines.CompanyName);
SalesLine2.RESET;
SalesLine2.SETFILTER(SalesLine2."Document Type",'%1',SalesLine2."Document Type"::Order);
SalesLine2.SETFILTER(SalesLine2."Document No.",evalOrderCode);
IF SalesLine2.FINDLAST THEN
SalesLine."Line No." := SalesLine2."Line No." + 1
ELSE
SalesLine."Line No." := 1;

SalesLine.INSERT(TRUE);
SalesLine.Type := SalesLine.Type::Item;
SalesLine.VALIDATE("No.",evalItemCode);
SalesLine.VALIDATE(Quantity,myOrderLines.Quantity);
SalesLine.VALIDATE("Unit Price",myOrderLines.UnitPrice);
IF myOrderLines.RowDiscount THEN
SalesLine.VALIDATE(SalesLine."Line Discount %",100);

SalesLine.MODIFY(TRUE);

When I try SalesLine.VALIDATE("No.",evalItemCode); the errror occurs. Any idea?
Thank for the replies.

Merry Christmas!

Comments

  • kapamaroukapamarou Member Posts: 1,152
    When you call validate it doesn't happen on the Second Company you have changed to. It happens on the company you are working on... I think you'll need to skip all validations and perform them "Manually" through code, which is a lot of work... I think there are plenty of posts on changecompany here. Search for this and you'll find some posts.
  • garakgarak Member Posts: 3,263
    kapamarou is right.
    the changecopmany doens't change the company for the validateion triggers. Theses triggers will run in your company where you run the C/AL code.
    Do you make it right, it works too!
  • vijay_gvijay_g Member Posts: 884
    hi,
    is there Any other way to use validate function with CHANGECOMPANY?
  • FDickschatFDickschat Member Posts: 380
    Set up a Jobqueue NAS per company (assuming you are on 5.0 or higher, otherwise first get the objects from a current NAV into the DB), program a codeunit or report that does all the processing. Let it first decide on its own whether it needs to do something by looking into your staging tables if something new exists. Add a status field to your records or a log table so that you know exactly what the NAS is doing / has done last if an error occurs. Setup a jobqueue to run your code.

    This way you don't have to use changecompany at all as the NAS is already in the correct company. Either your staging table has DataPerCompany=Yes and the records are imported into the correct company or set DataPerCompany to No and add a field so that the NAS can decide where this needs to be processed.

    If your customer complains about cost (he has to purchase 8 NAS) tell him that this is much less expensive then your programming hours. And also he could use the Jobqueue to automate other things during the night.

    You could also already import the data with the help of the Jobqueue to fully automate the process and at the end just send a mail to someone informing them that it worked or that there was an error.

    Don't underestimate the deleopment time though: A NAS can NOT run a dataport or a form.
    Frank Dickschat
    FD Consulting
  • Alex_ChowAlex_Chow Member Posts: 5,063
    You might want to take a look at how the intercompany functionality works in standard NAV. You'll need to replicate what it does to fit what you need.

    What the standard NAV process does is the proper way to create sales orders in different companies.
  • vijay_gvijay_g Member Posts: 884
    where is placed nav standard intercompany functionalty?
  • raj2000beraj2000be Member Posts: 36
    Hi Vijay,

    The standard IC functionality is available in the Financial management menu. I am not sure about the technical aspects of how this can be done, but in terms of building the functionality I had to do this for one of my clients and this was a 3-4 months project to get IC sales and Purchases working in 3 companies. You will need to consider a number of things like changes to the parent document, undo receipts, etc and test the system in high detail before this can move to live.

    Cheers
    Raj
Sign In or Register to comment.