+ Start a Discussion
AnnaTAnnaT 

Before Delete Trigger doesn't work

Please help me.

I'm creating a Trigger.

There are Object A and Object B.

Object B has a look up field of Object A.

What I want to do is when Object B is deleted, Object A's status is changed from "close" to "Open".

I created the following code, but the status of Object A isn't changed.

trigger SetStatus on ObjectB__c (before delete) {
  Set<Id> objId = new Set<Id>();
  list<ObjectA__c> objA = new list<ObjectA__c>();
  
  if (Trigger.isDelete)
  {
    for (ObjectB__c oldObjB : trigger.old)
    {
      objId.add(oldObjB.LookupField__c);
    }
    ObjectA__c objAList =[SELECT id, Status__c FROM ObjectA__c WHERE id in :objId ];
    if(objAList<>null){
      for(ObjectB__c objObjB : trigger.old)
      {
        objAList.Status__c = 'Open';
        objA.add(objAList);
      }
     if(objA.size()>0)
     {
     update objAList;
     }
   }
 }
}

Thanks in advance for your cooperation!!!

 

A.Tanaka

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Hi Anna,

 

Please make the changes as below :-

 

trigger SetStatus on ObjectB__c (before delete) {
Set<Id> objId = new Set<Id>();
list<ObjectA__c> objA = new list<ObjectA__c>();

if (Trigger.isDelete)
{
for (ObjectB__c oldObjB : trigger.old)
{
objId.add(oldObjB.LookupField__c);
}
ObjectA__c objAList =[SELECT id, Status__c FROM ObjectA__c WHERE id in :objId ];
if(objAList<>null){
for(ObjectA__c objObja : objAList)
{
objObja.Status__c = 'Open';
objA.add(objObja);
}
if(objA.size()>0)
{
update objA;
}
}
}
}

All Answers

Vinit_KumarVinit_Kumar

Hi Anna,

 

Please make the changes as below :-

 

trigger SetStatus on ObjectB__c (before delete) {
Set<Id> objId = new Set<Id>();
list<ObjectA__c> objA = new list<ObjectA__c>();

if (Trigger.isDelete)
{
for (ObjectB__c oldObjB : trigger.old)
{
objId.add(oldObjB.LookupField__c);
}
ObjectA__c objAList =[SELECT id, Status__c FROM ObjectA__c WHERE id in :objId ];
if(objAList<>null){
for(ObjectA__c objObja : objAList)
{
objObja.Status__c = 'Open';
objA.add(objObja);
}
if(objA.size()>0)
{
update objA;
}
}
}
}

This was selected as the best answer
AnnaTAnnaT

Hi Vinit_Kumar,

 

Thank you very much for your comment.

 

I modified my code, but the following error occurrs.

 

Error: Compile Error: Loop must iterate over a collection type: SOBJECT:ObjectA at line 13 column 26

 

error occurs here ->  for(ObjectA__c objObja : objAList)



How can I eliminate this error?

 

Best regards,

Anna

AnnaTAnnaT

The error disappeared with this code:

List<ObjectA__c> objAList =[SELECT id, Status__c FROM ObjectA__c WHERE id in :objId ];

 But the status is not changed to "Open"...

AnnaTAnnaT

Hi again Vinit_Kumar,

 

I found that another trigger was blocking this trigger.

When I invalid that trigger, the status is changed to "Open".

 

Thank you so much for your help!!!!!

 

Best regards,

Anna