/***********

Copyright Dave Vacanti, Magic Robot, Inc. 2018**

The following is provided publicly as educational material for the Salesforce.com administrator and developer community, specifically those learning Apex.

This material (and any derivative works) may not be used in any managed package published to the Salesforce AppExchange.

However, this material may be used freely to customize an individual Salesforce.com org.  Derivative works are encouraged.

This material may be copied and published anywhere, provided this complete copyright notice is also published at the top or with the other notices.

If you’re a consultant looking at this on behalf of a client, please contact Magic Robot at info@magicrobot.com to learn about pre-built components we can deliver to you where you get what you need and don’t have to re-invent the wheel — and Magic Robot gets: (a) new colleagues and (b) more in vivo insight to real clients who need some form of customization in this area. 

Let us know how we can help!

*/

 

Trigger on Opportunity Product Makes the App Go and submits the Data to the Classes that do the work.  Future editions will rely heavily on asynchronous options that can also create retroactive data.  To be clear this is not a complete architecture nor would Magic Robot endorse this as a fully baked solution.  As a “start-now-and-produce-data-going-forward-with-these-settings” solution, this works reliably. 

 

 

trigger OppLineItemAft_DelInsUpd on OpportunityLineItem (after delete, after insert, after update) {

/* Copyright Dave Vacanti, Magic Robot, Inc. 2018***

purpose of this trigger is to:a) Determine if it should run (Custom setting says to run, or relevant fields have changed their values)

b) submit a set of Opportunity Ids to a separate class to make updates to the oppportunities affected

i) if a custom setting says to use @future for update, it will do its work using the @future

(//@future is probably preferable in most ongoing settings

///whereas maintenance should be

//performed in the original triggers transaction and @future not used)

//It will reference both custom metadata types
//And Custom Settings

*/

 

MR_OLI__App_Settings__c CustomSetting = MR_OLI__App_Settings__c.getOrgDefaults();

boolean AppActivated;
boolean Triggerdisabled;
boolean RunAllOppsThru;
boolean UseAtFutureWhenPossible;

AppActivated = CustomSetting.MR_OLI__Activate_Application__c;
Triggerdisabled = CustomSetting.MR_OLI__Disable_OLI_Trigger__c;
RunAllOppsThru = CustomSetting.MR_OLI__Run_all_Opps_Thru_Update__c;
UseAtFutureWhenPossible = CustomSetting.MR_OLI__Use_At_Future_to_Update_Opps__c;

if(AppActivated == true){

if(triggerdisabled == false){

if(MR_OLI.MROppSummTrafficController.MRTestsSettingUpData == false){

Set OppIdsAlreadyProcessed = new Set();
if(MR_OLI.MROppSummTrafficController.OppIdsAlreadyProcessed != null){
OppIdsAlreadyProcessed = MR_OLI.MROppSummTrafficController.OppIdsAlreadyProcessed;
}

Set OppIds = new Set();

if (Trigger.isDelete) {
for (OpportunityLineItem a : trigger.old) {
if(!OppIdsAlreadyProcessed.contains(a.OpportunityId)){
OppIds.add(a.OpportunityId);
OppIdsAlreadyProcessed.add(a.OpportunityId);
}
}
}
if(Trigger.isInsert){

for (integer i = 0; i < Trigger.new.size(); i++) {
OpportunityLineItem a = Trigger.new[i];
system.debug(LoggingLevel.INFO, ‘This is debug: i now =: ‘+i);
if(!OppIdsAlreadyProcessed.contains(a.OpportunityId)){
OppIds.add(a.OpportunityId);
OppIdsAlreadyProcessed.add(a.OpportunityId);
}
}
}//end of isInsert

if(Trigger.isUpdate){

set FieldsToCheck = New Set();
FieldsToCheck.add(‘ProductCode’);
FieldsToCheck.add(‘OpportunityId’);
FieldsToCheck.add(‘Product2Id’);

string LineMapQueryString = MR_OLI.AppUtilities.csLineToParentQueryString;

if(MR_OLI.AppUtilities.MRLineToParentCSList == null){
MR_OLI.AppUtilities.MRLineToParentCSList = database.query(LineMapQueryString);
}

if(MR_OLI.AppUtilities.MRLineToParentCSList != null){
for(MR_OLI__Line_to_Parent_Map__c metaLine :MR_OLI.AppUtilities.MRLineToParentCSList){
FieldsToCheck.add(metaLine.MR_OLI__Line_Source_Field__c);
system.debug(LoggingLevel.INFO, ‘This is debug: just added the field to the set FieldsToCheck: ‘+metaLine.MR_OLI__Line_Source_Field__c);
}
}

for (integer i = 0; i < Trigger.new.size(); i++) {

Boolean ExamineOpp = false;

system.debug(LoggingLevel.INFO, ‘This is debug: i now =: ‘+i);

OpportunityLineItem newLI = Trigger.new[i]; if(!OppIdsAlreadyProcessed.contains(newLI.OpportunityId)){

if(RunAllOppsThru == false){

try{

OpportunityLineItem oldLI = trigger.oldmap.get(newLI.id); for(string s:FieldsToCheck){ system.debug(LoggingLevel.INFO, ‘THis is debug: checking the field: ‘+s); if(ExamineOpp == false){ //if(newLI.get(s) != null){ //if(oldLI.get(s) != null){ if(newLI.get(s) != oldLI.get(s)){ system.debug(LoggingLevel.INFO, ‘This is debug: setting ExamineOpp to true because NewLI.get(s) and oldLI.get(s) are different: ‘+NewLI.get(s)+’ and ‘+oldLI.get(s)+’, respectively’); ExamineOpp = true; } //} //} } }//end of string to check for loop } catch (exception e){ system.debug(LoggingLevel.INFO, ‘This is debug: There was an error processing the change check: ‘+e); } } else { ExamineOpp = true; system.debug(LoggingLevel.INFO, ‘THis is debug: because settings say to analyze all opps, just set ExamineOpp to true’); } if(ExamineOpp == true){ OppIds.add(newLI.OpportunityId); OppIdsAlreadyProcessed.add(newLI.OpportunityId); } }//end of this Opp already being processed }//end of for loop }//end of isUpdate MR_OLI.MROppSummTrafficController.OppIdsAlreadyProcessed = OppIdsAlreadyProcessed; System.debug(LoggingLevel.Info,’This is debug: OppIds is:’ + OppIds); System.debug(LoggingLevel.Info,’This is debug: Traffc.Oppids is:’ + MR_OLI.MROppSummTrafficController.OppIdsAlreadyProcessed); if(OppIds != null && OppIds.size()>0){
try{

boolean RunAtFuture = false;
if(UseAtFutureWhenPossible == true){
if(!System.isFuture() && !System.isBatch() && !System.isQueueable() && !System.isScheduled()
&& !test.isrunningtest()
){
RunAtFuture = true;
}
}

if(RunAtFuture == true){
MR_OLI.OppIdLineSummarizerAtFuture.SubmitIdsToLineSummarizer(OppIds);

} else {

MR_OLI.OppIdLineSummarizer.SummarizeTheseOppIds(OppIds);
}

} catch (exception e){

system.debug(LoggingLevel.INFO, ‘This is debug: there was a problem submitting the opps to the class to do the work: ‘+e);

}//end of catch

}//end of OppIds being not null

}//end of not setting up Magic Robot data during a test

}//end of this trigger not being disabled

}//end of custom setting being not null

}

 

 

 

global class OppIdLineSummarizer{global static void SummarizeTheseOppIds(Set OppIds){List OppsToUpdate = New List();//Set StringsToConcatForOpp = New Set();
//Set DecimalsToSumForOpp = New Set();

Map<String,Map<String,Set>> OppIdStringAttributeStringSet = new Map<String,Map<String,Set>>();
Map<String, Set> OppIdAttributeConcat_StringSet = new Map<String, Set>();

Map<String, String> TargetFieldNameSourceFieldNameMap = New Map<String, String>();
Map<String, set> SourceFieldNameTargetFieldsNameMap = New Map<String, set>();
Map<String, String> SourceFieldName_TypeMap = new Map<string,String>();
Map<String, integer> TargetFieldName_MaxLengthMap = new Map<String, integer>();

String UtilOLIString = MR_OLI.AppUtilities.OppLineItemFieldQueryString;
String MetaDOLiFields = ”;

List OLIList = New List();

string LineMapQueryString = MR_OLI.AppUtilities.csLineToParentQueryString;

//do a utilities class call that does a
//for loop here that will get the metadatasettings for fields
//then compile them to string for query

if(MR_OLI.AppUtilities.MRLineToParentCSList == null){
MR_OLI.AppUtilities.MRLineToParentCSList = database.query(LineMapQueryString);
}

if(MR_OLI.AppUtilities.MRLineToParentCSList != null){
for(MR_OLI__Line_to_Parent_Map__c metaLine :MR_OLI.AppUtilities.MRLineToParentCSList){
TargetFieldNameSourceFieldNameMap.put(metaLine.MR_OLI__Parent_Target_Field__c, metaLine.MR_OLI__Line_Source_Field__c);
SourceFieldName_TypeMap.put(metaLine.MR_OLI__Line_Source_Field__c, metaLine.MR_OLI__Source_Field_Type__c);
if(metaLine.MR_OLI__Max_Length_of_Target_String_Field__c != null){
integer ThisFieldMaxLength;
ThisFieldMaxLength = integer.valueof(metaLine.MR_OLI__Max_Length_of_Target_String_Field__c);

TargetFieldName_MaxLengthMap.put(metaLine.MR_OLI__Parent_Target_Field__c, ThisFieldMaxLength);
}

Set TempSet = new Set();
if(SourceFieldNameTargetFieldsNameMap.get(metaLine.MR_OLI__Line_Source_Field__c) == null){
TempSet.add(metaLine.MR_OLI__Parent_Target_Field__c);
SourceFieldNameTargetFieldsNameMap.put(metaLine.MR_OLI__Line_Source_Field__c, TempSet);
} else {
TempSet = SourceFieldNameTargetFieldsNameMap.get(metaLine.MR_OLI__Line_Source_Field__c);
TempSet.add(metaLine.MR_OLI__Parent_Target_Field__c);
SourceFieldNameTargetFieldsNameMap.put(metaLine.MR_OLI__Line_Source_Field__c, TempSet);
}

if(!MetaDOLiFields.contains(metaLine.MR_OLI__Line_Source_Field__c)
&& !UtilOLIString.contains(metaLine.MR_OLI__Line_Source_Field__c)
){
if(MetaDOLiFields.length()>0){
MetaDOLiFields += ‘, ‘;
}
MetaDOLiFields += metaLine.MR_OLI__Line_Source_Field__c;
}
}//end of for loop
}//end of there being mdt

if(MetaDOLiFields.length()>0){
UtilOLIString += ‘, ‘;
}

String QueryString = ‘SELECT ‘+UtilOLIString+’ ‘+MetaDOLiFields+’ FROM OpportunityLineItem WHERE OpportunityId in :OppIds ORDER BY OpportunityId ASC’;
system.debug(LoggingLevel.INFO, ‘This is debug: the QueryString is: ‘+QueryString);
try{
OLIList = database.query(QueryString);
} catch (exception e){
system.debug(LoggingLevel.INFO, ‘This is debug: there was an exception thrown running a query: ‘+e);
}

system.debug(‘This is debug: OLIList is: ‘+OLIList);

//A) Loop through the values, put them in:
// Map<OppId, Map>
// Map<OppId, Map
// Map<OppId, Map

//normalizing for ProductCode Consolodiation
if((SourceFieldName_TypeMap.keyset().size()==0 && Test.isRunningTest() == false) || MR_OLI.MROppSummTrafficController.MRTestsRunningEmulateMdt == true){
SourceFieldName_TypeMap.put(‘ProductCode’, ‘string’);
}

if((TargetFieldNameSourceFieldNameMap.keyset().size()==0 && Test.isRunningTest() == false) || MR_OLI.MROppSummTrafficController.MRTestsRunningEmulateMdt == true){
TargetFieldNameSourceFieldNameMap.put(‘Short_Opportunity_Product_Part_Numbers__c’, ‘ProductCode’);
TargetFieldNameSourceFieldNameMap.put(‘Opportunity_Products_Part_Numbers__c’, ‘ProductCode’);
}
if((SourceFieldNameTargetFieldsNameMap.keyset().size()==0 && Test.isRunningTest() == false) || MR_OLI.MROppSummTrafficController.MRTestsRunningEmulateMdt == true){
set TempSet = new set();
TempSet.add(‘Opportunity_Products_Part_Numbers__c’);
tempSet.add(‘Short_Opportunity_Product_Part_Numbers__c’);
SourceFieldNameTargetFieldsNameMap.put(‘ProductCode’,TempSet);
}
if((TargetFieldName_MaxLengthMap.keyset().size()==0 && Test.isRunningTest() == false) || MR_OLI.MROppSummTrafficController.MRTestsRunningEmulateMdt == true){
TargetFieldName_MaxLengthMap.put(‘Short_Opportunity_Product_Part_Numbers__c’, 250);
TargetFieldName_MaxLengthMap.put(‘Opportunity_Products_Part_Numbers__c’, 32000);
}

for(OpportunityLineItem oli:OLIList){
for(string sourcefieldname :SourceFieldName_TypeMap.keyset()){
string fieldtype = ‘string’;
if(SourceFieldName_TypeMap.get(sourcefieldname) != null){
fieldtype = SourceFieldName_TypeMap.get(sourcefieldname);
}
if(fieldtype == ‘string’){
if(oli.get(sourcefieldname) != null){
string fieldvalue = string.valueof(oli.get(sourcefieldname));
system.debug(LoggingLevel.INFO, ‘This is debug: fieldvalue is: ‘+fieldvalue);
string ThisOppIdAttributeString = ”;
ThisOppIdAttributeString = oli.OpportunityId+’_’+sourcefieldname;
if(OppIdAttributeConcat_StringSet.get(ThisOppIdAttributeString) == null){
set TempSet = new Set();
TempSet.add(fieldvalue);
OppIdAttributeConcat_StringSet.put(ThisOppIdAttributeString, TempSet);
} else {
set TempSet = new Set();
TempSet = OppIdAttributeConcat_StringSet.get(ThisOppIdAttributeString);
TempSet.add(fieldvalue);
OppIdAttributeConcat_StringSet.put(ThisOppIdAttributeString, TempSet);
}
}
}
}
}

system.debug(LoggingLevel.INFO, ‘This is debug: OppIdAttributeConcat_StringSet is: ‘+OppIdAttributeConcat_StringSet);

//B) Then loop thru the map sets for the strings and build the strings and set the fields

//string ProductCodeAttribute = ‘ProductCode’;
//StringsToConcatForOpp.add(ProductCodeAttribute);

for(string OppId: OppIds){

Opportunity p = new Opportunity();
p.id = OppId;

boolean updateThisOpp = false;
for(string OppAttribute :SourceFieldNameTargetFieldsNameMap.keyset()){
String FinalValue = ”;

String ConcatKey = OppId+’_’+OppAttribute;
system.debug(LoggingLevel.INFO, ‘This is debug: ConcatKey is: ‘+ConcatKey);
if(OppIdAttributeConcat_StringSet.get(ConcatKey) != null){
set ThisSetToStringOut = new Set();
ThisSetToStringOut = OppIdAttributeConcat_StringSet.get(ConcatKey);
for(string s: ThisSetToStringOut){
FinalValue += s+’; ‘;
}

}
//if(FinalValue.length()>0){
system.debug(LoggingLevel.INFO, ‘This is debug: The FinalValue of String is: ‘+FinalValue);
updateThisOpp = true;
set SetOfOppFieldsToFillwiththisvalue = new set();
system.debug(LoggingLevel.INFO, ‘This is debug: SourceFieldNameTargetFieldsNameMap is: ‘+SourceFieldNameTargetFieldsNameMap);
if(SourceFieldNameTargetFieldsNameMap.get(OppAttribute) != null){
SetOfOppFieldsToFillwiththisvalue = SourceFieldNameTargetFieldsNameMap.get(OppAttribute);
system.debug(LoggingLevel.INFO, ‘This is debug: SetOfOppFieldsToFillwiththisvalue is: ‘+SetOfOppFieldsToFillwiththisvalue);
for(String TargetName :SetOfOppFieldsToFillwiththisvalue){
try{
integer maxlengthofstring = 250;
if(TargetFieldName_MaxLengthMap.get(TargetName) != null){
maxlengthofstring = TargetFieldName_MaxLengthMap.get(TargetName);
if(FinalValue.length()>maxlengthofstring){
FinalValue = FinalValue.substring(0, maxlengthofstring);
}
}
system.debug(LoggingLevel.INFO, ‘This is debug: about to try to set targetname and finalvalue to: ‘+TargetName+’ and ‘+ FinalValue);
p.put(TargetName, FinalValue);
} catch (exception e){
//updateThisOpp = false;
system.debug(LoggingLevel.INFO, ‘This is debug: there was a problem setting a field value: ‘+e);
system.debug(LoggingLevel.INFO, ‘This is debug: had an error trying to set targetname and finalvalue to: ‘+TargetName+’ and ‘+ FinalValue);

}

}

}

//}

}//end of looping thru consolidated values from source fields
if(updateThisOpp == true){
system.debug(LoggingLevel.INFO, ‘This is debug: p is ‘+p);
OppsToUpdate.add(p);
set ThisSet = New Set();
if(MR_OLI.MROppSummTrafficController.OppIdsAlreadyProcessed != null){
ThisSet = MR_OLI.MROppSummTrafficController.OppIdsAlreadyProcessed;
}
ThisSet.add(p.id);
MR_OLI.MROppSummTrafficController.OppIdsAlreadyProcessed = ThisSet;
}
}//end of OppIdFor-Loop

if(OppsToUpdate.size()>0){
try{
MR_OLI.MROppSummTrafficController.MRRunningDMLStatements = true;
update OppsToUpdate;
MR_OLI.MROppSummTrafficController.MRRunningDMLStatements = false;
} catch (Exception e) {
system.debug(LoggingLevel.INFO, ‘This is debug: Exception thrown: ‘+e);
}

}//end updateopps

}//end of method

}

 

 

 

 

 

 

 

global class AppUtilities {

//togo in traffic controller
//global Static Boolean OppLineItemAft_DelInsUpd_Running = false;

global static string OppLineItemFieldQueryString = ‘ Id, ProductCode, OpportunityId’;

global static string OppFieldQueryString = ‘ Id’;

global static string mdtLineToParentQueryString = ‘SELECT MR_OLI__Line_Source_Field__c, MR_OLI__Parent_Target_Field__c, MR_OLI__Max_Length_of_Target_String_Field__c, MR_OLI__Source_Field_Type__c FROM MR_OLI__MR_Line_to_Parent_Map__mdt WHERE MR_OLI__Line_Source_Field__c != null and MR_OLI__Parent_Target_Field__c != null’;

global static string csLineToParentQueryString = ‘SELECT MR_OLI__Line_Source_Field__c, MR_OLI__Parent_Target_Field__c, MR_OLI__Max_Length_of_Target_String_Field__c, MR_OLI__Source_Field_Type__c FROM MR_OLI__Line_to_Parent_Map__c WHERE MR_OLI__Line_Source_Field__c != null and MR_OLI__Parent_Target_Field__c != null’;

//global static list<MR_OLI__MR_Line_to_Parent_Map__mdt> MRLineToParentMDTList;

global static list< MR_OLI__Line_to_Parent_Map__c> MRLineToParentCSList;

global static map<string, map<string, OpportunityLineItem>> OppIdLineItemIdsBeingProcessed;

global static map<string, map<string, string>> OppIdStringIdentifierStringValue;

}

 

 

global class MROppSummTrafficController {

global static boolean MRTestsSettingUpData = false;

global static boolean MRRunningDMLStatements = false;

global static boolean MRTestsRunning = false;

global static boolean MRTestsRunningEmulateMdt = false;

global static boolean OppLineItemAft_DelInsUpd_Running = false;

global static set<string> OppIdsAlreadyProcessed;

}

 

 

 

 

//////////

 

 

public class OppIdLineSummarizerAtFuture {
@future

public static void SubmitIdsToLineSummarizer(Set<String> OppIds) {
MR_OLI.OppIdLineSummarizer.SummarizeTheseOppIds(OppIds);
}

}

///////////

 

 

@isTest

public class TestMROLI{

private static testMethod void Test_MROLI_01_NoLines() {

MR_OLI.MROppSummTrafficController.MRTestsRunning = true;

MR_OLI.MROppSummTrafficController.MRTestsSettingUpData = true;

MR_OLI.TestMROLIUtils t = new MR_OLI.TestMROLIUtils();
t.CreateAcctw3Opps(false);

Set<String> OppIdsToTest = New Set<String>();

for(OpportunityLineItem oli: [SELECT Id, OpportunityId FROM OpportunityLineItem WHERE OpportunityId != null]){
OppIdsToTest.add(oli.OpportunityId);
}

MR_OLI.MROppSummTrafficController.MRTestsSettingUpData = false;

Test.startTest();

MR_OLI.OppIdLineSummarizer.SummarizeTheseOppIds(OppIdsToTest);

Test.StopTest();

}// end of test

private static testMethod void Test_MROLI_02_wLines_NoCustomSetting() {

MR_OLI.MROppSummTrafficController.MRTestsRunning = true;

MR_OLI.MROppSummTrafficController.MRTestsSettingUpData = true;

MR_OLI.TestMROLIUtils t = new MR_OLI.TestMROLIUtils();
t.CreateAcctw3Opps(true);

Set<String> OppIdsToTest = New Set<String>();

for(OpportunityLineItem oli: [SELECT Id, OpportunityId FROM OpportunityLineItem WHERE OpportunityId != null]){
OppIdsToTest.add(oli.OpportunityId);
}

MR_OLI.MROppSummTrafficController.MRTestsSettingUpData = false;

Test.startTest();

MR_OLI.OppIdLineSummarizer.SummarizeTheseOppIds(OppIdsToTest);

Test.StopTest();

}//end of test

private static testMethod void Test_MROLI_03_wLines_withCustomSetting() {

MR_OLI.MROppSummTrafficController.MRTestsRunning = true;

//MR_OLI.MROppSummTrafficController.MRTestsRunningEmulateMdt = true;

MR_OLI.MROppSummTrafficController.MRTestsSettingUpData = true;

MR_OLI.TestMROLIUtils t = new MR_OLI.TestMROLIUtils();
t.CreateAcctw3Opps(true);
t.InsertLineToParentFieldMap();

//t.Setting1.MR_OLI__Run_all_Opps_Thru_Update__c = true;
//update t.setting1;

Set<String> OppIdsToTest = New Set<String>();
List<OpportunityLineItem> Olis = new List<OpportunityLineItem>();

MR_OLI__Line_to_Parent_Map__c CSLineToParentMap3 = new MR_OLI__Line_to_Parent_Map__c();

CSLineToParentMap3.MR_OLI__Line_Source_Field__c = ‘Description’;
CSLineToParentMap3.MR_OLI__Source_Field_Type__c = ‘string’;
CSLineToParentMap3.MR_OLI__Parent_Target_Field__c = ‘LeadSource’;
CSLineToParentMap3.MR_OLI__Max_Length_of_Target_String_Field__c = 250;
CSLineToParentMap3.Name = ‘Description_LeadSource’;
insert CSLineToParentMap3;

for(OpportunityLineItem oli: [SELECT Id, ProductCode, Product2Id, OpportunityId FROM OpportunityLineItem WHERE OpportunityId != null]){
if(oli.ProductCode.contains(’04’)){
oli.Description = ‘putting a new value here from null’;
}
if(oli.ProductCode.contains(’02’)){
oli.Description = ‘another value’;
}
olis.add(oli);
OppIdsToTest.add(oli.OpportunityId);

}

MR_OLI.MROppSummTrafficController.MRTestsSettingUpData = false;

Test.startTest();

//MR_OLI.OppIdLineSummarizer.SummarizeTheseOppIds(OppIdsToTest);

update olis;

Test.StopTest();

}//end of test

private static testMethod void Test_MROLI_04_wLines_NoCustomSettingAndFieldsDontExist() {

MR_OLI.MROppSummTrafficController.MRTestsRunning = true;

MR_OLI.MROppSummTrafficController.MRTestsRunningEmulateMdt = true;

MR_OLI.MROppSummTrafficController.MRTestsSettingUpData = true;

MR_OLI.TestMROLIUtils t = new MR_OLI.TestMROLIUtils();
t.CreateAcctw3Opps(true);
//t.InsertLineToParentFieldMap();

Set<String> OppIdsToTest = New Set<String>();

for(OpportunityLineItem oli: [SELECT Id, ProductCode, Product2Id, OpportunityId FROM OpportunityLineItem WHERE OpportunityId != null]){
OppIdsToTest.add(oli.OpportunityId);
}

MR_OLI.MROppSummTrafficController.MRTestsSettingUpData = false;

Test.startTest();

MR_OLI.OppIdLineSummarizer.SummarizeTheseOppIds(OppIdsToTest);

Test.StopTest();

}//end of test

}

 

 

 

 

 

 

/////////

 

@IsTest

/* //////the purpose of this test class is to
be used to setup all other test classes so they don’t need to
build the test data directly.
This class is tested vicariously through other test classes that call
this class. //////*/

global class TestMROLIUtils {

global Account ac1;
global opportunity op1;
global Opportunity op2;
global Opportunity op3;

global MR_OLI__Line_to_Parent_Map__c CSLineToParentMap1;
global MR_OLI__Line_to_Parent_Map__c CSLineToParentMap2;

//global PriceBook2 PB;

//global PricebookEntry PBE1;

global MR_OLI__App_Settings__c Setting1;

global void InsertLineToParentFieldMap(){

list<MR_OLI__Line_to_Parent_Map__c> LtoPMapToInsert = new list<MR_OLI__Line_to_Parent_Map__c>();

CSLineToParentMap1 = new MR_OLI__Line_to_Parent_Map__c();
CSLineToParentMap2 = new MR_OLI__Line_to_Parent_Map__c();

CSLineToParentMap1.MR_OLI__Line_Source_Field__c = ‘ProductCode’;
CSLineToParentMap1.MR_OLI__Source_Field_Type__c = ‘string’;
CSLineToParentMap1.MR_OLI__Parent_Target_Field__c = ‘NextStep’;
CSLineToParentMap1.MR_OLI__Max_Length_of_Target_String_Field__c = 250;
CSLineToParentMap1.Name = ‘ProductCode_NextStep’;

CSLineToParentMap2.MR_OLI__Line_Source_Field__c = ‘ProductCode’;
CSLineToParentMap2.MR_OLI__Source_Field_Type__c = ‘string’;
CSLineToParentMap2.MR_OLI__Parent_Target_Field__c = ‘Description’;
CSLineToParentMap2.MR_OLI__Max_Length_of_Target_String_Field__c = 32000;
CSLineToParentMap2.Name = ‘ProductCode_Description’;

LtoPMapToInsert.add(CSLineToParentMap1);
LtoPMapToInsert.add(CSLineToParentMap2);

insert LtoPMapToInsert;

return;

}

global void CreateAcctw3Opps(boolean AddProducts){

Setting1 = new MR_OLI__App_Settings__c();

Setting1.MR_OLI__Disable_OLI_Trigger__c = false;
Setting1.MR_OLI__Run_all_Opps_Thru_Update__c = false;
Setting1.MR_OLI__Activate_Application__c = true;

insert Setting1;

ac1 = new account();
Ac1.Name = ‘New Test Account’;
// Ac.Industry = ‘Healthcare’;
// Ac.Description = ‘Great place’;
insert ac1;

op1 = new Opportunity();
op1.Name = ‘Rings’;
op1.CloseDate = System.today().addDays(15);
op1.Stagename = ‘Competing’;
op1.AccountId = ac1.Id;
op1.Amount = 100000;
insert op1;

op2 = new Opportunity();
op2.Name = ‘Won Rings’;
op2.CloseDate = System.today().addDays(15);
op2.Stagename = ‘Closed Won’;
op2.AccountId = ac1.Id;
op2.Amount = 100000;
insert op2;

op3 = new Opportunity();
op3.Name = ‘Rings Lost’;
op3.CloseDate = System.today().addDays(15);
op3.Stagename = ‘Closed Lost’;
op3.AccountId = ac1.Id;
op3.Amount = 100000;
insert op3;

if(AddProducts == true){

list<Product2> Products = New List<Product2>();
list<PricebookEntry> PBEs = New List<PriceBookEntry>();
list<OpportunityLineItem> Olis = New List<OpportunityLineItem>();
list<OpportunityLineItem> OlisToInsert = New List<OpportunityLineItem>();

//querythePricebookid for test
Pricebook2 PB = new Pricebook2(
Id = Test.getStandardPricebookId(),
IsActive = true
);

/*
PriceBook2 PB = new PriceBook2();
PB.Description = ‘This is Pricebook2 PB record’;
PB.IsActive = true;
PB.Name = ‘PB Name’;
insert PB;
*/
//weird workaround that sets this as the standard for test
update PB;

List<Pricebook2> PriceBooks = New List<Pricebook2>();

string PricebookIdToUse = ”;

for(Pricebook2 pb2: [select id from Pricebook2 where isStandard=true LIMIT 1]){
PriceBooks.add(pb2);
PricebookIdToUse = pb2.id;
}

system.debug(LoggingLevel.INFO, ‘This is debug: PriceBooks is: ‘+PriceBooks);
system.debug(LoggingLevel.INFO, ‘This is debug: The PricebookIdToUse is: ‘+PricebookIdToUse);

for(Integer i = 0; i < 5; i++) {
Product2 p = new Product2();
p.IsActive = true;
p.Name = ‘Product 0’+i;
p.ProductCode = ‘SKU0’+i;
//p.DefaultPrice = 10;
Products.add(p);
}
insert Products;

for(Product2 p2: [SELECT Id, IsActive, Name, ProductCode FROM Product2]){
PriceBookEntry pbe = new PriceBookEntry();
pbe.Pricebook2Id = PricebookIdToUse;
//pbe.Name = p2.Name;
pbe.IsActive = true;
pbe.Product2Id = p2.id;
pbe.UnitPrice = 10;
//pbe.UseStandardPrice = true;
PBEs.add(pbe);
}
insert PBEs;

for(PriceBookEntry pbe: [SELECT Id, Pricebook2Id, ProductCode FROM PriceBookEntry]){
OpportunityLineItem oli = new OpportunityLineItem();
oli.OpportunityId = op1.id;
oli.PricebookEntryId = pbe.id;
oli.Quantity = 5;
oli.UnitPrice = 10;
OlisToInsert.add(oli);
if(pbe.productCode == ‘SKU02’ || pbe.productCode == ‘SKU03’){
OpportunityLineItem oli2 = new OpportunityLineItem();
oli2.OpportunityId = op2.id;
oli2.PricebookEntryId = pbe.id;
oli2.Quantity = 5;
oli2.UnitPrice = 10;
OlisToInsert.add(oli2);
}
if(pbe.ProductCode == ‘SKU04’ || pbe.ProductCode == ‘SKU00’){
OpportunityLineItem oli3 = new OpportunityLineItem();
oli3.OpportunityId = op3.id;
oli3.PricebookEntryId = pbe.id;
oli3.Quantity = 5;
oli3.UnitPrice = 10;
OlisToInsert.add(oli3);

OpportunityLineItem oli4 = new OpportunityLineItem();
oli4.OpportunityId = op3.id;
oli4.PricebookEntryId = pbe.id;
oli4.Quantity = 5;
oli4.UnitPrice = 10;
OlisToInsert.add(oli4);
}

}

insert OlisToInsert;

}

return;

// List<Camp_Inf__CampaignInfluenceSetting__c> Settings = [SELECT id, Name
// FROM Camp_Inf__CampaignInfluenceSetting__c
// ORDER BY CreatedDate asc
// LIMIT 1];

//now the system asserts

//System.assertEquals(1, Settings.size());

}

}