Work Orders are the central part of any field service solution. Salesforce has work orders available in Enterprise, Performance, Unlimited and Professional editions. In this article, we are going to see how to create an Order from Work Order. We can create an Order with the selected line items from the list of Work Order Line items. We will be creating a lightning component in Salesforce which displays the list of work order line items with a checkbox to select the line items with which the Order is to be created. We will be creating 2 lightning components and a class to achieve this functionality.
Our requirement is to create a button called ‘Create Order’ on Work Order. It should display the list of work order line items. Once the Order is created for the work order, the ‘Create Order’ button should be disabled. Only the system administrator should be able to see the ‘Create Order’ button.
Steps to create the component :
- Create a lightning component to display Create Order button
- Create a lightning component to display the work Order line items
- Create a class to get the work order line items
- Add the component to Work Order Page
Create a lightning component to display Create Order button
First, we will be creating a component to display the ‘Create Order’ button on the Work Order Page.
Steps to create the Create Order button:
- Open developer console navigate to file -> New -> Lightning Component
- I’m creating the Lightning component with the name ‘CreateOrderview’.
- Below is the code to display the button. If the Order is created for Work Order the button will be disabled and the text ‘Order is created for this Work Order’ will be shown. Otherwise button will be enabled and ‘Click to create Order for Work Order : Work Order Name’ will be displayed.
- In the above screenshot, we are calling ‘openModal’ java script function. This function is written in the controller. It will call the component ‘CreateOrdertable’. ‘OpenModal’ functions is shown in the below screenshot
Create a lightning component to display the work Order line items
We have seen the steps to create the component ‘Create Order’ button. Now, we will see how to display the list of work order line items when the button is clicked.
- Create a component ‘CreateOrdertable’. We are using a lightning datatable to display the list of work order line items. Below is the code to display the lightning data table.
- We are writing an ‘init’ function to get the fields to be displayed. Here, we are displaying ‘LineItem No#’, ‘Unit Price’, ‘Quantity’ and ‘Total Price’. Below is the code
- As above function will be executed below screen will be displayed. It will have 2 buttons with ‘Create Order’ and ‘Cancel’.
- When this button is clicked the function ‘handlesubmit’ will be called, which calls the method ‘CreateOrder’ of the class ‘CreateOrder’ . It’s shown in the below image
- Below is the code in ‘handleSubmit’ function, which calls the class and gets the selected rows from the Work Order line items list
- Now, let’s see what does the ‘CreateOrder’ class does in detail.
Create a class to get the work order line items
The class contains a method named ‘CreateOrder’. It will the take the current work order details and create the ‘Order’ with those details. Below is the code for the same
In the above method, we are inserting Order and Order line items. We are also updating a checkbox ‘Order_Created__c’= true which represents that the Order is created for Work Order.
Add the component to Work Order Page
- Navigate to Work Orders and open any work Order
- Click on the setting Icon and select ‘Edit Page’ as shown below
- The below page will be shown where you can find the component you have created. Search for the ‘custom’ in the left hand side component field. Below screen will be shown, where you will find the list of components you have created.
- Drag and drop the Component in the position you wanted to see. I have placed it below the details section of the work order
- As my requirement is to display the button only to system Administrator, I have added the filter as shown below
- Now, click on Activate. Below screen will be shown. In the below screenshot you can see, ‘Remove as Org Default’. As, I have already added it to the Org default it’s showing ‘Remove as Org Default’. It will show ‘Assign as Org Default’. Select that so that, it will be assigned as Organization wide Default.
- Now, it will be added to the Work Order Page layout. It can be shown in the below screen
- As I have logged in with System Admin, I’m able to see the ‘Create Order’ button. Now, I will login with other user, the button will not be shown in the work order page. It is shown in the below screen.
- In the above screenshot the user name is different and the user is not a system admin so, the ‘Create Order’ button is not visible.
- When I click on the ‘Create Order’ button, a pop up will be displayed with the list of Work Order Items with a checkbox to select the Line items to create an order. It’s shown in the below sceenshot
- I have selected one line item and clicked the ‘Create Order’ button. The Order is created and the success message is shown in the below screenshot
- Now, navigate to the related list to check the order. Once the Order is created the ‘Create Order button will be disabled. It can be seen in the below screenshot
- Now, navigate to Order and we can see that the Order is created with one line item as shown below.
In this way, we can automate the process of creating an Order from Work Order with selective line items from Work Order Line Items.
How we can help
Our team can help you customize and integrated Salesforce as per your business process. Read about our Salesforce Integration Services. Reach out to us for a free assessment of your business needs.