Professional Documents
Culture Documents
Apex Assignment 1
Apex Assignment 1
Create a trigger
Before Delete, If Account has at least one Opportunity records associated to it then prevent user from
deleting that Account.
Error message to show: This Account has some related Opportunity record(s), you cannot delete this
Account.
//Handler
public class Assignment_1Handler {
public static void restrictForDeletion(List<Account> AccountList){
//taken opportunity who has account
List<Opportunity> oppList=[Select id,AccountId from Opportunity where AccountId In: AccountList];
for(Account acc : AccountList){
//if opportunity size is more than zero then it will show below error
if(opplist.size()>0){
acc.adderror(' Account has some related Opportunity records you cannot delete This Account');
}
}
}
/*
* @Name : Assignment_1Test.apxc
* @Purpose : Apex Assignment 1.
* @Description : Testing by Creating Account With opportunity and try to delete that Account.
* @Since : 13 09 2022
* @date : 13 09 2022
* @OtherRelateFiles : Assignment_1.apxt,Assignment_1Handler.apxc,Assignment_1Test.apxc
*/
@isTest
public class Assignment_1Test {
@testSetup static void setup() {
// Create common test accounts
List<Account> testAccount = new List<Account>();
for(Integer i=0;i<2;i++) {
testAccount.add(new Account(Name = 'TestAccount'+i));
}
insert testAccount;
}
Assignment 1 1
@isTest static void TestDeleteAccountWITHOUTOpportunity() {
// Test data setup
// Create an account without an opportunity, and try to delete it
Account accountRecord = [SELECT Id,Name FROM Account WHERE Name='TestAccount0' LIMIT 1];
}
Assignment 1 2