Krow Task Trigger and Test Class
Krow Task Trigger and Test Class
trigger KrowTaskTrigger1 on Krow__Task__c (before Insert) {//,
if(Trigger.isBefore && Trigger.isInsert){
Set<Id> projectIds = new Set<Id>();
Set<Id> tempprojectIds = new Set<Id>();
Map<Id,Krow__Task__c> templateProjectMap = new Map<Id,Krow__Task__c>();
for(Krow__Task__c kt : Trigger.new){
if(kt.Krow__Parent_Task__c == NULL && kt.Krow__Template_Task__c != NULL && kt.Krow__Template_Project__c == False){
templateProjectMap.put(kt.Krow__Template_Task__c,null);
tempprojectIds.add(kt.Krow__Project__c);
}
}
if(!templateProjectMap.isEmpty()){
Map<Id,Krow__Project__c> krowForOppMap = new Map<Id,Krow__Project__c>([select id,Opportunity__c,Project_Contact__c,(select id,Quote_Line__c from Krow__Tasks__r where Quote_Line__c <> null) from Krow__Project__c where id in : tempprojectIds]);
Set<Id> oppIds = new Set<Id>();
Set<Id> processedLineItems = new Set<Id>();
Set<Id> getKrTskId = new Set<Id>();
for(Krow__Project__c proRec : krowForOppMap.values()){
oppIds.add(proRec.Opportunity__c);
if(proRec.Krow__Tasks__r != null){
for(Krow__Task__c taskRec : proRec.Krow__Tasks__r){
processedLineItems.add(taskRec.Quote_Line__c);
getKrTskId.add(taskRec.Id);
}
}
}
Map<Id,List<Id>> mapOFTemp = new Map<Id,List<Id>>();
if(!tempprojectIds.isEmpty()){
for(Krow__Project__c val:[select id, (Select Id, Klient_Project__c from Klient_Project_ReadOnly__r) from Krow__Project__c WHERE id In :tempprojectIds]){
List<Id> tempPSSET = new List<Id>();
for(Klient_Project_ReadOnly__c psTracker: val.Klient_Project_ReadOnly__r){
tempPSSET.add(psTracker.Id);
}
mapOFTemp.put(val.Id,tempPSSET);
}
}
if(!mapOFTemp.isEmpty()){
for(Krow__Task__c kt : Trigger.new){
if(kt.Krow__Parent_Task__c == NULL && kt.Krow__Template_Task__c != NULL && kt.Krow__Template_Project__c == False){
if(mapOFTemp.containsKey(kt.Krow__Project__c) && mapOFTemp.get(kt.Krow__Project__c)!=null ){
kt.PS_Tracker__c = mapOFTemp.get(kt.Krow__Project__c)[0];
}
}
}
}
templateProjectMap = new Map<Id,Krow__Task__c> ([select Id,Krow__Project__c,Krow__Project__r.Krow__Project_Number__c from Krow__Task__c where Krow__Parent_Task__c = null and id in : templateProjectMap.keySet()]);
for(Krow__Task__c taskRec : templateProjectMap.values()){
projectIds.add(taskRec.Krow__Project__c);
}
Map<String,Set<Id>> newTaskWithProjectNumber = new Map<String,Set<Id>>();
Map<Id,OpportunityLineItem> taskIdwithOpportunityLineItem = new Map<Id,OpportunityLineItem>();
if(projectIds.size()>0){
System.debug('Hello123');
List<Krow__Project__c> projectLst = [select id,Krow__Project_Number__c,Opportunity__c from Krow__Project__c where id in : projectIds];
System.debug('1.2####' + projectLst);
Map<String,Krow__Project__c> projectMap = new Map<String,Krow__Project__c>();
for(Krow__Project__c projectRec : projectLst){
projectMap.put(projectRec.Krow__Project_Number__c,projectRec);
// projectMap.put(projectRec.Id,projectRec);
//oppIds.add(projectRec.Opportunity__c);
}
Map<String,List<OpportunityLineItem>> projectWithOppLines = new Map<String,List<OpportunityLineItem>>();
System.debug('1.2####' + projectMap);
System.debug('1.3####' + oppIds);
System.debug('1.3####' + [SELECT ID,OpportunityId,Product2.Project_Number__c FROM OpportunityLineItem WHERE OpportunityId IN: oppIds AND Product2.Project_Number__c IN: projectMap.keyset()]);
List<OpportunityLineItem> oppLinLst = [SELECT Id,OpportunityId,Product2.Project_Number__c,SBQQ__QuoteLine__c,SBQQ__QuoteLine__r.SBQQ__Quote__r.SBQQ__StartDate__c FROM OpportunityLineItem WHERE OpportunityId IN: oppIds AND Product2.Project_Number__c IN: projectMap.keyset()];
System.debug('1.4####' + oppLinLst);
for(OpportunityLineItem oppLine : oppLinLst){
List<OpportunityLineItem> lines = new List<OpportunityLineItem>();
if(projectWithOppLines.containsKey(oppLine.Product2.Project_Number__c)){
lines = projectWithOppLines.get(oppLine.Product2.Project_Number__c);
}
lines.add(oppLine);
projectWithOppLines.put(oppLine.Product2.Project_Number__c,lines);
}
System.debug('1.5####' + projectWithOppLines);
List<Krow__Task__c> tskUpdateLst = new List<Krow__Task__c>();
for(Krow__Task__c kt : Trigger.new){
if(kt.Krow__Parent_Task__c == null && kt.Krow__Template_Task__c != NULL && kt.Krow__Template_Project__c == False){
System.debug(kt.Id + '####'+kt.Krow__Parent_Task__c + '#####' +kt.Krow__Template_Task__c+'1.11####' + '$$$$' + kt.Krow__Template_Project__c);
if(templateProjectMap.containsKey(kt.Krow__Template_Task__c)){
System.debug(kt.Id + '####'+kt.Krow__Parent_Task__c + '#####' +kt.Krow__Template_Task__c+'1.12####' + '$$$$' + kt.Krow__Template_Project__c);
Krow__Task__c tempTask = templateProjectMap.get(kt.Krow__Template_Task__c);
System.debug('1.6####' + tempTask);
Krow__Project__c krowRec = projectMap.get(tempTask.Krow__Project__r.Krow__Project_Number__c);
if(projectWithOppLines.containsKey(krowRec.Krow__Project_Number__c)){
System.debug('1.7####' + projectWithOppLines);
List<OpportunityLineItem> lines = projectWithOppLines.get(krowRec.Krow__Project_Number__c);
for(OpportunityLineItem oppLine : lines){
if(!processedLineItems.contains(oppLine.SBQQ__QuoteLine__c)){
Krow__Project__c currentProjectRec = krowForOppMap.get(kt.Krow__Project__c);
System.debug('1.8####' + kt.Id);
/*Krow__Task__c ktObj = new Krow__Task__c(id = kt.Id);
ktObj.Quote_Line__c = oppLine.SBQQ__QuoteLine__c;
System.debug('1.9####' + ktObj.Quote_Line__c);
tskUpdateLst.add(ktObj);*/
kt.Quote_Line__c = oppLine.SBQQ__QuoteLine__c;
kt.Krow__Start_Date__c = oppLine.SBQQ__QuoteLine__r.SBQQ__Quote__r.SBQQ__StartDate__c;
kt.Project_Contact__c = currentProjectRec.Project_Contact__c;
processedLineItems.add(oppLine.SBQQ__QuoteLine__c);
break;
}
}
}
}
}
}
System.debug('1.10####' + tskUpdateLst);
if(!tskUpdateLst.isEmpty()){
update tskUpdateLst;
}
}
}
}
}
Test Class
@isTest
private class KrowTaskTrigger1_Test {
@testSetup static void setup() {
Account acc = TestFactory.createSingleAccount('Test ProvisionX', true);
Contact objContact = new Contact();
objContact.AccountId = acc.Id;
objContact.LastName= 'Test Sync2.0';
objContact.LeadSource = 'Event';
objContact.Email = 'test@leadqualification.com';
objContact.MailingCity = 'Florida';
objContact.MailingCountry = 'United States';
objContact.MailingPostalCode = '90263';
objContact.MailingStreet = 'Test 123';
objContact.MailingState = 'CA';
insert objContact;
List<Pricebook2> pbList = new List<Pricebook2>();
Pricebook2 objPriceBook = new Pricebook2(
Name='CPQ PriceBook',Description = 'TesT',isActive=true);
Pricebook2 customPB2 = new Pricebook2(
Name='CPQ MSP PriceBook',Description = 'TesT',isActive=true);
pbList.add(objPriceBook);
pbList.add(customPB2);
insert pbList;
Opportunity objOpp = new Opportunity();
objOpp.Name = 'opp2';
objOpp.AccountId = acc.Id;
objOpp.StageName = 'Qualification';
objOpp.Type = 'Up Sell';
objOpp.Primary_Contact__c = objContact.Id;
objOpp.CloseDate = system.Today();
//objOpp.Pricebook2Id = objPriceBook.Id;
objOpp.Billing_Method__c = 'Ecomm';
insert objOpp;
SBQQ__QuoteLine__c objQuoteLine = new SBQQ__QuoteLine__c();
//objQuoteLine.SBQQ__Quote__c = objQuote.Id;
//objQuoteLine.SBQQ__Product__c = objProduct.Id;
objQuoteLine.SBQQ__Quantity__c = 1;
try{
insert objQuoteLine;
}
catch(DmlException dm){
}
Contact con = new Contact();
con.LastName = 'TTst';
Insert con;
Krow__Project__c kp = new Krow__Project__c();
kp.Krow__Project_Template__c = FALSE;
kp.Krow__Public_Project_Template__c = FALSE;
kp.Krow__Task_Template__c = FALSE;
kp.Status__c = 'On Hold';
kp.Onboarding_Type__c = 'PS/Installation';
kp.Krow__Project_Number__c = '1';
kp.Opportunity__c = objOpp.Id;
kp.Project_Contact__c = con.Id;
Insert kp;
Krow__Task__c kt1 = new Krow__Task__c();
kt1.name = 'Test1';
kt1.Krow__Project__c = kp.Id;
kt1.Krow__Status__c = 'Pending';
kt1.On_Not_Started__c = System.today().addDays(10);
kt1.On_Pending__c = System.today().addDays(10);
// ktFinal.Krow__Status__c = 'Not Started';
Insert kt1;
Product2 objProduct = new Product2();
objProduct.Name = 'G Suite';
objProduct.ProductCode = 'NS-S-CLD-K3-GSUITE-A';
objProduct.IsActive = true;
objProduct.Project_Number__c = '1';
insert objProduct;
Id pricebookId = Test.getStandardPricebookId();
PriceBookentry objPBEntry = new PriceBookentry();
objPBEntry.Pricebook2Id = pbList[0].Id;
//objPBEntry.Pricebook2Id = pricebookId;
objPBEntry.Product2Id = objProduct.Id;
objPBEntry.UnitPrice = 26;
objPBEntry.IsActive = true;
insert objPBEntry;
opportunityLineItem objLine = new OpportunityLineItem();
objLine.OpportunityId = objOpp.Id;
objLine.Product2Id = objProduct.Id;
//objLine.Product2.Project_Number__c = kp.Krow__Project_Number__c;
objLine.PricebookEntryId = objPBEntry.Id;
objLine.Quantity = 1;
objLine.TotalPrice = 99.00;
//objLine.SBQQ__QuoteLine__c = objQuoteLine.id;
insert objLine;
system.debug('objLine-->'+ objLine);
Krow__Task__c ktFinal = new Krow__Task__c();
ktFinal.name = 'Test2';
ktFinal.Krow__Template_Task__c = kt1.Id;
ktFinal.Krow__Project__c = kp.Id;
ktFinal.On_Not_Started__c = System.today().addDays(10);
ktFinal.On_Pending__c = System.today().addDays(10);
ktFinal.Krow__Status__c = 'Not Started';
ktFinal.Krow__Parent_Task__c = NULL;
ktFinal.Quote_Line__c = objQuoteLine.id;
Insert ktFinal;
}
@isTest static void testMethod1() {
List<Krow__Task__c> ktLst = [SELECT name, Krow__Parent_Task__c,Krow__Template_Task__c,Krow__Project__c,Krow__Template_Project__c FROM Krow__Task__c WHERE name = 'Test2' LIMIT 2];
System.debug('ktLst--->'+ ktLst);
}
}
Comments
Post a Comment