Netsuite - Set Line Item Value - netsuite

I am trying to figure out how to set a custom value item from the value of another field on the item field. I am not getting any errors, but it is not changing the value.
Here is the code:
function validatePOLineItem(type){
if(type == 'item'){
for (var i = 0; i <= nlapiGetLineItemCount('item'); i++) {
// Get the value for amount on the item line
var amount = nlapiGetCurrentLineItemValue('item', 'amount');
// Get the value for the PO Amount on the item line
var po_amount = nlapiGetCurrentLineItemValue('item', 'custcol_po_amount');
// Set PO Amount equal to Amount on the item line
nlapiSetCurrentLineItemValue('item', po_amount, amount);
}
}
}

Looks like you left off the column name you are trying to set. Also, you should just have to update the "current" lineā€¦
function validatePOLineItem(type){
if(type == 'item'){
var amount = nlapiGetCurrentLineItemValue('item', 'amount');
nlapiSetCurrentLineItemValue('item', 'custcol_po_amount', amount);
}
}

Is this a client side script or a user event script? If it is a client side script that is deployed on the validate line event, then you do not need to do a loop. Since the function will trigger every time a line is added. Also you will need to add 'return true' as the last line for the line to be added.

You cannot manipulate the amount field programmatically. You need to modify the quantity or the rate instead. If you are trying to apply a discount or something like that, then I recommend using NetSuite's Discount Items or Promo Codes mechanisms.

Question: Why do you need to have a loop if you are just dealing with the current line item?
Suggested Solution: If you want to achieve it, you can set the Rate equal to 'custcol_po_amount' but this will only work if the quantity is 1 and assuming that the value on 'custcol_po_amount' is accurate.

Related

Copy previous value in a sublist field

I'm trying to add a field to item sublist on sales order record that copies the original value of another field when ever it is changed. It pretty much preserves the previous value of another field. When ever I do a nlapiGetCurrentLineItemValue at Validate field trigger in my client script, it is giving me the new value(changed by user) not the one before changing it. Is there a way to get the value of a field before it is edited at validatefield event in client script? or by any other ways?
function validateFieldChanged(type, name, linenum) {
if (type == 'item' && name == 'custcol_commit_date') {
nlapiSetCurrentLineItemValue('item', 'custcol_last_commit_date', nlapiGetCurrentLineItemValue('item', 'custcol_commit_date'), true,true);
}
return true;
}
I've previously done something similar.
At the top of your script, declare a variable such as var existingValue;
Then in your lineInit function, get the field value and store it in existingValue.
Then when you have your validateField, you compare the be value with the existingValue.

NetSuite - using nlapiCopyRecord function

I am generating an invoice when the original invoice is overdue by a certain time period. I want to know if we can use the nlapiCopyRecord to make a copy of the original invoice but allow us to insert a new line that will replace the old line item? I haven't found any sample to show how this is done.
Thanks.
Edit 1:
var new_inv = nlapiCopyRecord('invoice', internal_id,
{
item : 66,
amount: amount,
description: 'TEST'
});
var copiedId = nlapiSubmitRecord(new_inv);
return copiedId;
Above code fails in my scheduled script. You have entered an invalid default value for this record initialize operation.
I would like to override the line item on the newly copied invoice
Yes that is possible, just like if you copy a record in the UI you can modify the copy. You also need to remember that you need to save the record object after you have copied it.
Why are you doing this? If you are trying to charge a late fee you'd probably be better off by adding an expense line to the original invoice record. If you don't have expenses turned on then you could add an other "Other Charge for Sale"
If your code is running server side then:
var invRec = nlapiLoadRecord('invoice', internal_id);
var chargeIndex = invRec.getLineItemCount('item') + 1;
// don't think you need this for the end position invRec.insertLineItem('item', chargeIndex);
invRec.setLineItemValue('item', 'item', chargeIndex, charge_item_id);
invRec.setLineItemValue('item', 'rate', chargeIndex, amount);
invRec.setLineItemValue('item', 'amount', chargeIndex, amount);
nlapiSubmitRecord(invRec);
OR if you use an expense
var invRec = nlapiLoadRecord('invoice', internal_id);
invRec.insertLineItem('expense', 1);
invRec.setLineItemValue('expense', 'account', 1, penalty_account);
invRec.setLineItemValue('expense', 'amount', 1, amount);
invRec.setLineItemValue('expense', 'memo', 1, 'TEST');
nlapiSubmitRecord(invRec);

NetSuite Update Customer subscription entries

I'm triing to update customer subscriptions list in netsuite.
var itemCount = recLead.getLineItemCount('subscriptions');
for (var i = 1; i < itemCount; i++ ) { recLead.setCurrentLineItemValue('subscriptions', 'subscribed', 'T');}
But error throws:
Notice (SuiteScript)
You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist.
If you are planning on using the "current" line item function, then you do need to select the line to use. As below:
var itemCount=recLead.getLineItemCount('subscriptions');
for(var i=1;i<=itemCount;i++){
recLead.selectLineItem('item',i);
recLead.setCurrentLineItemValue('subscriptions','subscribed','T');
recLead.commitLineItem('item');
}
Alternatively, if you do not want to do it that way, you can use setLineItemValue, instead.
var itemCount=recLead.getLineItemCount('subscriptions');
for(var i=1;i<=itemCount;i++){
recLead.setLineItemValue('subscriptions','subscribed',i,'T');
}
Both, effectively, work the same.
BTW, since you have to start at row 1, you need to make sure you use i<=itemCount. Otherwise, if there are 10 rows, you will miss the last row. When you move to 2.0, and start your count at 0, you can use i< itemCount.
Before using setCurrentLineItemValue, you need to select the line using selectLineItem then commitLineItem to save the changes.

