SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel
Showing posts with label ToDoApp. Show all posts
Showing posts with label ToDoApp. Show all posts

Tuesday, 29 December 2020

Salesforce LWC Tutorial Part 7 | Wrapping up ToDo App Project | Add Spinner | Deploy to Salesforce

 Hello Trailblazers,

Welcome to the seventh and final tutorial in LWC Tutorial Series where we're building a ToDo App Project. We're focusing on the concept of Learning By Doing in this tutorial series. In this tutorial, we're going to refine our ToDo List component by adding a spinner and some validations in LWC. We're also going to deploy our todo list component to a Salesforce Org.


So, let's continue building the above application. This tutorial is published on SFDC Stop YouTube Channel. So, if you want to learn in detail, have a look at the tutorial video down below. Otherwise, you can scroll down to have a look at the code gist with explanation.

Tutorial Video


Code Gist with Explanation

I highly recommend you to have a look at the video to understand in detail. However, let's have a quick look at the code below to understand in short:-

Apex Class (ToDoListController.cls)

As you can see above, we haven't made any change in the apex class, so the code is same as it was in the previous tutorial.

JS Snippet (todo.js)

If you see carefully, we've defined a new variable named as processing which is initially true. This variable will be used to show a spinner whenever a call to apex is performed from our lwc component. Initially, it's true because we're loading the list of tasks from Salesforce when the component is initialized. We'll now see how we need to toggle this boolean attribute in js to show/hide the spinner at multiple places and finally, we'll add a spinner component to our HTML.

If you check the addTaskToList() we have added a check first of all i.e. if our this.newTask which is our task name is blank, the method will simply return and no operation will be performed, this check will stop the user to add a blank record in our todo list. After this, we've set the processing variable to true.We've done this at the beginning, because our apex call will be asynchronous and we want our spinner to display from the moment we click on the Add button until the task is inserted in Salesforce. 

Then, we've called out insertTask method which is responsible to insert a new task in Salesforce and we've linked a finally() method as well, after the then and catch method. Inside the finally method, we again set the processing variable to false. We've done this in the finally method because we want to hide the spinner irrespective of the fact that the call to apex is successful or not, so, it doesn't matter whether we're getting a successful response or an error from apex callout, the spinner will hide as the apex call is complete.

Remember setting up the id of the new task in our insertTask method as:- this.todoTasks[this.todoTasks.length - 1].id + 1 to prevent duplicacy? I have mentioned this in the previous blog because this was important although the detailed explanation of the same is given in the video embedded above as it's also a part of enhancement.

Now, let's move on to our deleteTaskFromList(), inside this method as well, you can see that we've set the processing variable to true before we're going to perform any operation to delete the particular task, this is because we want to show the spinner before executing any code. Then, we are finding the record id of the task to delete from salesforce and finally, we're calling the deleteTask method which is used to delete the task from Salesforce by using the record id. This method is also having a finally() method attached to it after the then and catch, where we've set the processing variable to false. The reason is same, we have to hide the spinner after the apex call is complete irrespective of the fact whether the task was deleted successfully or not.

Let's have a look at getTodoTasks() method now, as you know that this is a wire method, so, it'll be called automatically when the component is initialized. Remember, we kept the initial value of processing variable as true when we initialized it? We're going to make it false now when our wire method apex call is complete. As you can see, inside our getTodoTasks() method, we're setting processing to false when the response is received from salesforce irrespective of the fact that we're getting the data or error from salesforce side when we're loading the list of tasks.

Finally, in the refreshTodoList() method, initially, we have set the processing variable to true. I have linked a finally() method to the refreshApex method as well. Inside this finally method, we've set the processing variable to false as we did in other places.

HTML Snippet (todo.html)

