+ Start a Discussion
DeekDeek 

Trigger for Events created for Accounts

Hi All,
My requirement is to restrict users to save EVENTS thats being created for ACCOUNTS based on the below criteria.

 

If the START DATE of the Event is greater than TODAY ,"YOR_Activity_Status__c" cannot be set to "Completed" and the DESCRIPTION field cannot be NULL.

 

Below is the Events trigger thats being written to achieve this:

 

I am getting this error message while saving:

 

Please help me to solve this and correct if I am wrong.

 

Error: Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger myTrigger caused an unexpected exception, contact your administrator: myTrigger: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.myTrigger: line 3, column 1

 

***************************************************************

trigger myeventvalidation on Event (after insert,after update) {
Set<Id> accIds = new Set<Id> ();
List<Account> lstAcc;
for(Event e: Trigger.new) {
if(e.StartDateTime > System.today() && e.YOR_Activity_Status__c == 'Completed' && e.Description ==NULL) {
e.addError('Not Possible');
}
}


lstAcc = [Select Id from Account where ID IN: accIds];

for(Event e: Trigger.new) {
if(Trigger.isInsert) {
for(Account a: lstAcc) {
if(e.StartDateTime > System.today() && e.YOR_Activity_Status__c == 'Completed' && e.Description ==NULL) {
e.addError('Not Possible');
}
}
}


else if(Trigger.isUpdate) {
if(e.StartDateTime > System.today() && e.YOR_Activity_Status__c == 'Completed' && e.Description ==NULL) {
for(Account a: lstAcc) {
if(a.Id == e.WhatId) {
e.addError('Not Possible');
}
}
}
}
}

update lstAcc;
}

***************************************************************

Best Answer chosen by Admin (Salesforce Developers) 
SRKSRK
try this

trigger UpdateAccountYORDateNew on Event (before insert,before update)
{
Set<id> accIds = new Set<id>();
List lstAcc;
for(Event e: Trigger.new)
{
if(String.valueOf(e.WhatId).startsWith('001') && e.APN_Activity_Status__c == 'Completed')
{
accIds.add(e.WhatId);
}
if(e.StartDateTime > System.today() && e.APN_Activity_Status__c== 'Completed' && e.Description == NULL)
{
e.addError('Not Possible');
}
}
if(accIds.size()>0)
{
lstAcc = [Select APN_Last_Activity_Date__c from Account where ID IN: accIds];
if(lstAcc.size()>0)
{
for(Event e: Trigger.new)
{
if(Trigger.isInsert)
{
if(e.APN_Activity_Status__c == 'Completed')
{
for(Account a: lstAcc)
{
if(a.Id == e.WhatId)
{
a.APN_Last_Activity_Date__c = System.Today();
}
}
}
}
else if(Trigger.isUpdate)
{
if(e.APN_Activity_Status__c == 'Completed' && Trigger.oldMap.get(e.Id).APN_Activity_Status__c != 'Completed')
{
for(Account a: lstAcc)
{
if(a.Id == e.WhatId)
{
a.APN_Last_Activity_Date__c = System.Today();
}
}
}
}
}
update lstAcc;
}
}
}

All Answers

SRKSRK
Actually closing brackets are wrong i correct them

trigger myeventvalidation on Event (after insert,after update)
{
Set<Id> accIds = new Set<Id> ();
List<Account> lstAcc;
for(Event e: Trigger.new)
{
if(StartDateTime > TODAY() && YOR_Activity_Status__c = 'Completed' && Description ==NULL)
{
e.addError('Not Possible');
}
}
lstAcc = [Select name from Account where ID IN: accIds];
for(Event e: Trigger.new)
{
if(Trigger.isInsert)
{
for(Account a: lstAcc)
{
if(StartDateTime > TODAY() && YOR_Activity_Status__c = 'Completed' && Description ==NULL)
{
e.addError('Not Possible');
}
}
}
}
else if(Trigger.isUpdate)
{
if(StartDateTime > TODAY() && YOR_Activity_Status__c = 'Completed' && Description ==NULL )
{
for(Account a: lstAcc)
{
if(a.Id == e.WhatId)
{
e.addError('Not Possible');
}
}
}
}
}
DeekDeek
Hi SRK,



Still I am getting Error: Compile Error: expecting right curly bracket,
found 'else' at line 26 column 0



trigger myeventvalidation on Event (after insert,after update)
{
Set accIds = new Set ();
List lstAcc;
for(Event e: Trigger.new)
{
if(StartDateTime > TODAY() && YOR_Activity_Status__c = 'Completed' &&
Description ==NULL)
{
e.addError('Not Possible');
}
}
lstAcc = [Select name from Account where ID IN: accIds];
for(Event e: Trigger.new)
{
if(Trigger.isInsert)
{
for(Account a: lstAcc)
{
if(StartDateTime > TODAY() && YOR_Activity_Status__c = 'Completed' &&
Description ==NULL)
{
e.addError('Not Possible');
}
}
}
}
else if(Trigger.isUpdate)
{
if(StartDateTime > TODAY() && YOR_Activity_Status__c = 'Completed' &&
Description ==NULL )
{
for(Account a: lstAcc)
{
if(a.Id == e.WhatId)
{
e.addError('Not Possible');
}
}
}
}
}