Issue with List Apply in NetSuite

Unable to find a matching line for sublist apply with key: [doc,line] and value: [5489377,1].
I'm seeing this error when I try to update an apply list on a NetSuite transaction object. The "doc" is the internal ID of the object, and the line number seems to correspond to a line number on the object.
Why is this happening? Can't seem to find a solution.
This works for applying a credit memo to a particular invoice. invId is the internalid of the invoice record:
function applyPayment(creditMemo, payAmount, invId){
var didApply = false;
creditMemo.setFieldValue('autoapply', 'F');
if(payAmount === null) payAmount = creditMemo.getFieldValue('amountremaining');
for(var i = 1; i<=creditMemo.getLineItemCount('apply'); i++){
if(invId == creditMemo.getLineItemValue('apply', 'doc', i)){
didApply = true;
creditMemo.setLineItemValue('apply', 'apply', i, 'T');
creditMemo.setLineItemValue('apply', 'amount',i, payAmount);
}else if('T' == creditMemo.getLineItemValue('apply', 'apply', i)) creditMemo.setLineItemValue('apply', 'apply', i, 'F');
}
if(didApply) nlapiSubmitRecord(creditMemo);
}
We were getting this error with the Chargebee-Netsuite integration and the solution was to open the corresponding Accounting Period in Netsuite and rerun the sync.
Like you mentioned, the first number[5489377,1] is the Netsuite internal ID of the affected document. If you navigate to the document in Netsuite and it has a padlock this could be the reason Locked document
Open the Accounting Period for the affected document and rerun the sync. setup/accounting/manage accounting periods Manage accounting periods

Calculated Field on CRM Entity

I have a 1:N relationship between Account and Portfolios in Dynamics CRM
I.e each account has multiple Portfolios and Each Portfolio has Specific Assets.
I am trying to create a field on Account Form which calculates the sum of "ALL Assets of All related portfolios" of the account and display it on the Account form
As a workaround,I tried to create a Portfolio view grouping by Account but it doesnt SUM and rollup the Portfolio assets to Account level.
So on account Form i am trying to create a textfield which calculates the Total Account Assets to be $25,000 in this example
function setupGridRefresh() {
var targetgrid = document.getElementById("NAME OF SUBGRID");
// If already loaded
if (targetgrid.readyState == 'complete') {
targetgrid.attachEvent("onrefresh", subGridOnload);
}
else {
targetgrid.onreadystatechange = function applyRefreshEvent() {
var targetgrid = document.getElementById("NAME OF SUBGRID");
if (targetgrid.readyState == 'complete') {
targetgrid.attachEvent("onrefresh", subGridOnload);
}
}
}
subGridOnload();
}
function subGridOnload() {
//debugger;
var grid = Xrm.Page.ui.controls.get('NAME OF SUBGRID')._control;
var sum = 0.00;
if (grid.get_innerControl() == null) {
setTimeout(subGridOnload, 1000);
return;
}
else if (grid.get_innerControl()._element.innerText.search("Loading") != -1) {
setTimeout(subGridOnload, 1000);
return;
}
var ids = grid.get_innerControl().get_allRecordIds();
var cellValue;
for (i = 0; i < ids.length; i++) {
if (grid.get_innerControl().getCellValue('FIELD NAME LOWER CASE', ids[i]) != "") {
cellValue = grid.get_innerControl().getCellValue('FIELD NAME LOWER CASE', ids[i]);
cellValue = cellValue.substring(2);
cellValue = parseFloat(cellValue);
sum = sum + cellValue;
}
}
var currentSum = Xrm.Page.getAttribute('DESTINATION FIELD').getValue();
if (sum > 0 || (currentSum != sum && currentSum != null)) {
Xrm.Page.getAttribute('DESTINATION FIELD').setValue(sum);
}
}
I pieced this together from a couple of sources and currently use it one of my solutions. Let me know if you need some more help or if I've misread the question. (Btw, this solution is based on the assumption that you need the total to change when the subgrid has entries added or removed. If this is not the requirement, I would suggest the RetrieveMultiple OData call.)
Take a look at AutoSummary from Gap Consulting, well worth the cost. Or spend time to build your own. You need a field on the Account record which is updated every time you:
create a Portfolio record
update the value in a Portfolio record
delete a Portfolio record
re-parent a Partfolio record from one Account to another
The first two are easy enough to do with workflow or javascript on the onSave event on the portfolio. Third can only be done by workflow, not javascript (I think). Last one would need onLoad javascript to store current value of Account lookup so that onSave can compare and then decrement one and increment the other. All four could be done with a plugin.
Although this has been answered already, I'll put a second option on the plate for you. Take a look at FormulaManager from North 52 as well. You get a certain amount of Formulas for free so it might be an even more cost effective solution.
Update
To add to this, if the field is solely for reporting a value (and doesn't need to be saved to the database) then rather than using a physical field and plugins you could build a Web Resource that executes an Aggregated FetchXml query and simply displays the resulting value.
Again, this is something that I know Formula Manager does out of the box. Have never used Auto Summary.

Resources