function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rachel van den BergRachel van den Berg 

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');
            }
     }
     }
     }

Best Answer chosen by Rachel van den Berg
JSingh9JSingh9

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

JSingh9JSingh9
trigger DeleteJobApplication on cxsrec__cxsJob_application__c (before delete) {
    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
Rachel van den BergRachel van den Berg

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

 

JSingh9JSingh9
can u share the code?
Rachel van den BergRachel van den Berg
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');
        }
    }
}
JSingh9JSingh9
Sorry one mistake 

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 
Rachel van den BergRachel van den Berg

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"

JSingh9JSingh9

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

This was selected as the best answer
Rachel van den BergRachel van den Berg
Thanks JSingh9, this made it work!