Untitled

 avatar
unknown
java
a year ago
1.5 kB
7
Indexable
try {
	changeAutomationState(true);
	
	List<Product2> demotedProductsToPrimary = [SELECT Id FROM Product2 WHERE Name LIKE '% (subscription)' OR Name LIKE '% (Subscription)'];
	setCategory(demotedProductsToPrimary, 'Primary');

	List<Product2> bundleParents = [SELECT Id FROM Product2 WHERE Name LIKE '% (bundle)' OR Name LIKE '% (Bundle)'];
	setCategory(bundleParents, 'Bundle Parent');

	List<Product2> primaryProducts = [SELECT Id FROM Product2 WHERE IsActive = TRUE AND SBQQ__Component__c = FALSE AND SBQQ__SubscriptionType__c = 'Renewable' AND Id NOT IN :Utils.getIdSet(bundleParents) AND 	Subscription_Category__c != 'Primary'];
	setCategory(primaryProducts, 'Primary');

	List<Product2> options = [SELECT Id FROM Product2 WHERE IsActive = TRUE AND SBQQ__Component__c = TRUE AND SBQQ__SubscriptionType__c = 'Renewable' AND 	Subscription_Category__c != 'Primary'];
	setCategory(options, 'Option');
} catch (Exception e) {
	throw e;
} finally {
	changeAutomationState(false);
}

static void setCategory(List<Product2> products, String value) {
	for (Product2 pro : products) {
		pro.Subscription_Category__c = value;
	}
	update products;
}


static void changeAutomationState(Boolean valueToSet) {
	SObject controlSetting = Control_Product__c.getInstance();
	controlSetting.put('Trigger_Disable_All_Operations__c', valueToSet);
	controlSetting.put('Flow_Disable_All_Flows__c', valueToSet);
	controlSetting.put('Validation_Disable_All_Rules__c', valueToSet);
	upsert controlSetting;
}
Editor is loading...
Leave a Comment