0% found this document useful (0 votes)
20 views8 pages

Apex Assignment 2025: Voucher Management

The document outlines the Apex Assignment 2025, detailing the data model, trigger logic, and exception handling for certification voucher requests. It includes methods for creating voucher requests, logging new requests, updating certification expiry dates, and deactivating expired vouchers, along with a test class for validation. Additionally, it describes the frontend UI features and controller methods for user and certification management.

Uploaded by

alljavacodes
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Apex Assignment 2025: Voucher Management

The document outlines the Apex Assignment 2025, detailing the data model, trigger logic, and exception handling for certification voucher requests. It includes methods for creating voucher requests, logging new requests, updating certification expiry dates, and deactivating expired vouchers, along with a test class for validation. Additionally, it describes the frontend UI features and controller methods for user and certification management.

Uploaded by

alljavacodes
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Apex Assignment 2025

Data Model

Trigger Logic with Exception Handling


Certification Voucher Request can be raised by the User/Student only if the following
conditions are satisfied:

User/Student has completed the course.


Active Voucher is available for the requested Certification.

Trigger

trigger VoucherRequest on Voucher_Request__c (before insert, after insert)


{
Id userid = [Link]();

if([Link] && [Link])


{
[Link]([Link],userid);
}
}

Handler Class Method

Prevented using SOQL and DML operations within loop to avoid Governer Limitations.
Used try-catch for exception handling.

