+ Start a Discussion
MigMig 

after undelete - isDeleted

In this org:  

Affiliations__c is a non master relationship to Contact. 

If a Contact is deleted, a trigger is fired and will delete the related affiliations! 

 

What I want to do is : if a contact is undeleted, undelete the deleted related affiliations as well

 

My code :   

 

trigger ContactAfterUndelete on Contact (after undelete) {

Affiliation__c[] affiliationsFired ;

Affiliation__c[] affiliationsToUpdate = new Affiliation__c[]{};

affiliationsFired = [Select id from Affiliation__c where isDeleted = true and (Affiliation_From_Contact__c in :Trigger.old or Affiliation_To_Contact__c in: Trigger.old) ALL ROWS] ;

for (Integer i=0 ; i< affiliationsFired.size() ; i++){

affiliationsFired[i].isDeleted = false ;

AffiliationsToUpdate.add(affiliationsFired[i]);

}

update AffiliationsToUpdate ;}

 

I've got an error because isDeleted is non Writeable! 

 

Please, Can you give me some advices on how to solve this issue ? 

 

Tkx,

Mig 

 

Message Edited by Mig on 03-03-2009 04:33 PM
Best Answer chosen by Admin (Salesforce Developers) 
aalbertaalbert
You are trying to query and update the deleted records. Instead, use the undelete DML operation. This is the proper way to undelete a record. And that will internally set the isDeleted flag back to false. After the undelete, you might need to reset the lookup field to link to the proper Contact by Id.

All Answers

aalbertaalbert
You are trying to query and update the deleted records. Instead, use the undelete DML operation. This is the proper way to undelete a record. And that will internally set the isDeleted flag back to false. After the undelete, you might need to reset the lookup field to link to the proper Contact by Id.
This was selected as the best answer
MigMig
It works perfectly ! Thanks a lot one more time aalbert! =)