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
SF Dev CenoSF Dev Ceno 

Batch Job: Set Account custom field value based off related object field value

All,
I'm trying to update a field value on the Account based off a value on a related object called License. The batch job seems to ignore the Days Remaining field and changing all the Accounts the initial query captures. It should only change the Relationship Type field on the Account if the Days Remaining on the License is less than 0. Please see code below:
 
// Briceno: ALCHEMISTS-1806 : Set Former Partner & Customer
// Update Account Relationship Type based on Account Type and License Days Remaining < 0


global class WR_SetFormerCustomerAndPartnerBatch implements Database.Batchable<sObject> {
    //License_Setting__c settings = License_Setting__c.getInstance();
    //Decimal updateLookback = settings.Update_Lookback_Value__c;
    //Date dateScope = System.today().addDays(Integer.valueOf(-updateLookback));
    String CURRENTCUSTOMER = 'Current Customer';
    String CURRENTPARTNER = 'Current Partner';

    global final String query;

    global WR_SetFormerCustomerAndPartnerBatch(){
        query = 'SELECT Id, Relationship_Type__c, Type, RecordTypeId FROM Account WHERE Relationship_Type__c LIKE :CURRENTCUSTOMER OR Relationship_Type__c LIKE :CURRENTPARTNER';
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext bc, List<Account> scope) {

        // Direct Licenses
    	List<License__c> lst_AccountLicenses = new List<License__c>();
        	lst_AccountLicenses = [SELECT Id, Days_Remaining__c, Account__c, Capability_Type__c  FROM License__c WHERE Account__c IN :scope AND Capability_Type__c = 'Full'];

      	// Indirect Licenses
    	List<License__c> lst_AccountRelatedLicenses = new List<License__c>();
        	lst_AccountRelatedLicenses = [SELECT Id, Days_Remaining__c, Opportunity__r.Reseller_Account__c, Opportunity__r.Distributor_Account__c, Capability_Type__c FROM License__c WHERE (Opportunity__r.Reseller_Account__c IN :scope OR Opportunity__r.Distributor_Account__c IN :scope ) AND Capability_Type__c = 'Full'];

        List<Account> lst_Account = new List<Account>();
        if(scope.size() > 0){
            lst_Account = [SELECT Id, RecordTypeId, Type, Relationship_Type__c FROM Account WHERE Id IN: scope];
        }

        Map<Id, Account> accMap = new Map<Id, Account>();
        	accMap.putAll(lst_Account);
	        if(!accMap.isEmpty()){
	        	update accMap.values();
	        }

        if(!accMap.isEmpty()){
            List<License__c> lst_License = new List<License__c>();
        	
        	/**for(Account s : scope){
        		Account acct = (Account) s;**/

        	for(Account acct : accMap.values()){
	        	
	            if(acct.Type.equals('Business')){
	                lst_License.addAll(lst_AccountLicenses);
	                lst_License.addAll(lst_AccountRelatedLicenses);
	            }

	            if(acct.Type.equals('Consumer')){
	                lst_License.addAll(lst_AccountLicenses);
	                lst_License.addAll(lst_AccountRelatedLicenses);
	            }

	            if(acct.Type.equals('MSP')){
	                lst_License.addAll(lst_AccountLicenses);
	            }
	            
	            if(acct.Type.equals('MSSP')){
	                lst_License.addAll(lst_AccountLicenses);
	            }
	            
	            if(acct.Type.equals('Platform')){
	                lst_License.addAll(lst_AccountLicenses);
	            }

	            if(acct.Type.equals('MSP/Reseller Hybrid')){
	                lst_License.addAll(lst_AccountLicenses);
	                lst_License.addAll(lst_AccountRelatedLicenses);
	            }

	            if(acct.Type.equals('Reseller') || acct.Type.equals('Distributor')){
	                lst_License.addAll(lst_AccountRelatedLicenses);
	            }

	            if(lst_License.size() > 0){
	                for(License__c license: lst_License){
	                    if(license.Days_Remaining__c < 0){
                    		// Record Type of End User (Types Include: Consumer, Business)
	                        if(acct.RecordTypeId == '01261000000Xtcz' && acct.Relationship_Type__c == 'Current Customer')
	                            acct.Relationship_Type__c = 'Former Customer';
	                            //accMap.put(acct.Id, acct);
	                            break;
	                    } else {
	                    	// Record Type of Partner (Types Include: MSP, MSP/Reseller Hybrid, MSSP, Reseller, Distributor, Platform, OEM, IoT, MSSP, Fraud, Retail)
	                    	if(acct.RecordTypeId == '01261000000Xtcu' && acct.Relationship_Type__c == 'Current Partner')
	                            acct.Relationship_Type__c = 'Former Partner';
	                            //accMap.put(acct.Id, acct);
	                        
	                    }
	                }
	            }
		    }

		    update accMap.values();
        }

    }

    global void finish(Database.BatchableContext bc) {
        AsyncApexJob a = [Select Id, ApexClassId, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email, ExtendedStatus, CompletedDate
                          From AsyncApexJob
                          Where Id = :bc.getJobId()
                          Order by CompletedDate];

        // Send the email to the job submitter
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {a.CreatedBy.Email};
        mail.setToAddresses(toAddresses);
        mail.setSenderDisplayName('WR_SetFormerCustomerAndPartnerBatch: Batch Results');
        mail.setSubject('Status: ' + a.Status);
        mail.setPlainTextBody( 'Job ID ' +a.ApexClassId +
                               ' processed ' + a.TotalJobItems +
                               ' batches with ' +a.JobItemsProcessed +
                               ' Job Items Processed, ' + a.NumberOfErrors + 
                               ' Failures, and Extended Status of: ' + a.ExtendedStatus);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

    }
}