Hello!! I am trying to inizialize a Purchase Order...
# general
m
Hello!! I am trying to inizialize a Purchase Order created from a Sales Order with Special Order. I prepared a Client Script with pageInit function. The body fields are initialized without any problem. When I initialize the items from "Item" sublist, something strange happens. In particular, I am trying to set the rate field by taking values from a custom field that was inherited from Sales Order. The value is correctly initialized (i see it from the log) but it is cleared immediately afterwards. Obviously I tried the triggers like fireSlavingSync with no effects. I also tried to use lineInit or sublistChanged entry points without any luck. Have you experienced something like that? Does anybody know what is clearing the values? Thanks in advance.
n
What does your code look like?
m
This is my current pageInit function. In this version I see the value only on the last line, but the value is cleared if I select the line. Thanks Nick!
Copy code
function pageInit(scriptContext) {
         var current_record = scriptContext.currentRecord;                
         var rfq_vendor_id = current_record.getSublistValue({
             sublistId: "item",
             fieldId: "custcol_pce_vendor",
             line: 0
         });
         console.log("First Line Vendor ID: "+ rfq_vendor_id);
         if(rfq_vendor_id!=null){
             current_record.setValue({
                 fieldId: 'entity',
                 value: rfq_vendor_id,
                 fireSlavingSync: true
             });
             var num_lines = current_record.getLineCount({sublistId: 'item'});
             console.log("NUM Lines: "+ num_lines);
             for (i = num_lines-1; i >=0 ; i--) {
                 current_record.selectLine({
                     sublistId: "item",
                     line: i
                 });
                 current_rfq_vendor_id=current_record.getCurrentSublistValue({
                     sublistId: "item",
                     fieldId: "custcol_pce_vendor"
                 });
                 if(current_rfq_vendor_id!=rfq_vendor_id){
                     current_record.removeLine({
                         sublistId: "item",
                         line: i
                     });
                     continue;
                 }
                 console.log("RATE BEFORE: "+current_record.getCurrentSublistValue({
                     sublistId: "item",
                     fieldId: "rate"
                 }));
                 custcol_pce_cost=current_record.getCurrentSublistValue({
                     sublistId: "item",
                     fieldId: "custcol_pce_cost"
                 });
                 console.log("PCE Cost for Line "+i+": "+custcol_pce_cost);
                 current_record.setCurrentSublistValue({
                     sublistId: "item",
                     fieldId: "rate",
                     value: custcol_pce_cost,
                     fireSlavingSync: true
                 });
 
                 current_record.commitLine({
                     sublistId: "item"                    
                 });
 
                 current_record.selectLine({
                     sublistId: "item",
                     line: i
                 });
                 console.log("RATE AFTER: "+current_record.getCurrentSublistValue({
                     sublistId: "item",
                     fieldId: "rate"
                 }));
             }        
         }
     }
n
Do you have something in your field changed? Is item already on the line?
m
1. I don't change anything on any field other than rate 2. The item is already on the line, inherited from Sales Order. I think that there may be some sourcing mechanism on rate field (for example taking data from item record) that override my field setting.
n
The rate is sourced from the item and/or price level. Can you try setting the price level field to custom? use field Id price and value -1, and then set the rate
m
Is the price field available on any transaction? May it lack on Purchase Orders? I tried to add the field setting as you requested. No results. 😞
Wait. It was a cache problem. Now it is working. Thanks. I'll do further tests but it seems that it keeps the rate.
Ok. Further tests showed that the key point was not the price field but a so called "origrate" field. To have a persistent behavior both rate and origrate have to be setted. I don't know if this is documented anywhere: I found it using a trial and error approach but it seems to work.
Now I have a different problem. In some cases I have to remove a line (as you can see from my code). This works very well but when I try to submit the PO it says that a mandatory field is not filled on the line I've just removed... And there is no way to submit the form.
n
You are removing in wrong sequencing.
You should run the loop in reverse
m
I am already doing the look in reverse
😉
Copy code
var num_lines = current_record.getLineCount({sublistId: 'item'});
             console.log("NUM Lines: "+ num_lines);
             for (i = num_lines-1; i >=0 ; i--) {
                 current_record.selectLine({
                     sublistId: "item",
                     line: i
                 });
                 current_rfq_vendor_id=current_record.getCurrentSublistValue({
                     sublistId: "item",
                     fieldId: "custcol_pce_vendor"
                 });
                 if(current_rfq_vendor_id!=rfq_vendor_id){
                     current_record.removeLine({
                         sublistId: "item",
                         line: i
                     });
                     continue;
                 }
             }
The problem is not on the code. The code successfully removes the line and ends with no problems. After the execution of the code, it seems that something still is there and cause the validation to fail.
At the end I chose to move the code for line deletion from the CS to a User Event beforeLoad. In this way it works like a charm.