Files
smartclone/force-app/main/default/classes/OpportunityCloneServiceTest.cls
T
cgary f34a61e653 Adds rule-based filtering for cloned records
Implements a mechanism to exclude child records from being cloned based on configurable formulas.

Introduces a Custom Metadata object to define exclusion rules, allowing administrators to specify formulas for different SObjects.

Updates the cloning service to evaluate these formulas and filter out records that meet the exclusion criteria.
2025-04-21 16:39:07 -04:00

128 lines
4.8 KiB
OpenEdge ABL

/**
* @description The class will provide code coverage for the OpportunityCloneService
* @author Cloud Performer <cgary@cloupderformer.com>
* @version 1.0
* @group Opportunity Cloning
* @history 2021-04-12 - CJG - Created
*/
@isTest
private class OpportunityCloneServiceTest {
@TestSetup
static void makeData(){
List<Account> acctTests = new List<Account>();
List<Product2> productList = new List<Product2>();
List<PricebookEntry> pricebookItems = new List<PricebookEntry>();
List<Opportunity> oppTests = new List<Opportunity>();
List<OpportunityLineItem> oliTestLines = new List<OpportunityLineItem>();
//create accounts
acctTests.add(new Account(
Name = TestCloneUtility.GenerateRandomString(10),
AccountNumber = TestCloneUtility.GenerateRandomString(8).toUpperCase(),
BillingStreet = '1 ' + TestCloneUtility.GenerateRandomString(10) + ' Street',
BillingCity = 'Atlanta',
BillingState = 'Georgia',
BillingPostalCode = '30303',
BillingCountry = 'USA' ,
ShippingStreet = '2 ' + TestCloneUtility.GenerateRandomString(8) + ' Street',
ShippingCity = 'Atlanta',
ShippingState = 'Georgia',
ShippingPostalCode = '30303',
ShippingCountry = 'USA'
));
insert acctTests;
//create Products
for(Integer i=0; i < 5;i++){
productList.add(new Product2(
Name=TestCloneUtility.GenerateRandomString(5),
isActive = true,
ProductCode = TestCloneUtility.GenerateRandomString(6).toUpperCase()
));
}
insert productList;
//obtain test Pricbook
Id testPricebookId = test.getStandardPricebookId();
//create PricebookEntry records
for(Product2 prod:productList){
pricebookItems.add(new PricebookEntry(
IsActive = true,
Pricebook2Id = testPricebookId,
UnitPrice = 1.0,
Product2Id = prod.Id
));
}
insert pricebookItems;
//create test Opportunities
Schema.DescribeFieldResult fr = Schema.SObjectType.Opportunity.fields.StageName;
List<Schema.PicklistEntry> stageValues = fr.getPicklistValues();
for(Account a:acctTests){
oppTests.add(new Opportunity(
Name = TestCloneUtility.GenerateRandomString(8),
CloseDate = System.today().addDays(30),
StageName = stageValues[0].getValue()
));
}
insert oppTests;
//create OppLineItems
for(Opportunity opp:oppTests){
for(PricebookEntry pbe:pricebookItems){
oliTestLines.add(new OpportunityLineItem(PricebookEntryId=pbe.Id,Quantity=1.0,UnitPrice=pbe.UnitPrice,
OpportunityId=opp.Id));
}
}
insert oliTestLines;
testFutureMethod();
}
@isTest
static void ObtainOpporunityChildRelationships(){
Test.startTest();
Map<String,String> resultMap = OpportunityCloneService.ObtainOpporunityChildRelationships();
System.assert(!resultMap.isEmpty());
Test.stopTest();
}
@isTest
static void ObtainCloneOpportunityFields(){
Test.startTest();
Map<String,Boolean> resultMap = OpportunityCloneService.ObtainCloneOpportunityFields('ClonedFields');
System.assert(!resultMap.isEmpty());
Test.stopTest();
}
@isTest
static void ObtainQueryStringForOppAndChildren(){
//get an opportunity
Opportunity testOpp = [SELECT Id FROM Opportunity LIMIT 1];
List<String> relStringList = new List<String>{'OpportunityLineItems'};
Test.startTest();
String queryStringTest = OpportunityCloneService.ObtainQueryStringForOppAndChildren(testOpp.Id, relStringList);
System.assert(String.isNotBlank(queryStringTest));
Test.stopTest();
}
@isTest
static void CloneOpportunityWithChildren(){
Test.startTest();
Opportunity testOpp = [SELECT Id, Name FROM Opportunity LIMIT 1];
Map<String,String> testReplacements = new Map<String,String>();
testReplacements.put('Name',TestCloneUtility.GenerateRandomString(10));
Id testResult = OpportunityCloneService.CloneOpportunityWithChildren(testOpp.Id, testReplacements,
new List<String>{'OpportunityLineItems'});
System.assertNotEquals(null, testResult);
Test.stopTest();
}
@future
public static void testFutureMethod(){
//insert some Exlusion rules
Test.loadData(SmartCloneRecordExclusionRule__mdt.SObjectType, 'SmartClone_TestData');
}
}