You need to sign in to do that
Don't have an account?

Simple Trigger
Hi Experts,
I have total 3 quaries...
On Delete:
1. Query for related Recipient__c records. If count > 0, return error (sobject.addError())
"Please Cancel Request, or Remove Recipients before deleting"
2. Query for related Workflow_Instance__c records where Request_Status__c != 'Cancelled'. If count > 0, return error:
"Please Cancel Request before deleting"
On Update:
3. Query for related Workflow_Instance__c records where Request_Status__c != 'Cancelled', If count > 0, return error:
"You cannot edit a submitted Request"
Please modify below code.....
Trigger PreventDeletion1 on Request__c (before delete, before update)
{
if( Trigger.isDelete ) {
for (Request__c Pa : Trigger.Old)
{
list<Recipient__c> ChList1 = [select id, Request__c from Recipient__c where Request__c =: pa.id];
if(ChList1.size() > 0){
pa.adderror('Please Cancel Request, or Remove Recipients before deleting');
}
list<Workflow_Instance__c> ChList2 = [select id, Request__c from Workflow_Instance__c where Request_Status__c != 'Cancelled'];
if(ChList2.size() > 0)
{
pa.adderror('Please Cancel Request before deleting');
}
}
elseif ( Trigger.isUpdate )
for (Request__c Pa : Trigger.Old)
{
list<Workflow_Instance__c> ChList3 = [select id, Request__c from Workflow_Instance__c where Request_Status__c != 'Cancelled'];
if(ChList3.size() > 0)
{
pa.adderror('You cannot edit a submitted Request');
}
}
}
}
Thanks,
Manu
I have total 3 quaries...
On Delete:
1. Query for related Recipient__c records. If count > 0, return error (sobject.addError())
"Please Cancel Request, or Remove Recipients before deleting"
2. Query for related Workflow_Instance__c records where Request_Status__c != 'Cancelled'. If count > 0, return error:
"Please Cancel Request before deleting"
On Update:
3. Query for related Workflow_Instance__c records where Request_Status__c != 'Cancelled', If count > 0, return error:
"You cannot edit a submitted Request"
Please modify below code.....
Trigger PreventDeletion1 on Request__c (before delete, before update)
{
if( Trigger.isDelete ) {
for (Request__c Pa : Trigger.Old)
{
list<Recipient__c> ChList1 = [select id, Request__c from Recipient__c where Request__c =: pa.id];
if(ChList1.size() > 0){
pa.adderror('Please Cancel Request, or Remove Recipients before deleting');
}
list<Workflow_Instance__c> ChList2 = [select id, Request__c from Workflow_Instance__c where Request_Status__c != 'Cancelled'];
if(ChList2.size() > 0)
{
pa.adderror('Please Cancel Request before deleting');
}
}
elseif ( Trigger.isUpdate )
for (Request__c Pa : Trigger.Old)
{
list<Workflow_Instance__c> ChList3 = [select id, Request__c from Workflow_Instance__c where Request_Status__c != 'Cancelled'];
if(ChList3.size() > 0)
{
pa.adderror('You cannot edit a submitted Request');
}
}
}
}
Thanks,
Manu
There was a mistake which i have corrected now.
Please change your code with below code : Let me know if you still face any isue.
Thanks,
Abhishek
All Answers
What I understood you have 3 objects
1.Request__c Parent
2.Recipient__c child of Request
3.Workflow_Instance__c
Is there any relationship with Workflow_Instance__c with any of the two objects .
Please confirm to give better solution.
Thanks
Manoj
Please find your required code below :
Let me know if you have any issue in it or if you need any help.
Thanks,
Abhishek
Try with below trigger it will help ,However you need to check teh relationship name for both inner query .
Let me know if it helps !!
Thanks
Manoj
Error: Compile Error: line breaks not allowed in string literals at line 47 column -1
Error: Compile Error: unexpected token: ')' at line 7 column 90
Try with below code ,
Issue may come for relationship name try to change that incase issue .
Triggeris working or still any issue ?
I follow your code....
i got one error:
Error - Variable does not exist: reqIdWithRepSizeMap at line 16 column 16
Trigger PreventDeleteAndUpdate on Request__c (before delete, before update){
if(Trigger.isBefore){
if(Trigger.isDelete){
Map<Id,Integer> reqIdWithRepSizeMap=new Map<Id,Integer>();
for(Request__c req :[Select id,(SELECT id from Recipients__r ) FROM Request__c WHERE Id IN : Trigger.old]){
reqIdWithRepSizeMap.put(req.Id,req.Recipients__r.size());
}
for(Request__c reqq :Trigger.old){
if(reqIdWithRepSizeMap.get(reqq.id) >0){
reqq.adderror('Please Cancel Request, or Remove Recipients before deleting');
}
}
}if(Trigger.isDelete){
Map<Id,Integer> reqIdWithWrkinsSizeMap=new Map<Id,Integer>();
for(Request__c reqs :[Select id,(SELECT id FROM Workflow_Instance__r WHERE Request_Status__c != 'Cancelled' ) FROM Request__c WHERE Id IN : Trigger.new]){
reqIdWithRepSizeMap.put(reqs.Id,reqs.Workflow_Instance__r.size());
}
for(Request__c reqst :trigger.new){
if(reqIdWithWrkinsSizeMap.get(reqst.Id)>0){
reqst.adderror('You cannot update a submitted Request');
}
}
}
}
}
Please give it a try. Thanks,
Abhishek
I tried your code, i got below error.
Error: Variable does not exist: reci at line 19 column 59.
There was a mistake which i have corrected now.
Please change your code with below code : Let me know if you still face any isue.
Thanks,
Abhishek
Try with below code it wil help !! Let e know if it helps !!
Thanks
Manoj