Apex – Strings ”; Previous Next String in Apex, as in any other programming language, is any set of characters with no character limit. Example String companyName = ”Abc International”; System.debug(”Value companyName variable”+companyName); String Methods String class in Salesforce has many methods. We will take a look at some of the most important and frequently used string methods in this chapter. contains This method will return true if the given string contains the substring mentioned. Syntax public Boolean contains(String substring) Example String myProductName1 = ”HCL”; String myProductName2 = ”NAHCL”; Boolean result = myProductName2.contains(myProductName1); System.debug(”O/p will be true as it contains the String and Output is:”+result); equals This method will return true if the given string and the string passed in the method have the same binary sequence of characters and they are not null. You can compare the SFDC record id as well using this method. This method is case-sensitive. Syntax public Boolean equals(Object string) Example String myString1 = ”MyString”; String myString2 = ”MyString”; Boolean result = myString2.equals(myString1); System.debug(”Value of Result will be true as they are same and Result is:”+result); equalsIgnoreCase This method will return true if stringtoCompare has the same sequence of characters as the given string. However, this method is not case-sensitive. Syntax public Boolean equalsIgnoreCase(String stringtoCompare) Example The following code will return true as string characters and sequence are same, ignoring the case sensitivity. String myString1 = ”MySTRING”; String myString2 = ”MyString”; Boolean result = myString2.equalsIgnoreCase(myString1); System.debug(”Value of Result will be true as they are same and Result is:”+result); remove This method removes the string provided in stringToRemove from the given string. This is useful when you want to remove some specific characters from string and are not aware of the exact index of the characters to remove. This method is case sensitive and will not work if the same character sequence occurs but case is different. Syntax public String remove(String stringToRemove) Example String myString1 = ”This Is MyString Example”; String stringToRemove = ”MyString”; String result = myString1.remove(stringToRemove); System.debug(”Value of Result will be ”This Is Example” as we have removed the MyString and Result is :”+result); removeEndIgnoreCase This method removes the string provided in stringToRemove from the given string but only if it occurs at the end. This method is not case-sensitive. Syntax public String removeEndIgnoreCase(String stringToRemove) Example String myString1 = ”This Is MyString EXAMPLE”; String stringToRemove = ”Example”; String result = myString1.removeEndIgnoreCase(stringToRemove); System.debug(”Value of Result will be ”This Is MyString” as we have removed the ”Example” and Result is :”+result); startsWith This method will return true if the given string starts with the prefix provided in the method. Syntax public Boolean startsWith(String prefix) Example String myString1 = ”This Is MyString EXAMPLE”; String prefix = ”This”; Boolean result = myString1.startsWith(prefix); System.debug(” This will return true as our String starts with string ”This” and the Result is :”+result); Print Page Previous Next Advertisements ”;
Category: apex
Apex – Overview
Apex – Overview ”; Previous Next What is Apex? Apex is a proprietary language developed by the Salesforce.com. As per the official definition, Apex is a strongly typed, object-oriented programming language that allows developers to execute the flow and transaction control statements on the Force.com platform server in conjunction with calls to the Force.com API. It has a Java-like syntax and acts like database stored procedures. It enables the developers to add business logic to most system events, including button clicks, related record updates, and Visualforce pages.Apex code can be initiated by Web service requests and from triggers on objects. Apex is included in Performance Edition, Unlimited Edition, Enterprise Edition, and Developer Edition. Features of Apex as a Language Let us now discuss the features of Apex as a Language − Integrated Apex has built in support for DML operations like INSERT, UPDATE, DELETE and also DML Exception handling. It has support for inline SOQL and SOSL query handling which returns the set of sObject records. We will study the sObject, SOQL, SOSL in detail in future chapters. Java like syntax and easy to use Apex is easy to use as it uses the syntax like Java. For example, variable declaration, loop syntax and conditional statements. Strongly Integrated With Data Apex is data focused and designed to execute multiple queries and DML statements together. It issues multiple transaction statements on Database. Strongly Typed Apex is a strongly typed language. It uses direct reference to schema objects like sObject and any invalid reference quickly fails if it is deleted or if is of wrong data type. Multitenant Environment Apex runs in a multitenant environment. Consequently, the Apex runtime engine is designed to guard closely against runaway code, preventing it from monopolizing shared resources. Any code that violates limits fails with easy-to-understand error messages. Upgrades Automatically Apex is upgraded as part of Salesforce releases. We don”t have to upgrade it manually. Easy Testing Apex provides built-in support for unit test creation and execution, including test results that indicate how much code is covered, and which parts of your code can be more efficient. When Should Developer Choose Apex? Apex should be used when we are not able to implement the complex business functionality using the pre-built and existing out of the box functionalities. Below are the cases where we need to use apex over Salesforce configuration. Apex Applications We can use Apex when we want to − Create Web services with integrating other systems. Create email services for email blast or email setup. Perform complex validation over multiple objects at the same time and also custom validation implementation. Create complex business processes that are not supported by existing workflow functionality or flows. Create custom transactional logic (logic that occurs over the entire transaction, not just with a single record or object) like using the Database methods for updating the records. Perform some logic when a record is modified or modify the related object”s record when there is some event which has caused the trigger to fire. Working Structure of Apex As shown in the diagram below (Reference: Salesforce Developer Documentation), Apex runs entirely on demand Force.com Platform Flow of Actions There are two sequence of actions when the developer saves the code and when an end user performs some action which invokes the Apex code as shown below − Developer Action When a developer writes and saves Apex code to the platform, the platform application server first compiles the code into a set of instructions that can be understood by the Apex runtime interpreter, and then saves those instructions as metadata. End User Action When an end-user triggers the execution of Apex, by clicking a button or accessing a Visualforce page, the platform application server retrieves the compiled instructions from the metadata and sends them through the runtime interpreter before returning the result. The end-user observes no differences in execution time as compared to the standard application platform request. Since Apex is the proprietary language of Salesforce.com, it does not support some features which a general programming language does. Following are a few features which Apex does not support − It cannot show the elements in User Interface. You cannot change the standard SFDC provided functionality and also it is not possible to prevent the standard functionality execution. Creating multiple threads is also not possible as we can do it in other languages. Understanding the Apex Syntax Apex code typically contains many things that we might be familiar with from other programming languages. Variable Declaration As strongly typed language, you must declare every variable with data type in Apex. As seen in the code below (screenshot below), lstAcc is declared with data type as List of Accounts. SOQL Query This will be used to fetch the data from Salesforce database. The query shown in screenshot below is fetching data from Account object. Loop Statement This loop statement is used for iterating over a list or iterating over a piece of code for a specified number of times. In the code shown in the screenshot below, iteration will be same as the number of records we have. Flow Control Statement The If statement is used for flow control in this code. Based on certain condition, it is decided whether to go for execution or to stop the execution of the particular piece of code. For example, in the code shown below, it is checking whether the list is empty or it contains records. DML Statement Performs the records insert, update, upsert, delete operation on the records in database. For example, the code given below helps in updating Accounts with new field value. Following is an example of how an Apex code snippet will look like. We are going to study all these Apex programming concepts further in this tutorial. Print Page Previous Next Advertisements ”;