If you see above, inside the first lightning-layout-item tag, we've added a template with a condition which specifies, if the processing variable is true, the lightning-spinner component will be visible which is kept inside this template tag. This lightning-spinner component is responsible to display the loading spinner and is controlled by the processing variable. We've already toggled the processing variable at various places in our js code in order to show/hide the loading spinner. If you see the lightning-input tag, we've have added another property there named as autocomplete, whose value is off. This property will stop the suggestions that are coming when we're typing the name of a new task. This is also an enhancement that you can add-on to your custom lwc components.

XML Snippet (todo.js-meta.xml)

Now, it's time to deploy our lwc component to Salesforce Org. As you can see in the meta file above, we've marked isExposed property as true which will make the component visible in Salesforce Org. Apart from this, we've added a masterLabel using which you can search this component in the page builder. A friendly description of the component is added as well and under the targets tag, we've added 3 target tags:-

lightning__RecordPage:- To make our component available to a record detail page.
lightning__AppPage:- To make our component available to an app page.
lightning__HomePage:- To make our component available to a home page.

Now you can deploy this component to a Salesforce org using VSCode and you can embed your component to the homepage and test it out.

That's all for this tutorial. I hope you liked it. All the code used in this tutorial is available on GitHub and you can have a look at that by clicking here. Make sure to have a look at the video if you want to learn in detail and let me know your feedback in the comments down below.

Happy Trailblazing..!!

Wednesday, 23 December 2020

Salesforce LWC Tutorial Part 6 | ToDo App Project | Call Apex Imperatively from LWC

Hello Trailblazers,

Welcome to the sixth tutorial in LWC Tutorial Series where we're building a ToDo App Project. We're focusing on the concept of Learning By Doing in this tutorial series. In this tutorial, we're going to learn how we can send the data to the apex controller from our ToDo List component by performing an imperative call to apex from LWC. We're going to use this while inserting a new task and deleting a task as we need to insert/delete the task record from Salesforce, when the button is clicked in our component.


So, let's continue building the above application. This tutorial is published on SFDC Stop YouTube Channel. So, if you want to learn in detail, have a look at the tutorial video down below. Otherwise, you can scroll down to have a look at the code gist with explanation.

Tutorial Video


Code Gist with Explanation

I highly recommend you to have a look at the video to understand in detail. However, let's have a quick look at the code below to understand in short:-

Apex Class (ToDoListController.cls)

As you can see above, we have created two more methods insertTask and deleteTask

insertTask() is used to insert a new task with the subject which will be passed in this apex method as a parameter. This subject will be nothing but our todo task name that we've added to the todo list. OwnerId of the new task will be the id of logged in user, status is hardcoded as "Not Started" and the priority is "Normal". The new task which is inserted is returned from the method.

deleteTask() is used to delete a task from salesforce. It accepts the record id of the task to be deleted as a parameter and it deletes that task. It returns true, if the task is successfully deleted. Otherwise, returns false.

JS Snippet (todo.js)

Let's have a look at the import statements first of all, we have imported the insertTask and deleteTask method from my ToDoListController. 

Now, inside our addTaskToList() we have called insertTask method where we passed the name of our todo task which is stored in this.newTask variable, in the subject parameter. We have console logged the result of that apex call to debug what's coming in the response, this result will be the new task record which is inserted in Salesforce. Once the task is inserted, we're going to push this task in this.todoTasks list where id of the new task is calculated based on the number of tasks that are present in the list. If the number of tasks are more than 0, we'll take the id of the last task in the list, add 1 to it to get the id of the new task which is now added to the todoTasks list. Otherwise, the id of the new task is 0 if the todoTasks list is empty. This is important to make sure that we don't have tasks with duplicate ids in the list ( have a look at the video which will be provided in the next blog (part 7) to see the issue with duplicate ids in detail ). The name of the new task as we know is the value of this.newTask variable. We have added one more property to our new task here which is the recordId of the task. This property will store the salesforce record id of the newly inserted task which we can access using result.Id. As earlier, we can make the value of this.newTask as empty after the task is inserted. We've also added a catch to console log the error (if any) and we also have a finally block to assign the processing variable to false which is a small enhancement and we'll learn about it in the next tutorial.

