You need to sign in to do that
Don't have an account?
Anoop Patel 7
How can you prevent users from deleting a record based on certain criteria?
I have written the code below to stop any users from deleting a record on my custom object (Service Items) if the related Opportunity is Closed Won. This is within another trigger that I have already developed and tested but now when I try to save I get a error message 'Error: Compile Error: line breaks not allowed in string literals at line 36 column -1'. I'm not even sure if the new piece of code will do what I want.
if(Trigger.isDelete && Trigger.isBefore){
for (Service_Item__c si: trigger.old){
if(si.Opportunity__c!= null){
qoppIds.add(si.Opportunity__c);
system.debug('oqppid++'+ qoppIds);
}
}
qoppdetails = [select Id,StageName from Opportunity where Id in : qoppIds];
system.debug('qoppdetails++'+ qoppdetails);
for(Opportunity q : qoppdetails){
if(q.StageName=='Closed Won') - this is line 36 in my code.
{
q.adderror(‘Do Not Delete.Thank you.');
}
}
}
if(Trigger.isDelete && Trigger.isBefore){
for (Service_Item__c si: trigger.old){
if(si.Opportunity__c!= null){
qoppIds.add(si.Opportunity__c);
system.debug('oqppid++'+ qoppIds);
}
}
qoppdetails = [select Id,StageName from Opportunity where Id in : qoppIds];
system.debug('qoppdetails++'+ qoppdetails);
for(Opportunity q : qoppdetails){
if(q.StageName=='Closed Won') - this is line 36 in my code.
{
q.adderror(‘Do Not Delete.Thank you.');
}
}
}
Try this
All Answers
Set<Id> oppIds = new Set<Id>();
List<Opportunity> oppdetails = new List <Opportunity>();
Set<Id> qoppIds = new Set<Id>();
List<Opportunity> qoppdetails = new List <Opportunity>();
for(Service_Item__c ser : trigger.new){
if(ser.Opportunity__c!= null){
oppIds.add(ser.Opportunity__c);
system.debug('oppid++'+ oppIds);
}
}
oppdetails = [select Id,Accountid from Opportunity where Id in : oppIds];
system.debug('oppdetails++'+ oppdetails);
for(Service_Item__c ser : trigger.new){
if(ser.Opportunity__c != null) {
ser.Account__c = oppdetails[0].Accountid;
}
}
if(Trigger.isDelete && Trigger.isBefore){
for (Service_Item__c si: trigger.old){
if(si.Opportunity__c!= null){
qoppIds.add(si.Opportunity__c);
system.debug('oqppid++'+ qoppIds);
}
}
qoppdetails = [select Id,StageName from Opportunity where Id in : qoppIds];
system.debug('qoppdetails++'+ qoppdetails);
for(Opportunity q : qoppdetails){
if(q.StageName=='Closed Won')
{
q.adderror(‘Do Not Delete.Thank you.');
}
}
}
}
First quotation mark is incorrect. It looks like apostrophe mark. Try to replace iwth this
trigger PopulateAccountfromOpp on Service_Item__c(before insert, before update, Before Delete){
}
I have made the changes and now receiving the error below when I delete the Service Item record from Opportunity or the record itself.
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger PopulateAccountfromOpp caused an unexpected exception, contact your administrator: PopulateAccountfromOpp: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.PopulateAccountfromOpp: line 9, column 1".
From Docs about Trigger.new
"Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers."
So you cannot use Trigger.new in Delete trigger.
What can I use then to make this work?
Try this.
Error: Compile Error: Variable does not exist: qoppdetails at line 37 column 5
qoppdetails = [select Id,StageName from Opportunity where Id in : qoppIds];
Try this
Code saved successfully but it allows me to delete the record from both the Opp and the Service Item objects.
and tell me if your code reaches this line?
the before delete function is missing and code works as expected!
Thanks Denis much appreciated.
I'm glad to hear that you found the solution