Manage Salesforce Custom Objects in VSCode

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I will show you how to use Visual Studio code to manage your metadata files in Salesforce more specifically we're going to show how to create custom objects with custom fields in Salesforce and then retrieve those metadata files that represent those objects in Visual Studio code so we can manipulate them and potentially check them into Source control down the line so the first thing to do is to pick an org to work with in this case I have a Trailhead org here that we're going to use the domain name on that is this resilient shark here so I'm just going to copy that and minimize my window now let's open Visual Studio code and within Visual Studio code we're going to want to go to the terminal window so let's say I don't seem to have one we'll give Visual Studio code just a second it's uh it's a little slow loading extensions and things like that so if we go to the terminal menu and say new terminal we'll get a Powershell command prompt since I'm on a PC I imagine on a Mac this would be the terminal window just as it's called so from here what we need to do is we need to make a connection to the org we want to work with so we need to authorize that org with our login credentials so in order to do that from this terminal window I will type sfdx which is the Salesforce CLI application on the command line and we will say force or I always do that Force off web login and when we log in to the form that comes up when I run this command we will store those user credentials locally on our computer so we want to give those user credentials a better name than this long resilient shark with a unique string ID at the end of it so we'll give an alias in order to do that you hit you hit Dash a for Alias and we'll say DX demo for our alias so when we run that command we should get a browser window that pops up with the Salesforce login form so here we are so I know that my username is that plus the domain and I have already reset my password if you need to know how to do that's it's pretty easy from the the main landing page on a Trailhead playground but in this case I already have a password set up so I'll log in with that and we're in so I've authorized my Salesforce CLI and therefore the access via Visual Studio code to access this org so I'm just going to close all that because we don't care about that right now so now that we're we are connected to a Salesforce org we we should actually go back to the uh the org we're working with to create a custom object that we can retrieve the files for so I'll show you how to just open whatever org you want from the command line so to do that we use the same sfdx application on the command line and we say sfdx Force org list and this will show that the the trailhead playground that we just authorized shows up here as DX demo so the resilient shark domain name is here and the DX demo Alias is here so if we ever want to open that or we can do that using the sfdx application with sfdx.org sorry sfdx Force org open and then Dash U for user because the Alias is really just another name for that long username the Mitch route resilient shark blah blah blah so if we say DX demo that will once again open our org that we our Trailhead playground that we want to work with and there will be no login prompt this time so here we are in setup directly of that org now for demonstration Let's uh let's create a simple simple object simple custom object so in this case we'll do something silly like we're going to call this fun facts well we're going to call it fun fact Inlet the plural will be fun facts so there we go so we'll create that and just to make it seem semi-real we'll give it to new fields first field will be a text field and we'll just say that's the fact itself and we'll give that a length of 32. and X and then let's do one more just so we can see a couple of different custom Fields Let's uh let's do a number and let's call that field fund level and our fun level one goes from zero to nine so let's just give it a length of one great so now we have a custom object called fun fact with two custom Fields fun level and fact great so you guys all know how to do that I presume so what we want to do is get the metadata the XML files or the human readable files that represent these objects into our Visual Studio code so we can do things with them outside of Salesforce so let's open Visual Studio code now the sfdx application the Salesforce CLI has a whole section of commands for pulling files or pulling metadata out of Salesforce so in this case what we want is sfdx force source retrieve oh I need my little colon there retrieve and then Dash M for metadata and here is where you specify what kind of metadata you're trying to retrieve so in this case we're just going to say give me all the custom objects except that you need to know how to spell the word objects and you need to make it singular so if we say four source for cheap custom object we'll actually get an error it says this directory does not contain a valid Salesforce DX project so you can't just start pulling these files willy-nilly Salesforce wants you to have some structure in your vs code destination so that it kind of knows where to put the various objects the various files so what we want to do now is create a Salesforce DX project so in order to do that the best way is to use Ctrl shift p so you can see here it says show all commands Ctrl shift p that's also called the command palette on Mac it would be command shift p so if I hit Ctrl shift p it pops up the command palette and you can see I use it a lot so you can see the create project is the very first command at the top of the list if you don't have that at the top before list you can just type the word create and it'll filter and your create project will come to the top now if you can't quite get that hotkey combination down you can't do the Ctrl shift p you can also find that in the Run menu I'm sorry it's the view menu command palette Ctrl shift p so you can click there and get the same effect great so now we have the create project command at the top of our list let's click on that and you see this activating extensions down here in the bottom left corner that can take a minute so this create project especially the first time you run it takes a little while and it could be you think nothing's happening and it's not doing anything I've done this so many times where I'm just running that command over and over and over again it eventually it pops up but really just the first time you run it it takes quite some time so don't be spooked by that don't worry about your computer not working or something this is very common unless you have a supercomputer and I had time for a drink of water and there's no editing going on here no speeding up it's still really thinking about that command it would be a nice feature if they were to give you some sort of progress bar or hourglass or something but nothing happens for quite some time there see that was it I didn't touch anything that is how long it took on my computer to pop that up so let me show you the second time you run it so if you click off of it it goes away now if I hit Ctrl shift p again you get that menu popping up I click create project and you see the selection of what kind of project you want comes up just right away so we're going to call this call this project DX demo and you can see it's taking its time on this one too what should happen here is a file selection dialog will pop up asking me where I want to put the files but it is brutally slow the X demo come on there we go okay so now it's asking us where we want to put the files it generates when it creates a sfdx project so in my case I like to put my stuff in a SRC or Source directory at the root of my hard drive so down here I have SRC I'm going to select that and say create project now so the Salesforce CLI or sfdx creates all these files for you in a directory structure for you so let me show you a little bit well you can see the directory structure here in Visual Studio code the main things you need to worry about well the main the main folder you need to worry about is this Force app folder if you click on this you can see Force app main default that is the main area where Salesforce is going to put all of your files when you retrieve them from Salesforce so you can see by default you get all these different categories this folder will have your applications this folder your our pages your Apex classes go in classes layouts lightning-way components objects which is what we're concerned about because we're we're creating objects in this demo and tabs triggers permission sets theoretically you can pull out anything you can customize in the UI in Salesforce you can pull it out into a file in one of these sfdx projects and save it in human readable format which is great okay so the thing we care about we want to run our sfdx Command to get those custom objects into custom Fields into our Visual Studio code project so let's go find our terminal window again and let's see if we can run that retrieve command again so let's try sfdx Force Source retrieve ah I actually forgot well before we do that one thing that's nice to do when you're working with a an Oregon Visual Studio code is to set your default org so in our case we have that DX demo org we want to set as default so in order to do that we do the control shift p to pull up our Command palette and you can see the top of this list set a default org so if you don't have this pop-up immediately like I do you could just do SF DX and get a list of all of your sfdx commands in the command palette or in this case you can just type org and that should give you a list of things that work related we did not like that yeah so you can see how slow this palette can be let's let's do this over again and if I pop it up immediately it gives me a a better list of things I've used in the past but if this command prompt immediately you type in default it will filter out open default org or set of default org in our case we want to set a default org and it'll take a second has to activate some extensions okay so there we go I'm going to do a couple times to get knock some of the rust off of it but up here you can see it has a couple of commands that you could run but then underneath that you can see there's a list of some orgs that I've authorized via my Salesforce CLI so here we see DX demo is one of our options and that's that's the org we're working with so let's select that one and once we've selected that we can see that our default org is displayed here in the bottom left corner this is really cool so we know at all times which org we're actually authorized and pointed to right now this is the one we're actively working with and now that you've set a default org you can actually click this and it'll automatically open up your command palette if you want to change what that default org is we're going to keep ours the same because this is the work we're working with but this is a nice thing to switch back and forth between different orgs that you've authorized okay great so now we have a default org let's go back to our terminal window and in our terminal window we can run the sftx command for retrieving our source just like we did before but we got an error saying we didn't have a sfdx project so now if we do sfdx Force source retrieve and then with the the metadata parameter we can specify what kind of metadata what kind of thing we're pulling down from Salesforce in this case we want a custom object and the way I've written this it will pull down every custom object in the organization that we're currently pointed to in this case we only have the well actually no I might not let's not do it that way that's that's really just bad practice let's let's say custom object is the type so we want to get a custom object and we want the custom object that has the the API name of specifically of the object that we just created so let's go back to our our fun fact object and go to details and let's grab that API name and let's paste it there so this tells Salesforce let's grab the metadata files for specifically this custom object so if we hit enter there it's going out to the Salesforce org it's trying to find that custom object by that name and there it is pulled it down so you can see here we have the fun facts object name and in addition to that we have two files one each for the fields that we put onto that custom object and they have the right names and everything else so you can see the path here is Force app main default objects and then under the the name of the the object are the fields and if you go over here and look in the same the same path in our File Explorer on the left hand side we can see that in the objects we expand that and we have here's the fun facts object and below that are the two custom fields that we created now if we look at those fields we can open these files so here's the the fact feel our text field we can see that it's not unique it doesn't have any identifier attribute on it it's a type text link the 32 it's not an external ID here's the name everything label everything that represents that field in Salesforce is right here so one of the cool things we can do if we decide ah 32 characters not long enough for our facts we've got some long facts let's make 128 characters so if we change that file save it and then I go back to this field I right click on it and I can say deploy source.org so Source in this case is this file we have in Visual Studio code and we're going to send that file to the org and that will change the attribute on the field okay so right click context menu deploy source.org here we go running running running okay so we have deployed our file and the changes up to Salesforce so if you didn't already have our default org opened up and we're going to go look at it you can click on this well actually that's probably not the best way to do it you can just go up here and say command palette open default org click on that and then a new browser window already authorized to your org will will pop up we already had a browser window to that so we didn't need in this case I just wanted to show you that feature okay so in that case let's just close this browser tab since we don't need it we have our fun fact object page already open and set up so let's go to fields and relationships let's click on that well we don't click on it you can see here in the data type column of the the fact column it's now 128. so originally it was 32 characters long now it's 128. so you can see how you can start manipulating the attributes and you know what makes up the object in readable text files which is pretty cool one of the main reasons to have your metadata as human readable files is so you can put them into Source control something like git work with other people and really so that your your creative property or the things you've been building exist outside of Salesforce in in some independent Repository so they can be deployed to various orgs actually that reminds me one thing we can do we can take our fun fact and deploy it to one of the other orgs that I already have authorized so let's demonstrate how to do that that's an interesting exercise so in this case Let's uh let's list my orgs real quick so we can kind of see what I what I'm working with here so if I say sfdx Force org list I think I have four or five different orgs on here Let's uh let's use my Dev Hub so let's uh let's set the default I guess I could have just done this I click here and I can set the default to Devo so now in the bottom left corner you should see it update to Dev Hub cool so now we're pointing to a different org and I can now take my fun facts object I can deploy it to the default work when it says deploy source.org it means employee source to your default work so that takes a second to run great looks like it completed successfully and if we want to open our default org we do the command palette command open default org and we should go directly into the setup of my Dev headboard and here we are so in the object manager we can search for facts or just fact and you can see here's the custom object we just created and if you go to the fields you can see we have the updated length of the text field and both of our custom columns so everything's there so if you need to switch back and forth between deploying to various orgs if you have a development process where you want to develop in one org but then test to make sure everything deploys into a fresh org you can create scratch orgs and deploy directly to that you can deploy to your friends org you can work on it with a partner that is running their own orgs and you can collaborate later it's a very flexible and very powerful concept and that's everything thanks for watching
Info
Channel: Mitch Routt
Views: 2,958
Rating: undefined out of 5
Keywords: Salesforce, SFDX, VSCode, Visual Studio Code
Id: qmPbWdqgLEs
Channel Id: undefined
Length: 21min 37sec (1297 seconds)
Published: Wed Feb 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.