+ Start a Discussion
Azrel Rahiman 7Azrel Rahiman 7 

Trigger task before insert

Hi All, new to Salesforce Development

I'm trying to create an Apex Trigger in Task(Activity)/Event object.

1) Check a field in Account (Number Field A__c) if more than 100
2) Check a field in Account (Number Field B__c) if more than 50

Basically I have criteria above if not pass, prompt error message to users upon saving their Task/Event record.
Best Answer chosen by Azrel Rahiman 7
Azrel Rahiman 7Azrel Rahiman 7
Managed to solved this by myself with this reference: https://developer.salesforce.com/forums/?id=906F0000000kBhmIAE

Thank you.

All Answers

Kim Mikhail CabelaKim Mikhail Cabela
Create a trigger before insert and use addError to prompt the error message.

https://developer.salesforce.com/forums/?id=906F00000008zcBIAQ this might help for the add error.

Thanks!
Azrel Rahiman 7Azrel Rahiman 7
Hi,

I have this code:
 
trigger checkAccount on Task (before insert) {

    for (Task t: Trigger.new)
        {
          if (t.Account.A__c >= 100 && t.Account.B__c <= 50){
            t.addError('Please update your record in Account');
          }
        }
}

Apparently, even the criteria are met, I'm able to create the task. 

Also I did try to pull the information using formula field in Task and upate the code with this, still unable to pass:

if (t.A__c >= 100 && t.B__c <= 50)

Kim Mikhail CabelaKim Mikhail Cabela
Can you add system.debug before the if statement to check the values of those field. And also you should create a Task Handler to store the logic of your code rather than doing it in the trigger itself. 

https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices
Lokesh KumarLokesh Kumar
Did you check that your Trigger is Active or Not? 

-Lokesh
Azrel Rahiman 7Azrel Rahiman 7
Hi Lokesh,

In the Apex Trigger details, it shows: Status - Active.
Deepak Maheshwari 7Deepak Maheshwari 7

Hi,


On Task and events we can not reference cross objects in validation rules:

 

Please find the link below for the article on Idea exchange:

 

https://success.salesforce.com/ideaView?id=08730000000JFgE

 

and for workaround, please use below article:

 

https://success.salesforce.com/answers?id=90630000000CorrAAC

 

Thanks

Deepak

dpkm20@gmail.com

Azrel Rahiman 7Azrel Rahiman 7
Hi Deepak, 

I'm aware on the limitation using validation rule on this. My validation rule will only run upon saving the Task/Event record.

Seeking help to complete my Apex Code above, having hard time to make it run :(
Azrel Rahiman 7Azrel Rahiman 7
Managed to solved this by myself with this reference: https://developer.salesforce.com/forums/?id=906F0000000kBhmIAE

Thank you.
This was selected as the best answer