Notice



This email and any attachments are strictly confidential and subject to copyright. They may
contain privileged information. If you are not the intended recipient please delete the message
and notify the sender. You should not read, copy, use, change, alter or disclose this email or
its attachments without authorisation. The company and any related or associated companies do
not accept any liability in connection with this email and any attachments including in connection
with computer viruses, data corruption, delay, interruption, unauthorised access or unauthorised
amendment. Any views expressed in this email and any attachments do not necessarily reflect the
views of the company or the views of any of our related or associated companies.
SRKSRK

but i have a confusion bcoz you are not filling accIds any where ??


Please try this

trigger myeventvalidation on Event (after insert,after update)
{
Set<Id> accIds = new Set<Id> ();
List<Account> lstAcc;
for(Event e: Trigger.new)
{
if(StartDateTime > TODAY() && YOR_Activity_Status__c = 'Completed' && Description ==NULL)
{
e.addError('Not Possible');
}
}
lstAcc = [Select name from Account where ID IN: accIds];
for(Event e: Trigger.new)
{
if(Trigger.isInsert)
{
for(Account a: lstAcc)
{
if(StartDateTime > TODAY() && YOR_Activity_Status__c = 'Completed' && Description ==NULL)
{
e.addError('Not Possible');
}
}
}
else if(Trigger.isUpdate)
{
if(StartDateTime > TODAY() && YOR_Activity_Status__c = 'Completed' && Description ==NULL )
{
for(Account a: lstAcc)
{
if(a.Id == e.WhatId)
{
e.addError('Not Possible');
}
}
}
}
}
}
DeekDeek
Here is my updated code:



Now the error message is :



Error: Compile Error: unexpected token: ')' at line 15 column 19







trigger UpdateAccountYORDateNew on Event (before insert,before update) {

Set accIds = new Set ();

List lstAcc;

for(Event e: Trigger.new) {

if(String.valueOf(e.WhatId).startsWith('001') &&
e.APN_Activity_Status__c == 'Completed') {

accIds.add(e.WhatId);

}

if(e.StartDateTime > System.today() &&
e.APN_Activity_Status__c== 'Completed' && e.Description == NULL) {

e.addError('Not Possible');

}

}



lstAcc = [Select APN_Last_Activity_Date__c from Account where ID IN:
accIds];



for(Trigger.new) {

if(Trigger.isInsert) {

if(e.APN_Activity_Status__c == 'Completed') {

for(Account a: lstAcc) {

if(a.Id == e.WhatId) {

a.APN_Last_Activity_Date__c = System.Today();

}

}

}

}





else if(Trigger.isUpdate) {

if(e.APN_Activity_Status__c == 'Completed' &&
Trigger.oldMap.get(e.Id).APN_Activity_Status__c != 'Completed')) {

for(Account a: lstAcc) {

if(a.Id == e.WhatId) {

a.APN_Last_Activity_Date__c = System.Today();



}

}

}

}

}



update lstAcc;

}
SRKSRK
on line number 15 you write for(Trigger.new)
if you want to ittrate Trigger.new
you need to write if(Event e: Trigger.isInsert)
DeekDeek
Sorry I didn't understand:



Could you pls modify and send it to me please.



Below is my code:



Error: Compile Error: unexpected token: ')' at line 15 column 19



trigger UpdateAccountYORDateNew on Event (before insert,before update) {

Set accIds = new Set ();

List lstAcc;

for(Event e: Trigger.new) {

if(String.valueOf(e.WhatId).startsWith('001') &&
e.APN_Activity_Status__c == 'Completed') {

accIds.add(e.WhatId);

}

if(e.StartDateTime > System.today() &&
e.APN_Activity_Status__c== 'Completed' && e.Description == NULL) {

e.addError('Not Possible');

}

}



lstAcc = [Select APN_Last_Activity_Date__c from Account where ID IN:
accIds];



for(Trigger.new) {

if(Trigger.isInsert) {

if(e.APN_Activity_Status__c == 'Completed') {

for(Account a: lstAcc) {

if(a.Id == e.WhatId) {

a.APN_Last_Activity_Date__c = System.Today();

}

}

}

}





else if(Trigger.isUpdate) {

if(e.APN_Activity_Status__c == 'Completed' &&
Trigger.oldMap.get(e.Id).APN_Activity_Status__c != 'Completed')) {

for(Account a: lstAcc) {

if(a.Id == e.WhatId) {

a.APN_Last_Activity_Date__c = System.Today();



}

}

}

}

}



update lstAcc;

}


Notice