Finally, inside our deleteTaskFromList() we have created one more variable which is named as recordIdToDelete. This variable will be used to delete the task from salesforce by using the salesforce record id. After the for loop, we've populated the recordIdToDelete variable by the recordId property of the todo task which is present at todoTaskIndex in the todoTasks list. Now, as we have the salesforce record id, we've called our deleteTask method in which we have passed the recordIdToDelete to the recordId parameter. As we know, this method will delete the task from salesforce and will return true if the task is successfully deleted, otherwise it'll return false. Therefore, we'll have a boolean value in the result variable. If the result is true, we'll remove the task present at todoTaskIndex from the todoTaks list. Otherwise, we'll simply console log the message i.e. Unable to delete task. We've also added a catch to console log the error (if any) and we also have a finally block to assign the processing variable to false which is a small enhancement and we'll learn about it in the next tutorial.

HTML Snippet (todo.html)

We've added some enhancements in the html file about which we'll learn in the next tutorial. Stay tuned..!!

That's all for this tutorial. I hope you liked it. All the code used in this tutorial is available on GitHub and you can have a look at that by clicking here. Make sure to have a look at the video if you want to learn in detail and let me know your feedback in the comments down below.

Happy Trailblazing..!!

Wednesday, 23 September 2020

Salesforce LWC Tutorial Part 5 | ToDo App Project | Wire Service linked to Function and RefreshApex

 Hello Trailblazers,

Welcome to the fifth tutorial in LWC Tutorial Series where we're building a ToDo App Project. We're focusing on the concept of Learning By Doing in this tutorial series. In this tutorial, we're going to learn how we can fetch the data from the apex controller for our ToDo List component using wire service in LWC. We're going to bind our wire service to a function in js which will receive the response. This time we'll be able to mutate the ToDoList as it won''t be directly binded with wire service. We'll also learn about RefreshApex function which will be used to refresh the browser cache.


So, let's continue building the above application. This tutorial is published on SFDC Stop YouTube Channel. So, if you want to learn in detail, have a look at the tutorial video down below. Otherwise, you can scroll down to have a look at the code gist with explanation.

Tutorial Video


Code Gist with Explanation

I highly recommend you to have a look at the video to understand in detail. However, let's have a quick look at the code below to understand in short:-

Apex Class (ToDoListController.cls)

As you can see above, I have created two more methods insertTask and deleteTask

insertTask() is used to insert a new task with the subject which will be passed in this apex method as a parameter. This subject will be nothing but our todo task name that we've added to the todo list. OwnerId of the new task will be the id of logged in user, status is hardcoded as "Not Started" and the priority is "Normal". The task inserted is returned from the method.

deleteTask() is used to delete a task from salesforce. It accepts the record id of the task to be deleted as a parameter and delete that task. It returns true if the task is succesfully deleted. Otherwise, returns false.

JS Snippet (todo.js)

Let's have a look at the wire method first which is the second last method in the above code. It's calling the same getTasks method which is imported from the apex controller like we did in the previous tutorial, the only difference here is that, instead of binding a variable, we've binded a function with the wire method. The response object in this case (which is having data and error as properties) is not directly assigned to a variable but it's coming as a parameter named response in the getTodoTasks() method which is binded with wire service. This response is stored in the todoTasksResponse variable which we've defined in the beginning of the class. After that we've stored the data and error from the response object into data and error variables for simplifying code.

If we have the data received successfully, we're re-initializing the todoTasks variable to empty array and pushing all the tasks one by one by using the foreach loop on the data array. The id of each todo task will be the (total array length + 1) (to keep it unique), the name of each todo task will be the Subject of the task received from salesforce and the recordId of each todo task will be the id of the task received from Salesforce. We're storing recordId here as well as it'll help us to delete the todoTask from salesforce in our next tutorial.