public static void createVoucherRequest(List<Voucher_Request__c> vrlist,Id


curUserId)
{
try
{
Set<Id> certIdList = new Set<Id>();

for(Voucher_Request__c vr: vrlist)


{
[Link](vr.Certification__c);
}

List<Course__c> cList = [Select Id,Certification__c from Course__c


where CreatedById =:curUserId
AND Certification__c IN :certIdList AND
Status__c = 'Completed'];

List<Voucher__c> vList = [Select Id,Certification__c from Voucher__c


where Certification__c IN :certIdList
AND Active__c=true];

Set<Id> certCourseMap = new Set<Id>();


Map<Id,Voucher__c> certVoucherMap = new Map<Id,Voucher__c>();

for(Course__c c: cList)
{
[Link](c.Certification__c);
}
for(Voucher__c v: vList)
{
[Link](v.Certification__c,v);
}

for(Voucher_Request__c vr: vrlist)


{
Id userid = [Link];
String errorString = '';

if(![Link](vr.Certification__c))
{
errorString += '- Course Not Completed.\n';
}

if(![Link](vr.Certification__c))
{
errorString += '- Voucher Not Available.\n';
}
else
{
vr.Voucher__r = [Link](vr.Certification__c);
vr.Voucher__c = [Link](vr.Certification__c).Id;
}

if([Link]()>0)
{
[Link]('Record Cannot be inserted because of the
following errors:\n\n'+errorString);
}
}
}

catch(Exception e)
{
[Link]('Error: '+[Link]());
}

Asynchronous Apex
Future Method
To Log details of newly created voucher requests

public static void logRequestInsert(List<Voucher_Request__c> vrlist)


{
for(Voucher_Request__c vr: vrlist)
{
logMethod('New Voucher Request created. Here are the following
details:'+
'\nName: '+[Link]+'\nCertification:
'+vr.Certification__r.Name);
}

@future
public static void logMethod(String msg)
{
[Link](msg);
}

Queueable Interface
To automatically Update Certification Expiry Date after the date is past.

public class UpdateCertificationExpiryDate implements Queueable


{
public void execute(QueueableContext qc)
{
List<Certification__c> certList = [Select Id from Certification__c
where Expiry_Date__c < TODAY];
for(Certification__c cert:certList)
{
cert.Expiry_Date__c = [Link]().addDays(30);
}
update certlist;
}
}

Schedulable Job

To Automatically Deactivate Voucher after expiry date is past.

public class VoucherExpiry implements Schedulable


{
public void execute(SchedulableContext sc)
{
List<Voucher__c> voucherList = [Select Id from Voucher__c where
Expiry_Date__c < Today];
for(Voucher__c v: voucherList)
{
v.Active__c = false;
}
update voucherList;
}
}

Apex Test Class


To test the function of trigger and handler class

Code Coverage: 80%


Logging not tested.

@isTest
public class CertificationTesting
{
@isTest(SeeAllData=true)
public static void testCreateVoucherRequest()
{

Certification__c c = [Select Id from Certification__c where Name =


'Salesforce Application Architect' LIMIT 1];
Voucher_Request__c v = new Voucher_Request__c(Certification__r=c);
[Link]();
[Link] result = [Link](v,false);
[Link]();

[Link](![Link]());
}
}

Frontend UI

Features:

user details.
courses enrolled.
course completion status.
Details of the available certifications.
Search certifications by name.

Contoller

public with sharing class CertificationController


{
@AuraEnabled(cacheable=true)
public static User getUserById(Id userid)
{
try
{
User u = [Select Id,Name,Email,Phone,Country,City from User where
Id =:userid LIMIT 1];
return u;
}
catch(Exception e)
{
[Link]('Error: '+[Link]());
return null;
}
}

@AuraEnabled(cacheable=true)
public static List<Course__c> getCourseByUser(Id userId)
{
[Link](userid);
return [Select
Id,Name,Status__c,Certification__r.Name,Certification__r.Description__c,Certif
ication__r.Image_Url__c from Course__c where CreatedById =:userId];
}

@AuraEnabled(cacheable=true)
public static List<Certification__c> searchCertificationByName(String n)
{
try
{
String searchString = '%'+n+'%';
return [Select
Id,Name,Active__c,Description__c,Cost__c,Level__c,Image_Url__c from
Certification__c where Name like :searchString Limit 100];
}
catch(Exception e)
{
[Link]([Link]());
return new List<Certification__c>();
}

@AuraEnabled(cacheable=true)
public static Certification__c getCertificationByName(String n)
{
try
{
return [Select Id,Name,Active__c,Description__c,Cost__c,Level__c
from Certification__c where Name =:n limit 1];
}
catch(Exception e)
{
[Link]([Link]());
return null;
}
}

@AuraEnabled(cacheable=true)
public static Certification__c getCertificationById(Id certificationId)
{ try
{
return [Select Id,Name,Active__c,Description__c,Cost__c,Level__c
from Certification__c where Id=:certificationId];
}
catch(Exception e)
{
[Link]('Error: '+[Link]());
return null;
}
}

@AuraEnabled(cacheable=true)
public static List<Certification__c> getAllCertifications()
{
try
{
return [Select
Id,Name,Active__c,Description__c,Cost__c,Level__c,Image_Url__c from
Certification__c Limit 100];
}
catch(Exception e)
{
[Link]([Link]());
return new List<Certification__c>();
}
}
}

Common questions

Powered by AI

The key conditions for raising a Certification Voucher Request are that the User/Student must have completed the relevant course, and an active voucher must be available for the requested certification .

Exceptions in the CertificationController class methods occur during database operations, such as retrieving user data or certifications. They are handled using try-catch blocks where the exception message is logged to debug, and the method returns null or an empty list as appropriate .

Asynchronous Apex is used in two ways: a future method logs details of newly created voucher requests to allow for non-blocking execution, and the Queueable interface updates certification expiry dates by extending them, thereby ensuring resources are processed in the background when the system load permits .

The system employs a Schedulable job to automatically deactivate vouchers after their expiry date has passed. This ensures that expired vouchers cannot be used .

The system checks the completion status of the course related to the certification before approving a voucher request. It queries the database for completed courses under the current user’s ID and verifies if the requested certification matches those completed courses. Only if the course is completed will the voucher request process proceed without errors .

The logging method involves using a future method to record the details of new voucher requests. This asynchronous logging is crucial as it ensures that request details are documented without affecting the transaction response time. It supports system audits and the debugging process by providing detailed records of operations .

The system uses the Queueable interface for updating certification expiry dates and the Schedulable interface for deactivating expired vouchers. The @future annotation is used for logging voucher requests asynchronously. These features manage system tasks effectively and ensure that processes do not interfere with user operations .

The system reports errors during a Voucher Request failure for two main reasons: if the course is not completed and if a voucher is not available. These conditions are checked and detailed error messages are generated to inform the user of the specific issues, such as ‘Course Not Completed’ or ‘Voucher Not Available’ .

The CertificationController class allows dynamic retrieval of certifications by providing methods to fetch certifications by name using pattern matching and by certification ID. This enables customized searches and allows users to find certifications based on partial or complete names efficiently. The cacheable=true annotation also improves performance by caching repetitive queries, reducing load on the database .

The system prevents exceeding governor limits by avoiding SOQL and DML operations within loops and using try-catch blocks for exception handling. This ensures efficient processing without hitting system limitations .

You might also like