This email and any attachments are strictly confidential and subject to copyright. They may
contain privileged information. If you are not the intended recipient please delete the message
and notify the sender. You should not read, copy, use, change, alter or disclose this email or
its attachments without authorisation. The company and any related or associated companies do
not accept any liability in connection with this email and any attachments including in connection
with computer viruses, data corruption, delay, interruption, unauthorised access or unauthorised
amendment. Any views expressed in this email and any attachments do not necessarily reflect the
views of the company or the views of any of our related or associated companies.
SRKSRK

trigger UpdateAccountYORDateNew on Event (before insert,before update)
{
    Set accIds = new Set ();
    List lstAcc;
    for(Event e: Trigger.new)
    {
        if(String.valueOf(e.WhatId).startsWith('001') && e.APN_Activity_Status__c == 'Completed')
        {
            accIds.add(e.WhatId);
        }
        if(e.StartDateTime > System.today() && e.APN_Activity_Status__c== 'Completed' && e.Description == NULL)
        {
            e.addError('Not Possible');
        }
    }
    if(accIds.size()>0)
    {
    lstAcc = [Select APN_Last_Activity_Date__c from Account where ID IN: accIds];
    if(lstAcc.size()>0)
    {
        for(Event e: Trigger.new)
        {
            if(Trigger.isInsert)
            {
                if(e.APN_Activity_Status__c == 'Completed')
                {
                    for(Account a: lstAcc)
                    {
                        if(a.Id == e.WhatId)
                        {
                            a.APN_Last_Activity_Date__c = System.Today();
                        }
                    }
                }
            }
            else if(Trigger.isUpdate)
            {
                if(e.APN_Activity_Status__c == 'Completed' && Trigger.oldMap.get(e.Id).APN_Activity_Status__c != 'Completed'))
                {
                    for(Account a: lstAcc)
                    {
                        if(a.Id == e.WhatId)
                        {
                        a.APN_Last_Activity_Date__c = System.Today();
                        }
                    }
                }
            }
        }
        update lstAcc;
    }
    }
}

DeekDeek
Got a new error message:



Error: Compile Error: expecting a semi-colon, found 'accIds' at line 3
column 8


Notice



This email and any attachments are strictly confidential and subject to copyright. They may
contain privileged information. If you are not the intended recipient please delete the message
and notify the sender. You should not read, copy, use, change, alter or disclose this email or
its attachments without authorisation. The company and any related or associated companies do
not accept any liability in connection with this email and any attachments including in connection
with computer viruses, data corruption, delay, interruption, unauthorised access or unauthorised
amendment. Any views expressed in this email and any attachments do not necessarily reflect the
views of the company or the views of any of our related or associated companies.
SRKSRK
define the set type i..e
replace this line
Set accIds = new Set ();
with this
Set<id> accIds = new Set<id>();
DeekDeek

Hi,

 

After replacing I get a similar error message:

 

Error: Compile Error: unexpected token: ')' at line 37 column 125.

 

Please advise.

SRKSRK
There is a extra ")" bracket at line number 38
SRKSRK
try this

trigger UpdateAccountYORDateNew on Event (before insert,before update)
{
Set<id> accIds = new Set<id>();
List lstAcc;
for(Event e: Trigger.new)
{
if(String.valueOf(e.WhatId).startsWith('001') && e.APN_Activity_Status__c == 'Completed')
{
accIds.add(e.WhatId);
}
if(e.StartDateTime > System.today() && e.APN_Activity_Status__c== 'Completed' && e.Description == NULL)
{
e.addError('Not Possible');
}
}
if(accIds.size()>0)
{
lstAcc = [Select APN_Last_Activity_Date__c from Account where ID IN: accIds];
if(lstAcc.size()>0)
{
for(Event e: Trigger.new)
{
if(Trigger.isInsert)
{
if(e.APN_Activity_Status__c == 'Completed')
{
for(Account a: lstAcc)
{
if(a.Id == e.WhatId)
{
a.APN_Last_Activity_Date__c = System.Today();
}
}
}
}
else if(Trigger.isUpdate)
{
if(e.APN_Activity_Status__c == 'Completed' && Trigger.oldMap.get(e.Id).APN_Activity_Status__c != 'Completed')
{
for(Account a: lstAcc)
{
if(a.Id == e.WhatId)
{
a.APN_Last_Activity_Date__c = System.Today();
}
}
}
}
}
update lstAcc;
}
}
}
This was selected as the best answer
DeekDeek
Hi SRK,



Thanks a tonne for all your help. That worked!


Notice



This email and any attachments are strictly confidential and subject to copyright. They may
contain privileged information. If you are not the intended recipient please delete the message
and notify the sender. You should not read, copy, use, change, alter or disclose this email or
its attachments without authorisation. The company and any related or associated companies do
not accept any liability in connection with this email and any attachments including in connection
with computer viruses, data corruption, delay, interruption, unauthorised access or unauthorised
amendment. Any views expressed in this email and any attachments do not necessarily reflect the
views of the company or the views of any of our related or associated companies.