Options

NAV 2015 Change company from code

varankamvarankam Member Posts: 8
edited 2015-01-23 in NAV Three Tier
My idea is to create sales order with published codeunit but I need to create orders in different companies.
Is there way to do this?
CHANGECOMPANY does not really help since I have to validate a lot of fields.

Comments

  • Options
    Jan_VeenendaalJan_Veenendaal Member Posts: 206
    [wrong part of the forum, I think]

    The published codeunit will be available for each company, so do not make the 'change company' in NAV but in the calling application
    Jan Veenendaal
  • Options
    varankamvarankam Member Posts: 8
    [yes, wrong part. My mistake]

    Published codeunit will have different endpoints for each company. Is it possible to use one endpoint and write to different companies?
  • Options
    krikikriki Member, Moderator Posts: 9,096
    [Topic moved from 'NAV/Navision Classic Client' forum to 'NAV Three Tier' forum]
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • Options
    andrewtandrewt Member Posts: 73
    The companyname is part of the webservice URL.
    You may want to have a look at the function GETURL. This one allows you to dynamically build the required URL for a specific company by parameters:
    FromURL := GETURL(CLIENTTYPE::SOAP,YourCompany,OBJECTTYPE::Codeunit,YourWSObjectID);
    

    Hope this helps
    Andrew
  • Options
    MarijnMarijn Member Posts: 69
    Well. If you want do create a sales order in a particular NAV company from some sales order in some company, then you could create a codeunit which is exposed as a webservice. The companyname in the endpoint is the company in which the sales order will be created. You could pass a companyname and an invoice No. as parameters to this codeunit, and use a CHANGECOMPANY statement inside this codeunit to switch the sales header and lines to the company from which the values are to be read. In this case, the limitations of the CHANGECOMPANY do not play a role since you'll be just copying the values from it and assigning them to the new sales order. All business logic like the OnValidate are executed for this new sales order without using a CHANGECOMPANY statement.

    I am using this technique to push items from company A to company B. If item X in company A needs to be copied to company B, I am calling the codeunit with company B in it's endpoint and company A as one of the parameters. This can be done for any table.

    If however, you want to create a sales order from some external source, like a XML, then you should call the webservice with the applicable companyname in the endpoint and the contents of the xml as it's parameters.

    Although I never tried, I think it's perfectly possible to call a codeunit which is exposed as a webservice, and have this codeunit call another (or even the same) webservice which is a codeunit. And therefore ensuring that all business logic is correctly executed without any limitations. So the aswer is yes. You can call any codeunit in any company from any company.
  • Options
    varankamvarankam Member Posts: 8
    Thank you Marijn!

    I'll try this way.
    At the moment I fall to another trouble. If function does not throw an Error than everything in the xmlport was commited even there is error during import.

    published function
    PostPurchOrder(SourceApp : Text[100];PurchOrderInfo : XMLport "Purch. Order";VAR Response : XMLport Response)
    IF NOT PurchOrderInfo.IMPORT THEN BEGIN
    AlertMgt.AddAlert(LogEntryNo,0,GETLASTERRORTEXT);
    END;



    inside xmlport:
    OnPostXMLport()
    PurchHeader2.INIT;
    PurchHeader2.VALIDATE("Document Type", PurchHeader2."Document Type"::Order);
    PurchHeader2.INSERT(TRUE);
    ERROR(PurchHeader2."No.");


    This function creates new Purch Order each time I call it from VS.

    I have no idea how to fix it.
  • Options
    MarijnMarijn Member Posts: 69
    Hi,

    My advice is to stay away from XMLPorts when they are not used to migrate data from one system into another. I had much trouble too with XMLPorts when used for other purposes like webservices. So I'll suggest you use the XML automations or .NET classes. Examples how to use them can be found anywhere online.
Sign In or Register to comment.