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.

8 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. I had the same issue. I discovered that it should be deployed as client script, but as user event script, before submit.

      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
  3. All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks sales tax calculator

    ReplyDelete
  4. Thank you for some other informative website. The place else may just I get that kind of information written in such a perfect method? I have a venture that I am simply now running on, and I’ve been at the glance out for such info. Psychotic

    ReplyDelete
  5. Thank you so much for such a well-written article. It’s full of insightful information. Your point of view is the best among many without fail.For certain, It is one of the best blogs in my opinion. bodyweight burn review

    ReplyDelete
  6. I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... Bodybuilding Meals And Recipes

    ReplyDelete