The code snippet public String name { get; private set; } defines a property called "name" with a public getter method and a private setter method.
Getters and setters are methods used to access and update the values of class properties, also known as instance variables or fields.
Getters are used to retrieve the value of a field, while setters are used to set or update the value of a field.
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
The code snippet public String name { get; private set; } defines a property called “name” with a public getter method and a private setter method.
The get keyword specifies that the “name” property can be accessed publicly, meaning that other classes can read the value of this property using the getter method.
The private set keyword specifies that the “name” property can only be set or modified from within the same class. Other classes cannot directly modify the value of this property, which helps to enforce data encapsulation and prevent unintended changes to the property’s value.
By using this syntax, developers can define read-only properties that can be accessed publicly but cannot be modified outside of the class. This is useful for exposing data to other classes or components while maintaining control over how that data is updated.
If you have missed the previous Session about how the Loops Works in Salesforce – You can access the complete details from using this link.
public class AccountDetails {
private String accountName;
public String getAccountName() {
return accountName;
}
public void setAccountName(String name) {
if (name != null && name.trim().length() > 0) {
accountName = name;
}
}
}
public class AccountDetails {
private String accountName;
private Integer noOfEmployees;
// private Integer noOfEmployees { private get; private set; }
public String rating { get; private set; }
public String typex { private get; set; }
public String partner { get; set; }
public String phone;
// public String phone { get; set; }
// Publically accessible
// Private Set (within class)
public String getName(){
return this.accountName;
}
public void setName(String name){
this.accountName = name;
}
public Integer getEmployees(){
return this.noOfEmployees;
}
public void setEmployees(Integer noOfEmployees){
this.noOfEmployees = noOfEmployees;
}
}
AccountDetails account = new AccountDetails();
account.setName('Test Account');
String name = account.getName();
System.debug(' name \n '+ name);
account.rating = 'Hot';
System.debug(' rating \n '+ account.rating);
Here are some commonly used operators in Salesforce:
String greeting = 'Hello ';
//greeting = greeting + ', Welcome to the Salesforce World!';
System.debug(' '+greeting);
greeting += ', Welcome to the Salesforce World!';
//greeting = greeting + ', Welcome to the Salesforce World!';
System.debug(' '+greeting);
Integer i = 5;
String message = '';
if(i > 5){
message = 'Greater than 5';
}else if(i == 5){
message = 'equal to 5';
}else{
message = 'less then 5';
}
System.debug(' '+message);
String message1 = i > 5 ? 'Greater than 5' : 'less than or equal 5';
String message2 = i > 5 ? 'Greater than 5' : i == 5 ? 'equal to 5' : 'less then 5';
System.debug(message1);
System.debug(message2);
Integer i = 5;
if( ! (i >= 5) ){ // True - False, True
System.debug(' less than 5 ');
} else {
System.debug(' greater than or equal 5');
}
Data type conversion in Salesforce refers to the process of converting a value from one data type to another.
String noOfEmployees = '8998';
String age = '24';
Integer employees = Integer.valueOf( noOfEmployees );
System.debug( employees );
String condition = 'True';
Boolean isTrue = Boolean.valueOf(condition);
if(isTrue){
System.debug('True');
}
Inner Class in Salesforce is a Class inside another Salesforce Class. Sometimes we also refer to this class as a wrapper class as This class is used to store the data from Single or Multiple Related and UnRelated Objects.
Below is the Example of an Inner Class.
public class AccountDetails {
private String accountName;
private Integer noOfEmployees;
// private Integer noOfEmployees { private get; private set; }
public String rating { get; private set; }
public String typex { private get; set; }
public String partner { get; set; }
public String phone;
// public String phone { get; set; }
// Publically accessible
// Private Set (within class)
public String getName(){
return this.accountName;
}
public void setName(String name){
this.accountName = name;
}
public Integer getEmployees(){
return this.noOfEmployees;
}
public void setEmployees(Integer noOfEmployees){
this.noOfEmployees = noOfEmployees;
}
public void testWrapper(){
AccountDetails.AccountWrapper wrapper = new AccountDetails.AccountWrapper();
wrapper.accountName = 'United Oil & Gas Inc';
System.debug(wrapper);
}
private class AccountWrapper {
private String accountName;
private Integer noOfEmployees;
}
}
AccountDetails acc = new AccountDetails(); acc.testWrapper(); AccountDetails.AccountWrapper wrapper = new AccountDetails.AccountWrapper();
Link is described in YouTube playlist of Salesforce development Zero to Hero (Hindi)
Code is not visible in many pages of development blog pages.
Please resolve this issue.
We have updated the code now.
We have recently updated the web and somehow multiple posts lost the code. Keep helping us by letting us know and we will update the code.