Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Unable to set Unit_Price on sales line via webservice

(0) ShareShare
ReportReport
Posted on by

Hi,

I have been struggling with an issue, and am also somewhat an amateur developer as well.

I have a php function, which creates a new sales line to an existing blanket sales order. I do this via both update and update_multiple methods. 

I can create new sales lines, and my code works all the time. If i add to my code ability to set the "Unit_Price", i get an error: 

The Sales Line already exists. Identification fields and values: Document Type='Blanket Order',Document No.='3060',Line No.='10000'


If i write some code that first creates the blanket order, and then updates it with sales lines, i can make it work. The code for update is more or less the same. 

The sales line as far as i can see, is always new, even if the blanket order itself is new or old. Or is this where i am wrong?


The only solution i see this far, is to re-read the order after i have put everything but the unit_price, and then set the price only. I am struggling on how to identify the line to edit, since there is no identical key, except the "key". What i am trying to say, how do i know which key i want to make an update to, after i have created a new line. NAV does not say "you just created xxx with key xxx. I have to read the order and somewhat guess which line i just created.

Anywho, here is my code:

Code:

        $service = new NTLMSoapClient($pageURL);

        $blanketorder = new stdClass();
        $sq = new stdClass();
        $update = new stdClass();


// Read order to update
        foreach ($varelinjeny as $order1) {
            $blanketorder = array('No' => $order1[onr]);           
            $result = $service->Read($blanketorder);
            $blanket = $result->OrderCard;
            $key = $result->OrderCard->Key;

//Set the key to use for update-message later on $sq->Key = $key; $ii = 0; $i = -1; foreach ($varelinjeny as $ordre1) { $ii++; unset ($salesLine); $salesLine = new stdClass();
// $salesLine->Key = $ii;; //this only works if i create the order first
$vare = explode("|",$ordre1[varenr]); $i++; $salesLine->Type = 'Item'; $salesLine->No = $vare[0];
$salesLine->Unit_Price = "123"; //comment out this, and code is ok if ($ordre1[otf] == 'on') { $salesLine->One_time_fee = 'true'; } else { $salesLine->One_time_fee = ''; } $salesLine->Antall_opprinnelig_bestil = $ordre1[antall];
//some custom fields if (!empty($ordre1[nocat])) { $salesLine->Username = $ordre1[nocat]; } if (!empty($ordre1[linespeed])) { $salesLine->Line_Speed = $ordre1[linespeed]; } if (!empty($ordre1[binding])) { $salesLine->Subscription_Time = $ordre1[binding]; } if (!empty($ordre1[smbnr])) { $salesLine->Communication_Number = $ordre1[smbnr]; } // Add SalesLineList to SalesQuote $sq->SalesLines[$i] = $salesLine; } } $update->OrderCard = $sq; $result = $service->Update($update);


Thanks

--
Kim

*This post is locked for comments

  • Verified answer
    RE: Unable to set Unit_Price on sales line via webservice

    Hi,

    Solved it, sort of. Feeling that i have to do a lot of roundtrips with this stuff, but it works...

    What i ended up doing is this, and i think it is what Ishwar meant:

    1. I read the order to get the keys to use for update
    2. I run a update, creating a new salesline. Here i set all that NAV accepts in this update, omitting Unit_Price.
    3. I read the result of this, and loops trough all keys to save them in an array. At the same loop i also strip the keys so that i get the last part of it, split at "9;" .
    4. I the find the highest number of this key-part, and that has to be the last record.
    5. Then i use the key belonging to the latest record, to do a new update, setting the price.

    My code for this probably bloated and considered "bad practice" or something like that, but as i said, i am an amateur :P

    Regards, and thanks!

    --
    Kim


    
    
    $service = new NTLMSoapClient($pageURL);
    
    //Set some vars
            $blanketorder = new stdClass();
            $sq = new stdClass();
            $update = new stdClass();
    
    // Read order to update
            foreach ($varelinjeny as $order1) {
                $blanketorder = array('No' => $order1[onr]);
                $result = $service->Read($blanketorder);
    
    
                $blanket = $result->OrderCard;
                $key = $result->OrderCard->Key;
                $sq->Key = $key;
                //$sq2->Key = $key;
    
    
                $ii = 0;
                $i = -1;
    
                foreach ($varelinjeny as $ordre1) {
                    $ii++;
                    unset ($salesLine);
                    $salesLine = new stdClass();
                    $salesLine->Type = 'Item';
    
                    $vare = explode("|",$ordre1[varenr]);
                    $salesLine->No = $vare[0];
                    //$salesLine->Unit_Price = '123';
    
                    if ($ordre1[otf] == 'on') {
                        $salesLine->One_time_fee = 'true';
                    } else {
                        $salesLine->One_time_fee = '';
                    }
    
                    $salesLine->Antall_opprinnelig_bestil = $ordre1[antall];
    
                    if (!empty($ordre1[nocat])) {
                        $salesLine->Username = $ordre1[nocat];
                    }
    
                    if (!empty($ordre1[linespeed])) {
                        $salesLine->Line_Speed = $ordre1[linespeed];
                    }
    
                    if (!empty($ordre1[binding])) {
                        $salesLine->Subscription_Time = $ordre1[binding];//date('Y-m-d', strtotime('+1 years'));
                    }
    
                    if (!empty($ordre1[smbnr])) {
                        $salesLine->Communication_Number = $ordre1[smbnr];
                    }
    
                    $sq->SalesLines->Blanket_Sales_Order_Line = $salesLine;
    
                    $update->OrderCard = $sq;
                    $result = $service->Update($update);
                    
    
                    foreach ($result->OrderCard->SalesLines->Blanket_Sales_Order_Line as $finditem){
                            $linekey[] = $finditem->Key;
                            //strip keys to the last part only split at "9;"
                            $keytoedit[] = substr($finditem->Key, strpos($finditem->Key, "9;")+ 2);
                    }
                    //find the highest value of key-part
                    $value = max($keytoedit);
    
                    //find the full key for this part
                    foreach ($linekey as $lkey){
                    if (strpos($lkey,"9;".$value ) !== false) {
                        
                        $ekey = $lkey;
                        echo "Key to edit: ".$ekey;
    
                    }
                    }
                    
                    echo "Highest key-part:".$value;
    
                    $key2 = $result->OrderCard->Key;
                    $sq->Key = $key2;
    
                    //assigning here the "difficult" stuff
                    $salesLine2->Key = $ekey;
                    $salesLine2->Unit_Price = "123";
    
                    $sq->SalesLines->Blanket_Sales_Order_Line = $salesLine2;
    
                    $update->OrderCard = $sq;
                    $result = $service->Update($update);
    
    
                }
            }
    
    
              
                echo "We got no errors in placing the order! To make sure; doublecheck by clicking here: <a href='http://**** . "'>$mamutid</a>";
            }
    
        catch
            (Exception $e) {
                echo 'Crap, we hit an error:<br><br><br> <h3>' . $e->getMessage() . '</h3>';
            }
    
    
    
  • RE: Unable to set Unit_Price on sales line via webservice

    Hi, thanks!

    When you say filter, how can i find the last record? Does this have a identifier as "being the last"? I have not seen any pattern in the keys to use them to find something. 

    2:

    When you say "update line once inserted", what is the NAV logic in this? As far as i know I am doing it all at once. The update  message sends a array with sales line content, all of it is inserted in one operation and then validated. Or do NAV actually do this in some other sequence?

    I have been thinking about creating a line with only "item" as type. Then, filtering and identifying this line and fill it with the rest.

    However this limits me to only one line at a time. As now, my code allows me to send two or more sales lines in my initial array, and then PHP loops trough it. (Thats why i have the foreach loops in place)

    Best regards

  • Verified answer
    IshwarSharma Profile Picture
    IshwarSharma 729 on at
    RE: Unable to set Unit_Price on sales line via webservice

    You don't need to guess. The sales line you are creating will be having the same value for Document Type and Document No. as the Sales Header (or Order). Apply a filter on these 2 fields find the last record and update unit cost in it.

    OR you may do what you're doing, i.e. update the line once it is inserted. This way you still have the reference of the record in memory. Insert and then modify the line.

    Hope it helps.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans