+ Start a Discussion
sandeep reddy 37sandeep reddy 37 

undelete condeation cheack

I want to check deleted record assocated user id and undeleted record assocated user id must be equql other wise throws error
trigger undeletecheack on Account (after delete,after undelete) {
    set<id> personids=new set<id>();
    for(account c:trigger.old){
        if(trigger.isafter&&trigger.isdelete){
     user u=[select id,name from user where id=:userinfo.getUserId()]; 
       personids.add(u.id); 
    }
                              }
    for(account aa:trigger.new){
        if(trigger.isafter&&trigger.isundelete){
         user u2=[select id,name from user where id=:userinfo.getUserId()];
        list<user> lu=[select id,name from user where id=:personids];
        for(user i:lu){
            if(i.id==u2.id){
               
            } 
        }
        }
    }
}
advance thanks 
 
VineetKumarVineetKumar
Your requirememt is not clear, infact your code is doing nothing but just comparing the same values.
sandeep reddy 37sandeep reddy 37
Hi vineet 

for example you want  to undelete record from recycle bin  running user id and deleted record assocated user id must be equal  
thanks
sandeep
VineetKumarVineetKumar
Your trigger would looks something like this :
trigger undeletecheack on Account (after delete, after undelete) {                              
    // To avoid undeleting or record
	if(trigger.isafter && trigger.isundelete){
		for(account aa : trigger.new){
			if(aa.ownerId != userinfo.getUserId()){
				aa.addError('You don\'t have the sufficient right to undelete this record.');
			} 
		}
	}
	
	// To avoid deletion or record
	if(trigger.isafter && trigger.isdelete){
		for(account aa : trigger.old){
			if(aa.ownerId != userinfo.getUserId()){
				aa.addError('You don\'t have the sufficient right to delete this record.');
			} 
		}
	}
}

This trigger will only allow the owner of the record to delete and undelet the record.
Let me know if that helped.
sandeep reddy 37sandeep reddy 37
Hi veenit thats ok its help ful but depending upon who deleted the recore that person have access to undeletd record Thanks sandeep
VineetKumarVineetKumar
I'm not sure what you meant by that
sandeep reddy 37sandeep reddy 37
if you delete the record you only acces to undelete the record know one undelete the record not dependig upon owner 
 
sandeep reddy 37sandeep reddy 37
i got it but its not working here is the trigger trigger undeletecheack on Account (after delete,after undelete) { set personids=new set(); account a=new account(); for(account c:trigger.old){ if(trigger.isafter&&trigger.isdelete){ user u=[select id,name from user where id=:userinfo.getUserId()]; c.srinivasreddy__deletedby__c=u.name; } } for(account aa:trigger.new){ if(trigger.isafter&&trigger.isundelete){ if(aa.srinivasreddy__deletedby__c==userinfo.getName()){ } } } undelete a; }
VineetKumarVineetKumar
Check this code :
trigger undeletecheack on Account (before delete, after undelete) { 	
	if(trigger.isBefore && trigger.isdelete){
		for(account c : trigger.old){ 		 
			c.srinivasreddy__deletedby__c = userinfo.getName(); 
		} 
	}
	
	if(trigger.isafter && trigger.isundelete){
		for(account aa:trigger.new){ 		 
			if(aa.srinivasreddy__deletedby__c == userinfo.getName()){ 
				aa.addError('You cannot undelete this record');
			} 
		} 
	} 
}
sandeep reddy 37sandeep reddy 37

i got this errror


Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger srinivasreddy.undeletecheack caused an unexpected exception, contact your administrator: srinivasreddy.undeletecheack: execution of BeforeDelete caused by: System.FinalException: Record is read-only: Trigger.srinivasreddy.undeletecheack: line 4, column 1". 

Click here to return to the previous page.
sandeep reddy 37sandeep reddy 37
thats ok but system validation throws after deleted record you can not perform update 

vineet can we perform dml on deleted record
VineetKumarVineetKumar
No, you cannot perform a DML on deleted record.
sandeep reddy 37sandeep reddy 37
can we do this or not
VineetKumarVineetKumar
This is possible, but you may have to use SOAP API to get the info.
Reference :
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_queryall.htm.
But writing a future method would be recommended here.
Also, I'm not getting the point to why you want to put such kind of limitation. There seems to be a gap in your implementation, perhaps you may want to revisit your delete permission.
sandeep reddy 37sandeep reddy 37
system adminstrator and deleted user have access to undelete but administor only one knows that last record deletedy which user that will register on object right ?
 

 
VineetKumarVineetKumar
Nope
sandeep reddy 37sandeep reddy 37
ok vineet thanks for help 
sandeep reddy 37sandeep reddy 37
can u give me small example with soap api calls using queryall(),and get deleted()

Thanks