You need to sign in to do that
Don't have an account?
Trigger to prevent delete record when record is created by site user
Hi all,
I am quite new to triggers. Using the forum I was able to create a trigger to prevent delete based on a picklist value. Now I am trying to write a trigger that will prevent delete based on 'who' the record is Createdby. We do not want users to be able to delete records created by the site user. Unfortunately I am not able to figure this one out. Can anyone help with the code?
Thanks!
trigger DeleteJobApplication on cxsrec__cxsJob_application__c(before delete){
if(system.Trigger.isDelete){
String Userid=[SELECT id,Name FROM User WHERE Id =:UserInfo.getuserid()].Name ;
for(cxsrec__cxsJob_application__c J:Trigger.old){
if(J.Createdby == 'ResourceManager Profile' ){
J.addError('Je kan de inschrijving niet verwijderen wanneer deze is gemaakt door de site user');
}
}
}
}
try this
trigger DeleteJobApplication on cxsrec__cxsJob_application__c (before delete) {
for(cxsrec__cxsJob_application__c ja: Trigger.old){
if(ja.CreatedById == Label.UserOwnerId){
ja.addError('De inschrijving kan niet verwijdert worden wanneer deze is gemaakt via de website');
}
}
}
Also make sure u create new custom label and out Site users Id in that. U can go to set up and search for Custom label
All Answers
for(cxsrec__cxsJob_application__c ja : Trigger.new){
if(ja.CreatedById = Label.UserOwnerId){
ja.addError('cant');
}
}
}
You can create a custom label by name UserOwnerId and put the User Id in the label. and then this trigger should work
Hi JSingh9,
Thanks, I tried your code but I then get the following error when I try to save: Field is not writeable: cxsrec__cxsJob_application__c.CreatedById
for(cxsrec__cxsJob_application__c ja: Trigger.new){
if(ja.CreatedById = Label.UserOwnerId){
ja.addError('De inschrijving kan niet verwijdert worden wanneer deze is gemaakt via de website');
}
}
}
trigger DeleteJobApplication on cxsrec__cxsJob_application__c (before delete) {
for(cxsrec__cxsJob_application__c ja: Trigger.new){
if(ja.CreatedById == Label.UserOwnerId){
ja.addError('De inschrijving kan niet verwijdert worden wanneer deze is gemaakt via de website');
}
}
}
try this
Hi,
I am now able to save the trigger but it prevents the user to delete any record, no matter who de user is who created the record, also it does not show the error message 'De inschrijving kan niet verwijdert worden wanneer deze is gemaakt via de website'. Instead it shows this: DeleteJobApplication: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.DeleteJobApplication: line 2, column 1"
try this
trigger DeleteJobApplication on cxsrec__cxsJob_application__c (before delete) {
for(cxsrec__cxsJob_application__c ja: Trigger.old){
if(ja.CreatedById == Label.UserOwnerId){
ja.addError('De inschrijving kan niet verwijdert worden wanneer deze is gemaakt via de website');
}
}
}
Also make sure u create new custom label and out Site users Id in that. U can go to set up and search for Custom label