Friday, July 12, 2019

Create a Custom Transaction Body Field to Dynamically Calculate the Total Weight of the Items in a Sales Order Using SuiteScript

The steps below show how to add a custom Transaction Body Field to the sales order form to dynamically calculate the total weight of the items in the sales order. The calculation will only work if the weight values entered in the item record have the same units. E.g Kilograms, grams, pounds.

- Create a custom Transaction Column Field to source the weight of the item:

1. Navigate to  Customization > Lists, Records, & Fields > Transaction Line Fields > New.
2. In the Label field, enter Unit Weight.
3. In the ID field, enter _individual_weight
4. In the Type field, select Decimal Number.
5. Go to the Applies To tab:

  • Check Sale Item.

6. Under to the Display tab:

  • In the Display Type field, select Disabled.

7. Under the Sourcing & Filtering tab:

  • In the Source List field, select Item.
  • In the Source From field, select Weight.

- Create a custom Transaction Body Field to show the total weight of the items:

1. Navigate to  Customization >  Lists, Records, & Fields >  Transaction Body Fields > New.
2. In the Label field, enter Total Weight.
3. in the ID field, enter _items_total_weight.
4. Check Store Value.
5. In the Type field, select Decimal Number.
6. Under the Applies To tab:

  • Check Sale.

7. Under the Display tab:

  • In the Subtab field, select Main.
  • In the Display Type field, select Disabled.

- Write the SuiteScript:

1. Use a text editor to create the SuiteScript below:

function calculateTotalWeight(type){
 
      var lines = nlapiGetLineItemCount('item');
      var totalWeight = 0 ;
  
      for(var i=1; i< lines+1 ; i++){   
           var weight = nlapiGetLineItemValue('item', 'custcol_individual_weight', i);
           var quantity = nlapiGetLineItemValue('item', 'quantity', i);
           var weightTimesQuantity = weight * quantity;
   
           totalWeight = totalWeight + weightTimesQuantity ;
      }
      nlapiSetFieldValue('custbody_items_total_weight', totalWeight);
}

2. Name it sales_order_client_script.js and save it.
3. Navigate to Documents > Files > SuiteScripts.
4. Click Add File.
5. Select the file created before.

- Create the SuiteScript record:

1. Navigate to  Customization > Scripting > Scripts > New.
2. Select Client.
3. In the Name field, enter Sales Order Client Script.
4. In the ID field, enter _sales_order_cs.
5. Under the Scripts tab:

  • In the Script File field, select sales_order_client_script.js
  • In the Save Record Function field, enter the name of the function: calculateTotalWeight.

6. In the Save button drop down menu, click Save & Deploy to be taken to the deployment page.

- Deploy the SuiteScript:

1. In the Applies To field, select Sales Order.
2. In the ID field, enter _sales_order_cs.
3. In the Status field, select Released.
4. Under the Audience tab:

  • Check Select All next to the roles list.

5. Click Save.

3 comments:

  1. it wont let me save my sales order when i deploy the script. I changed the nomenclature but reflected the changes in the script. Would you know how to fix this?

    ReplyDelete
    Replies
    1. add return true at the bottom of the function

      Delete
  2. It does not seem like this is working for me. When I release it, it calculates the total weight and populates the field but doesnt let the record submit. anything helps.

    ReplyDelete