+ Start a Discussion
Practise DataPractise Data 

if record gets deleted which name starts with A and start date = last month then it should get undeleted ??How to write a script Please help

Hii Team,
I need to write this trigger,If anyone can help it would be apreciated
It's urgent
 
Santosh Kumar 348Santosh Kumar 348
Hi,

First of all I would like to suggest that insetead of restoring the record after deletion, have a validation in trigger in before delete if above condition is Satisfied the don't allow user to delete the record.
 
if (Trigger.isBefore) {
    if (Trigger.isDelete) {
 
        // In a before delete trigger, the trigger accesses the records that will be
        // deleted with the Trigger.old list.
        for (Account a : Trigger.old) {
            if (a.name != 'okToDelete') {
                a.addError('You can\'t delete this record!');
            } 
        }
    }
}

You can refer this in detail on below link :

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm

Coming back to your Orignal question in case you already deleted the record and want to restore it.
After you have deleted records, the records are placed in the Recycle Bin for 15 days, after which they are permanently deleted. While the records are still in the Recycle Bin, you can restore them using the undelete operation. If you accidentally deleted some records that you want to keep, restore them from the Recycle Bin using below code.
 
Account a = new Account(Name='Universal Containers');
insert(a);
insert(new Contact(LastName='Carter',AccountId=a.Id));
delete a;
 
Account[] savedAccts = [SELECT Id, Name FROM Account WHERE Name = 'Universal Containers' ALL ROWS]; 
try {
    undelete savedAccts;
} catch (DmlException e) {
    // Process exception here
}

You can read this in detail if you need any help on : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_undelete.htm

SImilar question is already answered on : https://salesforce.stackexchange.com/questions/107521/restoring-deleted-records-using-apex/107522

Please mark this as best answer if this helps.


Regards,
Santosh Kumar