You need to sign in to do that
Don't have an account?
Oliver Jones Rajkumar
Help on Triggers
Hi All,
I'm a begginer in Apex Triggers. The following is a trigger I used but its not working. My Trigger should not allow ANY USERS to delete the record if the record creater (User) becomes INACTIVE. This trigger never allowed me to delete a record eventhough the User is active. Help n Thanks.
trigger preventdelete on Loan__c (before delete){
for(Loan__c loan:Trigger.old){
if(loan.CreatedBy.IsActive == FALSE ){
loan.addError('You cant delete this record since it is created by an inactive user');
}
}
}
I'm a begginer in Apex Triggers. The following is a trigger I used but its not working. My Trigger should not allow ANY USERS to delete the record if the record creater (User) becomes INACTIVE. This trigger never allowed me to delete a record eventhough the User is active. Help n Thanks.
trigger preventdelete on Loan__c (before delete){
for(Loan__c loan:Trigger.old){
if(loan.CreatedBy.IsActive == FALSE ){
loan.addError('You cant delete this record since it is created by an inactive user');
}
}
}
Please try this code. I have made the error to display on your custom object.
Let me know if there is still any issues.
Please close the thread if it worked
Thankyou
All Answers
Try this code ,
Let me know if you have any issues.
Mark it as best answer if it works.
Regards
I havent tried it, but the followign seems to be a visible issue:
Instead of
try:
Let me know if it doesnt work. I will try it on my org then.
Thanks,
Mann.
Please mark as answer if you feel it is :)
Thanks. Your Code works accurately as per the scenario. Only problem is the error message thrown as below:
------
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 preventdelete caused an unexpected exception, contact your administrator: preventdelete: execution of BeforeDelete caused by: System.FinalException: SObject row does not allow errors: Trigger.preventdelete: line 14, column 1".
Click here to return to the previous page.
------
You are using u.addError where u is a user instance. Wont this error be displayed on user? While required is to display error on Loan__c
I received an error message on compiling apex trigger code itself.
--- Error: Compile Error: Variable does not exist: load.id at line 14 column 32 ---
Change it to loan.id
the above code is throwing an error to me , where i slightly changed my scenario . CAN YOU MODIFY MY CODE PLEASE
scenario : i am using account object here , so a when ever account record is created by a user and if the user is made inactive and if other user is trying to delete it it should throw an error .
my code :
trigger DeleteRecord on Account (before delete)
{
set<id> AcCreatedid = new set<id>();
for(Account acc :trigger.old)
{
system.debug('====acc.CreatedById====='+acc.CreatedById);
AcCreatedid.add(acc.CreatedById);
}
list<user> userlist = new list<user>([SELECT id ,name ,IsActive FROM User where id in:AcCreatedid ]);
for( user u:userlist )
{
if(u.IsActive == FALSE )
{
//User userRecord = Trigger.oldMap.get(AcCreatedid);
userRecord.addError('You cant delete this record since it is created by an inactive user');
}
}
}
getting error : There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger DeleteRecord caused an unexpected exception, contact your administrator: DeleteRecord: execution of BeforeDelete caused by: System.FinalException: SObject row does not allow errors: Trigger.DeleteRecord: line 19, column 1".
in google i checked how to resolve it : http://www.srinivas4sfdc.com/2013/12/systemexception-sobject-row-does-not.html
Please post it as another thread . I will look into into it.
Regards
Before triggers does not have access to values that are set by database on after insert(such as Created By, Created Date, Modified By, Modified Date etc.).
Regards
Now I got an error like this ---
It looks like I will have to get my hands dirty.
Alllow me some time and I will create the trigger , test it on dev org and paste it here.
THANKS for being patient.
Please try this code. I have made the error to display on your custom object.
Let me know if there is still any issues.
Please close the thread if it worked
Thankyou
Here is the working and tested code: (am working on account object, so replace it with your Loan object)
Here is the screenshot of output as expected:
This will work :)
Kindly mark as answered.
Thanks.
And here is the reason why your code did not work.
When accessing createdby.isactive, it always results in false!!
I am not sure why salesforce behaves this way
thanks both for helping , is this the correct output which we are getting for the above trigger ???? can you correct me
one question: was this question for your own benefit of learning or was it for a live scenario in your company?
i dont have any validation rules on account object , i just checked