function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PEDRO PRADAPEDRO PRADA 

Unlock approval using apex certain condition

Hi everyone, this is my first post seeking some assistance.  I am new to Apex and looking to get some code to get it to work....

The business requirement is that an approval is unlocked after 30 days of approval date and that the standard field status is set to "Expired" (have not added this to logic.

Can someone look at this code and tell me how to fix it and add the logic to set the quote.status to expired?

public class UnlockApprovedDelivery //Get records to unlock
{
    List<Quote> quoteList = [SELECT Id From Quote WHERE Rate_Expiry_Date__c > TODAY];
//Check locked records
    List<Quote> quoteLockList = new List<Quote>();
for(Quote q:quoteList){
    if(Approval.isLocked(q.id)){
        quoteLockList.add(q);
    }
}
//Unlock record
if(!quoteLockList.isEmpty()){
    //Unlock records
    List<Approval.UnlockResult> ulrList = Approval.unlock(quoteLockList, false);
     
    // Iterate through each returned result
    for(Approval.UnlockResult  ulr : ulrList) {
        if (ulr.isSuccess()) {
            //Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully locked account with ID: ' + ulr.getId());
        }
        else {
            //Operation failed, so get all errors                
            for(Database.Error err : ulr.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Case fields that affected this error: ' + err.getFields());
            }
        }
    }
}

 
karthikeyan perumalkarthikeyan perumal
Hello, 

i have added some logic in this code in unlock record. its not qccurate one so u need to align this code. and create on custom date field in quote to update record approve date. so that we can calculate days. 
 
public class UnlockApprovedDelivery //Get records to unlock
{
    List<Quote> quoteList = [SELECT Id, QuotelockedDate__c From Quote WHERE Rate_Expiry_Date__c > TODAY];
//Check locked records
    List<Quote> quoteLockList = new List<Quote>();
	List<Quote> quotestatusList = new List<Quote>();
for(Quote q:quoteList){
    if(Approval.isLocked(q.id)){
        quoteLockList.add(q);
    }
}
//Unlock record
if(!quoteLockList.isEmpty()){
    //Unlock records
    List<Approval.UnlockResult> ulrList = Approval.unlock(quoteLockList, false);
     
    // Iterate through each returned result
    for(Approval.UnlockResult  ulr : ulrList) {
        if (ulr.isSuccess()) {
            //Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully locked account with ID: ' + ulr.getId());
			
			for(Quote q:quoteList){
			if ( !Approval.isLocked(q.id)){
				
			   Integer noOfDaysBetweenDates = Date.Today().daysBetween(Date.valueof(q.QuotelockedDate__c))
			   if(noOfDaysBetweenDates >=30)
			   {
				   q.status='Expired';
				   quotestatusList.add(q);
			   }
			   
			}
		    }
        }
        else {
            //Operation failed, so get all errors                
            for(Database.Error err : ulr.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Case fields that affected this error: ' + err.getFields());
            }
        }
    }
}

hope this will give you some idea. 

Thanks
karthik