In case of an error we're displaying that in the console.

We've added a refreshTodoList() method as well which will be used to refresh the todo list from server on clicking the refresh/sync button. This method is calling the refreshApex() imported from @salesforce/apex which is used to refresh the response of wire method stored in browser cache. We're passing todoTasksResponse in this method which is nothing but the response received in our wire method.

I've also reset the addTaskToList() from Tutorial 3 as discussed in the previous tutorial that we don't be retaining/using any code of Tutorial 4 in further tutorials.

Note:- Try to add a new task to your todo list. You'll notice that this time you'll be able to mutate/update the todo list as we haven't directly binded the todoTasks array to wire service. Instead we've received the tasks list (response) in the function as a parameter and updated the todoTasks array manually.

HTML Snippet (todo.html)

We've reset the HTML code also from Tutorial 3 and have done some important changes. If you see the refresh button (lightning-button-icon) in the beginning of lightning:card, you'll notice that it's has an onclick attribute with the value as:- refreshTodoList as it'll call this method from js whenever the button is clicked to refresh the todo list. I have also updated the alternative-text and title from Sync to Refresh.

Rest all the code is same as it was in Tutorial 3. We've done the main effort in js to link wire service with a function and we've also used refreshapex which will be called when the refresh button is clicked.

Give it a try and let me know your observations in the comments down below.

Important Observation:- If you see carefully while testing, you'll notice that the refreshApex() is only refreshing the todo list, when there is a change on the server side (Try inserting a new task record directly in salesforce and click on the refresh button 2-3 times in your todo list to check how many times the wire service is refreshed). Basically, the refreshApex() only update the browser cache when there is a change on the server side otherwise it won't do anything. So, your apex calls are preserved. Thanks to LWC...!!

That's all for this tutorial. I hope you liked it. All the code used in this tutorial is available on GitHub and you can have a look at that by clicking here. Make sure to have a look at the video if you want to learn in detail and let me know your feedback in the comments down below.

Happy Trailblazing..!!

Monday, 14 September 2020

Salesforce LWC Tutorial Part 4 | ToDo App Project | Understanding Wire Service Immutability

Hello Trailblazers,

Welcome to the fourth tutorial in LWC Tutorial Series where we're building a ToDo App Project. We're focusing on the concept of Learning By Doing in this tutorial series. In this tutorial, we're going to learn how we can fetch the data from the apex controller for our ToDo List component using wire service in LWC. We'll learn about how we can bind our return value to a property by using wire and we'll try to mutate/change that property and thereby learn about the immutability property of wire service.

Note:- We'll not be using the changes that we'll do in this tutorial as a base in further LWC tutorials for ToDo App Series. This tutorial is important because, It's Equally important to understand WHAT NOT TO DO as it is to understand WHAT TO DO. We'll be learning about immutability property of wire service in this tutorial and we'll see how we can fetch the data from apex to lwc component using wire service.


So, let's continue building the above application. This tutorial is published on SFDC Stop YouTube Channel. So, if you want to learn in detail, have a look at the tutorial video down below. Otherwise, you can scroll down to have a look at the code gist with explanation.

Tutorial Video


Code Gist with Explanation

I highly recommend you to have a look at the video to understand in detail. However, let's have a quick look at the code below to understand in short:-

Apex Class (ToDoListController.cls)

As you can see above, I have created a simple method named as:- getTasks() which will return the list of tasks assigned to the logged-in user. Notice that I have added:- @AuraEnabled(cacheable=true) annotation to the method as it's required in order to make it available to the wire service. Let's have a look at the JavaScript code now.

JS Snippet (todo.js)

As you can see above, I have imported the getTasks method from the apex controller and wire service from lwc. At the end of the js, I have called getTasks method using the wire service and binded the response with todoTasks variable. It's the same variable that we initialized to an empty array in the previous tutorials (I've commented that code as you can see above the newTask variable declaration). Now, this todoTasks variable will store the response that we're getting by calling getTasks method i.e. an object with 2 keys:- data - which will contain the list of tasks and error - which will contain the error messages as you can see in the comments below the wire service.

