• Rohan S
  • NEWBIE
  • 50 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 5
    Replies
I currently have a date field Difference__c that displays the difference between Today and Last_Call_Made__c (Date field). Here is the formula I used: TODAY() - Last_Call_Made__c. I would like the formula to exclude weekends. Can someone please help me with this?
  • September 21, 2020
  • Like
  • 0
I'm wrtting a trigger which restricts an account to be deleted if it has opportunities. Below is the Apex class & trigger.

Apex Class:
public class RestrictAccountDeleteClass
{
public static void RestrictAccountDelete(list<account>AccList)
{
list<account> OldAccList = new list<account>();
OldAccList = [SELECT Id, (SELECT Id FROM Opportunities ) FROM Account WHERE Id IN : AccList];
for(Account a : OldAccList)
{
if(a.Opportunities.size()!=0)
{
a.addError('Cannot delete account as it has associated opportunities');
}
}
}
}

Trigger:
trigger RestrictAccountDeleteTrigger on Account (before delete) {
if(trigger.isBefore==true && trigger.isDelete==true){
RestrictAccountDeleteClass.RestrictAccountDelete(trigger.old);
}
}

When I try to delete a record, it gives the following error: Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger RestrictAccountDeleteTrigger caused an unexpected exception, contact your administrator: RestrictAccountDeleteTrigger: execution of BeforeDelete caused by: System.FinalException: SObject row does not allow errors: Class.RestrictAccountDeleteClass.RestrictAccountDelete: line 11, column 1".

Can someone please let me know where I am going wrong?
On Account object I want a field (NumberOfContacts__c) to show the number of associated child Contacts. Can someone please help me with the Apex Class for this trigger which should work in all DML operations such as Create, Update, Delete and UnDelete? Thanks in advance.
I have written an Apex class and its Test class which restricts a user from deleting an Account record if he is not the Account owner, but the class currently has 75% code coverage. How do I increase it to 100%?

Apex Class:
public class AccountDeleteCheckClass {
//Declare function if account owner is not the deleting user
public static void AccountDeleteCheck(list<Account> AccountsList){
for(Account VarA : AccountsList){
if(VarA.OwnerId != UserInfo.getUserId()){
VarA.addError('Only owner can delete');
}
}
}
}

Test Class:
@isTest
class AccountDeleteCheckClassTest
{
static testmethod void Function()
{
//Insert & delete a record
Account VarA = new Account();
VarA.Name = 'ABC Corp';
insert VarA;

//Check deletion
try {
delete VarA;
}
catch(Exception e)
{
System.assert(e.getMessage().contains('Only owner can delete'));
}
}
}
I'm getting the following error while saving a record in Student object: "Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger StudentAttendanceTrigger caused an unexpected exception, contact your administrator: StudentAttendanceTrigger: execution of AfterInsert caused by: System.ListException: List index out of bounds: 12: Class.StudentAttendanceClass.StudentAttendance: line 24, column 1"

I have the following objects and fields:
Object 1: Student
Field Name: Name(Std), & Roll_Number__c

Object 2: Attendance
Field Name: Name(Std), Roll_Number__c, Student__c(Master-Detail Reln, Student object is master)

My requirement is when I create any student record, it should create 12 Attendance records (Jan to Dec) in Attendance object. I have written the following Apex class. I'm getting the above error while inserting a record in Student object. Any inputs highly appreciated!!!


public class StudentAttendanceClass
{
public static void StudentAttendance(list<Student__c> StudentsList)
{
list<Attendance__c> AttendanceList = new list<Attendance__c>();
list<string> MonthsList = new list<string>();
MonthsList.add('Jan');
MonthsList.add('Feb');
MonthsList.add('Mar');
MonthsList.add('Apr');
MonthsList.add('May');
MonthsList.add('Jun');
MonthsList.add('Jul');
MonthsList.add('Aug');
MonthsList.add('Sep');
MonthsList.add('Oct');
MonthsList.add('Nov');
MonthsList.add('Dec');
for(Student__c VarS : StudentsList)
{
for(integer p = 1; p <= 12; p = p+1)
{
Attendance__c VarA = new Attendance__c();
VarA.Name = MonthsList.get(p);
VarA.Student__c = VarS.id;
VarA.Roll_Number__c = VarS.Roll_Number__c;
AttendanceList.add(VarA);
}
}
insert AttendanceList;
}
}
I'm writing a trigger on Account, where on creation, if Employees > 1000, a Contact record should be created. Even though the 'Employees' field is present by default in an Account page, it shows the following error: Error: Compile Error: Variable does not exist: Employees at line 8 column 17. Funny thing is if I change the criteria with any other field (like BillingCountry='ABC'), it throws no error. Below is the Apex class:

public class CreateAccountContactClass {
//Declare Contact Creation function
public static void CreateAccountContact (list<Account> AccountsList)
{
    list<Contact> ContactsList = new list<Contact>();
    for(Account VarA : AccountsList)
    {
        if(VarA.Employees > 1000)
        {
            Contact VarC = New Contact();
            VarC.FirstName = 'Admin';
            VarC.LastName = 'General';
            VarC.AccountId = VarA.id;
            ContactsList.add(VarC);
        }
    }
    insert ContactsList;
}
}
I currently have a date field Difference__c that displays the difference between Today and Last_Call_Made__c (Date field). Here is the formula I used: TODAY() - Last_Call_Made__c. I would like the formula to exclude weekends. Can someone please help me with this?
  • September 21, 2020
  • Like
  • 0
I'm wrtting a trigger which restricts an account to be deleted if it has opportunities. Below is the Apex class & trigger.

Apex Class:
public class RestrictAccountDeleteClass
{
public static void RestrictAccountDelete(list<account>AccList)
{
list<account> OldAccList = new list<account>();
OldAccList = [SELECT Id, (SELECT Id FROM Opportunities ) FROM Account WHERE Id IN : AccList];
for(Account a : OldAccList)
{
if(a.Opportunities.size()!=0)
{
a.addError('Cannot delete account as it has associated opportunities');
}
}
}
}

Trigger:
trigger RestrictAccountDeleteTrigger on Account (before delete) {
if(trigger.isBefore==true && trigger.isDelete==true){
RestrictAccountDeleteClass.RestrictAccountDelete(trigger.old);
}
}

When I try to delete a record, it gives the following error: Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger RestrictAccountDeleteTrigger caused an unexpected exception, contact your administrator: RestrictAccountDeleteTrigger: execution of BeforeDelete caused by: System.FinalException: SObject row does not allow errors: Class.RestrictAccountDeleteClass.RestrictAccountDelete: line 11, column 1".

Can someone please let me know where I am going wrong?
On Account object I want a field (NumberOfContacts__c) to show the number of associated child Contacts. Can someone please help me with the Apex Class for this trigger which should work in all DML operations such as Create, Update, Delete and UnDelete? Thanks in advance.
I have written an Apex class and its Test class which restricts a user from deleting an Account record if he is not the Account owner, but the class currently has 75% code coverage. How do I increase it to 100%?

Apex Class:
public class AccountDeleteCheckClass {
//Declare function if account owner is not the deleting user
public static void AccountDeleteCheck(list<Account> AccountsList){
for(Account VarA : AccountsList){
if(VarA.OwnerId != UserInfo.getUserId()){
VarA.addError('Only owner can delete');
}
}
}
}

Test Class:
@isTest
class AccountDeleteCheckClassTest
{
static testmethod void Function()
{
//Insert & delete a record
Account VarA = new Account();
VarA.Name = 'ABC Corp';
insert VarA;

//Check deletion
try {
delete VarA;
}
catch(Exception e)
{
System.assert(e.getMessage().contains('Only owner can delete'));
}
}
}
I'm getting the following error while saving a record in Student object: "Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger StudentAttendanceTrigger caused an unexpected exception, contact your administrator: StudentAttendanceTrigger: execution of AfterInsert caused by: System.ListException: List index out of bounds: 12: Class.StudentAttendanceClass.StudentAttendance: line 24, column 1"

I have the following objects and fields:
Object 1: Student
Field Name: Name(Std), & Roll_Number__c

Object 2: Attendance
Field Name: Name(Std), Roll_Number__c, Student__c(Master-Detail Reln, Student object is master)

My requirement is when I create any student record, it should create 12 Attendance records (Jan to Dec) in Attendance object. I have written the following Apex class. I'm getting the above error while inserting a record in Student object. Any inputs highly appreciated!!!


public class StudentAttendanceClass
{
public static void StudentAttendance(list<Student__c> StudentsList)
{
list<Attendance__c> AttendanceList = new list<Attendance__c>();
list<string> MonthsList = new list<string>();
MonthsList.add('Jan');
MonthsList.add('Feb');
MonthsList.add('Mar');
MonthsList.add('Apr');
MonthsList.add('May');
MonthsList.add('Jun');
MonthsList.add('Jul');
MonthsList.add('Aug');
MonthsList.add('Sep');
MonthsList.add('Oct');
MonthsList.add('Nov');
MonthsList.add('Dec');
for(Student__c VarS : StudentsList)
{
for(integer p = 1; p <= 12; p = p+1)
{
Attendance__c VarA = new Attendance__c();
VarA.Name = MonthsList.get(p);
VarA.Student__c = VarS.id;
VarA.Roll_Number__c = VarS.Roll_Number__c;
AttendanceList.add(VarA);
}
}
insert AttendanceList;
}
}