# Calculate Work Order Profit

Use case: Calculate the profit earned for a work order.

Profit = Grand Total of the work order - Total cost price of the work order

where,
Total cost price of the work order = Total cost price of the services used + Total cost price of the parts used

Total cost price of the services used = (Cost Price of the service1 X Quantity used) + (Cost Price of the service2 X Quantity used) +....+ (Cost Price of the serviceN X Quantity used)

Total cost price of the parts used = (Cost Price of the part1 X Quantity used) + (Cost Price of the part2 X Quantity used) +....+ (Cost Price of the partN X Quantity used)

Follow the steps below to implement this use case:

## Step 1: Add custom fields

 Module Field Name Field Type Description Services And Parts (Services layout) Cost Price Decimal For a service, enter the cost/purchase price of the service in this field. Services And Parts (Parts layout) Cost Price Decimal For a part, enter the cost/purchase price of the part in this field. Work Orders Total Cost Price Decimal Whenever the workflow is triggered, this field will be populated with the total cost price of the work order. Work Orders Total Profit Decimal Whenever the workflow is triggered, this field will be populated with the profit earned for the work order.

When you add a new field to the Services module, that field will automatically get added to the Unused Fields of the Parts module and vice versa. You can drag and drop the field from the Unused Fields section to the module layout.

## Step 2: Create a custom function

Create a custom function to calculate the Total Cost Price and Total Profit of a work order and to populate these values in the Total Cost Price, and Total Profit fields of a work order.
1. Navigate to Setup > Automation > Functions and click Create Function.
2. Enter the following details and click Save:
1. Function Name: CalculateWOProfit
2. Module: Work Order
3. In the Deluge Script Editor, enter the following script:
 serviceLineitems = work_order.get("Service_Line_Items");//info serviceLineitems;servicepricemap = Map();totalPurchased = 0;for each  sitem in serviceLineitems{ serviceId = sitem.get("Service").get("id"); sitemquantity = sitem.get("Quantity"); info sitemquantity; if(!servicepricemap.containKey(serviceId)) {  sinfo = zoho.fsm.getRecordById("Service_And_Parts",serviceId);  //info sinfo;  servicepurchased = sinfo.get("data").get(0).get("Cost_Price__C");  //info servicepurchased;  if(servicepurchased != null && servicepurchased != "")  {   servicepricemap.put(serviceId,servicepurchased);  }  else  {   servicepricemap.put(serviceId,sinfo.get("data").get(0).get("Unit_Price"));  } } if(servicepricemap.get(serviceId) != null) {  purchseval = servicepricemap.get(serviceId);  totalPurchased = totalPurchased + sitemquantity * purchseval;  //info "totalPurchased: " + totalPurchased; }}partlineitems = work_order.get("Part_Line_Items");partmap = Map();totalPartsPurchased = 0;for each  pitem in partlineitems{ partId = pitem.get("Part").get("id"); pitemquantity = pitem.get("Quantity"); if(!partmap.containKey(partId)) {  pinfo = zoho.fsm.getRecordById("Service_And_Parts",partId);  //info sinfo;  partpurchased = pinfo.get("data").get(0).get("Cost_Price__C");  if(partpurchased != null && partpurchased != "")  {   partmap.put(partId,partpurchased);  }  else  {   partmap.put(partId,pinfo.get("data").get(0).get("Unit_Price"));  } } if(partmap.get(partId) != null) {  partpurchaseVale = partmap.get(partId);  totalPartsPurchased = totalPartsPurchased + pitemquantity * partpurchaseVale;  //info "totalPartsPurchased: " + totalPartsPurchased; }}//info "totalPartsPurchased: " + totalPartsPurchased;//info "totalPurchased: " + totalPurchased;totalpurchasedWo = totalPartsPurchased + totalPurchased;info "totalpurchasedWo: " + totalpurchasedWo.toDecimal();wototal = work_order.get("Grand_Total");info "wototal:" + wototal;totalprofit = wototal - totalpurchasedWo;info "Profit: " + totalprofit.toDecimal();info zoho.fsm.updateRecord("Work_Orders",work_order.get("id"),{"Total_Cost_Price__C":totalpurchasedWo.toDecimal(),"Total_Profit__C":totalprofit.toDecimal()});

In the above code snippet, the API names of the custom fields used are highlighted in blue.  Replace these with the API names of the custom fields ypu have created. You can find the API names of the custom fields in the module builder (Setup > Customization > Module and Fields) of the respective modules.

## Step 3: Create a workflow rule

Create a workflow rule in Zoho FSM that will update a work order with the Total Cost Price and Total Profit of the work order.
1. Go to Setup > Automation > Workflow Rules and click Create Workflow.
2. Enter the following details, then click Next:
1. Module: Work Orders
2. Rule Name: CalculateWOProfit
3. Description: Calculate the profit earned for a work order

3. Select the rule trigger as Created or Edited and click Next.
Select the checkbox Repeat this workflow whenever a Work Order is edited.

4. Select the rule criteria as To all Work Orders and click Next.

5. Click +Action and select Function.

6. Select Existing Functions and click Next.

7. Select the custom function created in the previous step.

8. Click Save.

## Testing the use case

Provide the Cost Price for the Services and Parts.

Create or edit a work order. The Total Cost Price and Total Profit of the work order.will be populated.

### Zoho CRM Training Programs

Learn how to use the best tools for sales force automation and better customer engagement from Zoho's implementation specialists.

Redefine the way you work
with Zoho Workplace

### Zoho DataPrep Personalized Demo

If you'd like a personalized walk-through of our data preparation tool, please request a demo and we'll be happy to show you how to get the best out of Zoho DataPrep.

Create, share, and deliver

beautiful slides from anywhere.

Zoho Sign now offers specialized one-on-one training for both administrators and developers.

 Quick Links Workflow Automation Data Collection Web Forms Enterprise Begin Data Collection Interactive Forms Workplace Data Collection App Offline Forms Customer Service Accessible Forms Digital Forms Marketing Forms for Small Business HTML Forms Education Forms for Enterprise Contact Forms E-commerce Lead Generation Forms Healthcare Wordpress Forms Customer onboarding No Code Forms Construction Free Forms Travel Prefill Forms Non-Profit Intake Forms Legal Form Designer HR Card Forms Food Assign Forms Photography Translate Forms Electronic Forms Notification Emails for Forms Holiday Forms Form to PDF HIPAA Forms

Zoho Pagesense Resources

New to Zoho Survey?

Manage your brands on social media

Zoho Desk Resources

• Desk Community Learning Series

• Digest

• Functions

• Meetups

• Kbase

• Resources

• Glossary

• Desk Marketplace

• MVP Corner

• Word of the Day

# Zoho Sheet Resources

New to Zoho Forms?

# Zoho Forms Resources

communication with Zoho Mail

Mail on the move with
Zoho Mail mobile application

Stay on top of your schedule
at all times

Anytime, anywhere

# Zoho Sign Resources

Sign, Paperless!

Sign and send business documents on the go!

Zoho SalesIQ Resources

# Zoho DataPrep Resources

Zoho DataPrep Demo

Get a personalized demo or POC

Design. Discuss. Deliver.

Create visually engaging stories with Zoho Show.

Wherever you are is as good as

# Resources

### Videos

Watch comprehensive videos on features and other important topics that will help you master Zoho CRM.

### eBooks

Download free eBooks and access a range of topics to get deeper insight on successfully using Zoho CRM.

### Webinars

Sign up for our webinars and learn the Zoho CRM basics, from customization to sales force automation and more.

### CRM Tips

Make the most of Zoho CRM with these useful tips.