Also, in the addTaskToList() method, I've commented the previous code of pushing the new task to todoTasks list as now we'll be pushing the new task to todoTasks.data as the list of todoTasks is present there. The id and name keys of each task are now replaced by Id and Subject as we're getting those tasks from apex now. For a new ToDo Task, the Id will have a value:- this.todoTasks.data.length + 1 instead of this.todoTasks.length + 1 as the list of todo tasks is now stored in this.todoTasks.data.

HTML Snippet (todo.html)

If you notice carefully, I have updated the todoTasks array in the for:each loop. Instead of todoTasks, I am using todoTasks.data and instead of using todoTask.idtodoTask.name, I am using todoTask.Id and todoTask.Subject

I have also wrapped this for:each loop under an if condition using <template if:true={todoTasks.data} which will check, if the data under the todoTasks object is defined or in simple words we can say that it'll check, if the tasks are fetched from the server successfully, as it may take some time to fetch the data from the server when the component is rendered. Once all the data is fetched, it'll go inside this template with if condition and render all the todo tasks using a for:each loop.

In the output you'll see that the todoTasks are fetched from salesforce successfully and you'll be able to view those. However, if you try to add a new todoTask to the list, you won't be able to do that as All the data which is fetched by using the wire service is Immutable (can't be updated). We'll see the solution to this issue in the next tutorial, where we'll bind a function with the wire service instead of a variable.

That's all for this tutorial. I hope you liked it. All the code used in this tutorial is available on GitHub and you can have a look at that by clicking here. Make sure to have a look at the video if you want to learn in detail and let me know your feedback in the comments down below.

Happy Trailblazing..!!

Thursday, 27 August 2020

Salesforce LWC Tutorial Part 3 | ToDo App Project | Removing elements from ToDo List

Hello Trailblazers,

Welcome to the third tutorial in LWC Tutorial Series where we're building a ToDo App Project. We're focusing on the concept of Learning By Doing in this tutorial series. In this tutorial, we'll be modifying the JavaScript and HTML code of our ToDo Application so that we're able to remove a ToDo Task from the list by clicking on the delete button icon. The final result will be as follows:-


So, let's continue building the above application. This tutorial is published on SFDC Stop YouTube Channel. So, if you want to learn in detail, have a look at the tutorial video down below. Otherwise, you can scroll down to have a look at the code gist with explanation.

Tutorial Video


Code Gist with Explanation

I highly recommend you to have a look at the video to understand in detail. However, let's have a quick look at the code below to understand in short:-

HTML Snippet (todo.html)

Have a look at the code above, if you carefully see the delete button-icon, you'll notice that, I've added a name attribute and an onclick attribute. The name attribute consist of the id of current todo task which is being rendered in the list, specified by todoTask.Id. Using this name attribute we can identify which task is being deleted in the js and remove that task from the todo list array. The onclick attribute is binded with a js function named as:- deleteTaskFromList

Let's have a look at the changes in JavaScript code now.

JS Snippet (todo.js)

As you can see above, I added a new method named as:- deleteTaskFromList() which will be used to remove a task from the ToDo List. Whenever we're clicking on the delete button icon, we're getting the id of the task which we have to delete by using event.target.name and storing it in idToDelete variable. We're assigning the list of todo tasks to a local variable named as todoTasks that we're going to use in this method. We declared another variable named as todoTaskIndex which will be used to store the index of todo task that we have to delete.

After that we're iterating all the todo tasks using a for loop and we're storing the index of the todo task in the todoTaskIndex variable whose id is equal to the id stored in idToDelete variable.

