How to get the length of a string using Apex in Salesforce?
Sample Code:
String str = 'biswajeet'; Integer i = str.length(); System.debug('size: ' + i)
Sample Code:
String str = 'biswajeet'; Integer i = str.length(); System.debug('size: ' + i)
Sometimes we need to call a method of an apex class, on click of a custom button. Let’s take an example, how we call an apex method, on click of a custom button.
Step 1:
Lets first create the Apex class.
global class MyClass { webservice static String myMethod(String studentId){ return 'Test'; } }
Note:
Step 2:
Now to setup the custom Button.
Goto –> Setup –> Object –> Buttons, links and Actions section –> Click New Button or Link
In the code for button copy the following line of codes:
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")} try{ var studentId = '{!Student__c.Id}'; var result = sforce.apex.execute("MyClass", "myMethod",{studentId : studentId}); alert(result); } catch(ex) { alert(ex); }
Visualforce Page:
<apex:page doctype="html-5.0" standardcontroller="account" tabstyle="account"> <apex:pageblock title="{!account.Name}"> <apex:tabpanel id="tabpanel"> <apex:tab label="Account Details" rerender="tabpanel" switchtype="ajax"> <apex:detail relatedlist="false"></apex:detail> </apex:tab> <apex:tab label="Contacts" rerender="tabpanel" switchtype="ajax"> <apex:relatedlist list="Contacts"></apex:relatedlist> </apex:tab> <apex:tab label="Opportunities" rerender="tabpanel" switchtype="ajax"> <apex:relatedlist list="Opportunities"></apex:relatedlist> </apex:tab> <apex:tab label="Cases" rerender="tabpanel" switchtype="ajax"> <apex:relatedlist list="Cases"></apex:relatedlist> </apex:tab> </apex:tabpanel> </apex:pageblock> </apex:page>
Note: Please pass account id in url e.g.
https://salesforce.com/apex/TabInVisualForcePage?id=0019000001DEV5z
Output:
SavePoint and Rollback will help us to maintain transaction for DML statement.
Suppose you have written multiple lines of DML statements in a try block, If any error occurs during DML Operations, the operation will be rolled back to the most recent save point and the entire transaction will not be aborted.
Savepoint sp; try{ sp = Database.setSavepoint(); Account a = new Account(); a.Name = 'Test Account'; insert a; Contact c = new Contact(Account = a.Id); c.FirstName = 'Biswajeet'; c.LastName = 'Samal'; insert c; } catch(Exception e){ Database.RollBack(sp); }
In this example, if any error occurs while inserting the Account ‘a’ or Contact ‘c’, then the entire transaction will be rolled back to SavePoint ‘sp’, as specified in the catch section by Database.Rollback method.