- Theory assignments - Notes - Practical - Question Papers - DISCLAIMER and motto files Lastly, updated README. Stored using LFS: - Practical/Assignment-1/AWS EC2 - User Guide.pdf - Practical/Assignment-3/Bank-App-Demo.mp4 - Practical/Assignment-3/Demo-3.mp4 - Practical/Assignment-3/Salesforce Apex - Reference Guide.pdf
6.1 KiB
Bank Application using Apex
Problem Statement: Create Bank application in SalesForce.com using Apex programming Language.
- Method 1 (Create Visual App)
- Method 2 (Execution Window) (expected method)
- Video Instructions
Method 1
- In the Developer Console, after creating a new
Apex Classwith the nameCreateBankCustomer, paste the below code:
Important
Remove all the existing stuff from the file before pasting.
public class CreateBankCustomer {
public String customerName { get; set; }
public String contactNumber { get; set; }
public String customerSegment { get; set; }
public String accountNumber { get; set; }
public String accountType { get; set; }
public List<SelectOption> segmentOptions { get; set; }
public List<SelectOption> accountTypeOptions { get; set; }
public CreateBankCustomer(ApexPages.StandardController controller) {
segmentOptions = new List<SelectOption>();
segmentOptions.add(new SelectOption('', '- None -'));
segmentOptions.add(new SelectOption('Retail Banking', 'Retail Banking'));
segmentOptions.add(new SelectOption('Corporate Banking', 'Corporate Banking'));
segmentOptions.add(new SelectOption('Private Banking', 'Private Banking'));
segmentOptions.add(new SelectOption('Investment Banking', 'Investment Banking'));
accountTypeOptions = new List<SelectOption>();
accountTypeOptions.add(new SelectOption('', '- None -'));
accountTypeOptions.add(new SelectOption('Savings', 'Savings'));
accountTypeOptions.add(new SelectOption('Current', 'Current'));
}
public PageReference createCustomer() {
System.debug('Creating new bank customer');
if (!String.isEmpty(customerName)) {
Account customer = new Account(
Name = customerName,
Phone = contactNumber,
Industry = customerSegment,
AccountNumber = accountNumber,
Type = accountType
);
insert customer;
PageReference pg = new PageReference('/' + customer.Id);
pg.setRedirect(true);
return pg;
} else {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please enter Customer Name'));
}
return null;
}
public PageReference cancelCreation() {
return new PageReference('/' + Schema.SObjectType.Account.getKeyPrefix() + '/o');
}
}
-
In top-left corner, click on
Filefollowed bySaveto save this file. -
In top-left corner, click on
File, thenNew -> Visualforce Pageand create a new Apex Page titledCreateBankCustomer. Then, paste the following code:
Important
Remove all the existing stuff from the file before pasting.
<apex:page standardController="Account" extensions="CreateBankCustomer">
<apex:form>
<apex:pageMessages />
<apex:pageBlock title="Create Bank Customer">
<apex:pageBlockSection columns="1">
<apex:inputText value="{!customerName}" label="Customer Name"/>
<apex:inputText value="{!contactNumber}" label="Contact Number"/>
<apex:inputText value="{!accountNumber}" label="Account Number"/>
<apex:selectList value="{!accountType}" size="1" label="Account Type">
<apex:selectOptions value="{!accountTypeOptions}" />
</apex:selectList>
<apex:selectList value="{!customerSegment}" size="1" label="Customer Segment">
<apex:selectOptions value="{!segmentOptions}"/>
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="Create" action="{!createCustomer}" />
<apex:commandButton value="Cancel" action="{!cancelCreation}" />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
-
In top-left corner, click on
Filefollowed bySaveto save this file. -
In top-left corner, right above the line numbers, click on
Previewbutton. A new browser tab will open. Enter the required details and click onCreateto create a new user.
Tip
Visit
Accountssection from App Launcher to view the changes.
Method 2
- In the Developer Console, after creating a new
Apex Classwith the nameUser, paste the below code:
Important
Remove all the existing stuff from the file before pasting.
public class User {
public static void createAccount(String accName, String accType) {
Account newAcc = new Account();
newAcc.Name = accName;
newAcc.Type = accType; // Use the standard Type field
try {
insert newAcc;
System.debug('Account created with id: ' + newAcc.Id);
} catch (DmlException e) {
System.debug('Error creating Account: ' + e.getMessage());
}
}
public static void deleteAccount(Id accId) {
// Check if the account ID is valid
if (accId == null) {
System.debug('Account Id cannot be null');
return;
}
try {
// Perform the delete operation
delete [SELECT Id FROM Account WHERE Id = :accId LIMIT 1];
System.debug('Account with ID: ' + accId + ' has been deleted.');
} catch (DmlException e) {
System.debug('Error deleting Account: ' + e.getMessage());
} catch (QueryException e) {
System.debug('Error finding Account: ' + e.getMessage());
}
}
}
-
In top-left corner, click on
Filefollowed bySaveto save this file. -
Press
Ctrl+Eto open execute anonymous window. -
Paste the below content and hit
Execute.
User.createAccount('Test Account', 'Savings);
User.deleteAccount('#specified_Id');
// Id can be checked in URL when clicked on any account created in Accounts Page
Tip
Visit
Accountssection from App Launcher to view the changes.
Note
For account deletion, you have to do it directly from the
Accountssection in App Launcher.