Finally, we're removing the todo task from the list by calling the splice() method on the todoTasks array. The splice() method takes up the index of the element to remove from the array as the first parameter and the number of elements to remove from the array (starting from that index) as the second parameter.

In the method 2, which is commented in the above code, we're using the findIndex() method to find the index of the todo task that we need to remove from the list.

We've re-written the same code specified in method 2 in method 3 also, the only difference is that we've used the arrow function => there to reduce the code to one line. Please have a look at the video for a detailed explanation of those.

You can use any of the methods out of:- method 1, method 2 or method 3. All will do the same work i.e. removing the element from the todo list.

That's all for this tutorial. I hope you liked it. All the code used in this tutorial is available on GitHub and you can have a look at that by clicking here. Make sure to have a look at the video if you want to learn in detail and let me know your feedback in the comments down below.

Happy Trailblazing..!!

Sunday, 23 August 2020

Salesforce LWC Tutorial Part 2 | ToDo App Project | Data Binding and Adding elements to ToDo List

Hello Trailblazers,

Welcome to the second tutorial in LWC Tutorial Series where we're building a ToDo App Project. We're focusing on the concept of Learning By Doing in this tutorial series. In this tutorial, we'll be adding the JavaScript to our ToDo Application and update the HTML code as well so that it renders dynamically and we'll be moving one step forward to the below result at the end as we'll be able to add elements to todo list dynamically on click of the Add button.


So, let's continue building the above application. This tutorial is published on SFDC Stop YouTube Channel. So, if you want to learn in detail, have a look at the tutorial video down below. Otherwise, you can scroll down to have a look at the code gist with explanation.

Tutorial Video


Code Gist with Explanation

I highly recommend you to have a look at the video to understand in detail. However, let's have a quick look at the code below to understand in short:-

JS Snippet (todo.js)


As you can see above, I created an array of todo tasks with name todoTasks and initialized it as an empty array. along with the newTask variable which is an empty string initially. The array is annotated with @track annotation because we need to re-render the HTML whenever a new task is added to the list. The newTask variable will store the name of new todo task that we're going to add to the list.

After that we've defined an updateNewTask() method which is used to update the value of the new task variable as we type in the new task name in the input field. event.target.value will give us the value as we're going to bind this method to the onchange attribute of input field in HTML.

Finally, we have an addTaskToList() method which is used to add the task to the todo list. This method will create a new todo task object with an id and a name and push it to the todoTasks array, in the new object, the id of the todo task will be the current array length (i.e. number of tasks in the list) + 1 and the name of the task will be the value stored in newTask variable. After adding the new task to the list, we're setting up the newTask variable as blank again.

HTML Snippet (todo.html)


As you can see in the above code, I have removed all the list items under the unordered list and kept only a single list item which is surrounded by a template tag, that template tag is basically a for:each loop which is responsible to iterate the todoTasks array specified in js and in each iteration, the current element can be accessed by the todoTask variable which is specified as the value for for:item in the template. For each list item, I am keeping the todoTask id as the key.

Note:- The first HTML element under the template tag running a for loop must have a key element and that key should be unique.

So, in our case the key is specified by todoTask.id as we're setting up the id each time we're adding an element to the todo tasks list. At the place of task name, I have specified {todoTask.name} which will replace this expression with the name of current todo task as this list is being rendered.

If you carefully see the Enter New Task input field, I have binded the newTask javascript variable as the value of input field and I have also binded the updateNewTask() method in the onchange handler of the input field.

So, whenever we're going to write something in this input field, the updateNewTask method will be called automatically and it'll update the value of newTask variable which is being set as the value of input field.

Finally, we specified our addTaskToList method in the onclick handler of the Add button. So, that whenever we're going to click this button, our todo task will be added to the list.

That's all for this tutorial. I hope you liked it. All the code used in this tutorial is available on GitHub and you can have a look at that by clicking here. Make sure to have a look at the video if you want to learn in detail and let me know your feedback in the comments down below.

Happy Trailblazing..!!