I am having the trigger and handler class in the quote Line Item object it's work like, the I am having two same named custom field in the OpportunityLineItem and Quote Line item respectively.so that field is to be updated by the condition of when issyncing of the quote of the quoteline item to its opportunity is to be true. that custom field name SyncCheck__c. My trigger:
trigger customSyncHandlerTrigger on QuoteLineItem (after update) {
if (Trigger.isUpdate && Trigger.isAfter) {
CustomSyncHandler.UpdateTrigger(Trigger.New, Trigger.OldMap);
}
}
My handler class:
public class CustomSyncHandler {
public static void UpdateTrigger (List<QuoteLineItem> InsertedQuote, Map<Id,QuoteLineItem> OldInsertedQuoteMap) {
Set<Id> ProductIdset = new Set<Id>();
Set<Id> QuoteIdset = new Set<Id>();
Set<Id> OpportunityIdset = new Set<Id>();
List<QuoteLineItem> QuoteLineItemList = new List<QuoteLineItem>();
List<OpportunityLineItem> OpportunityLineItemList = new List<OpportunityLineItem>();
for (QuoteLineItem RecordQuoteItem: InsertedQuote) {
QuoteLineItem OldQuoteLineItemREC = OldInsertedQuoteMap.get(RecordQuoteItem.Id);
if (OldQuoteLineItemREC.SyncCheck__c != RecordQuoteItem.SyncCheck__c) {
ProductIdset.add(RecordQuoteItem.Product2Id);
QuoteIdset.add(RecordQuoteItem.QuoteId);
}
}
If (QuoteIdset.size()>0) {
QuoteLineItemList = [SELECT Id, QuoteId, Product2Id, SyncCheck__c, Quote.issyncing, Quote.OpportunityId FROM QuoteLineItem WHERE Product2Id IN :ProductIdset AND Quote.issyncing = True ];
}
If (QuoteLineItemList.size() > 0) {
for (QuoteLineItem quoteLineitemvalue: QuoteLineItemList) {
OpportunityIdset.add(quoteLineitemvalue.Quote.OpportunityId);
}
}
List<OpportunityLineItem> OpportunityLineitemvalueList = [SELECT Id, Name, OpportunityId, SyncCheck__c, Product2Id FROM OpportunityLineItem WHERE OpportunityId IN :OpportunityIdset AND Product2Id IN :ProductIdset];
Map<Id, List<OpportunityLineItem>> OpportunityandOppolineitemMap = new Map<Id, List<OpportunityLineItem>>();
for (OpportunityLineItem OpportunityLIRecord : OpportunityLineitemvalueList) {
If (!OpportunityandOppolineitemMap.Containskey(OpportunityLIRecord.Id)) {
OpportunityandOppolineitemMap.put(OpportunityLIRecord.OpportunityId, new List<OpportunityLineItem>());
}
OpportunityandOppolineitemMap.get(OpportunityLIRecord.OpportunityId).add(OpportunityLIRecord);
}
system.debug('@@@ OpportunityandOppolineitemMap value is'+OpportunityandOppolineitemMap);
for (QuoteLineItem QuoteLineItemRecord : QuoteLineItemList) {
if (OpportunityandOppolineitemMap.containsKey(QuoteLineItemRecord.Quote.OpportunityId)) {
for (OpportunityLineItem OpporVar : OpportunityandOppolineitemMap.get(QuoteLineItemRecord.Quote.OpportunityId)) {
if (OpporVar.Product2Id == QuoteLineItemRecord.Product2Id) {
OpporVar.SyncCheck__c = QuoteLineItemRecord.SyncCheck__c;
OpportunityLineItemList.add(OpporVar);
}
}
}
}
if (OpportunityLineItemList.size() > 0) {
Update OpportunityLineItemList;
}
}
}
My test class :
@isTest
public class TestCustomSyncHandlerTrigger {
static testMethod void UpdateCustomSyncHandler() {
Account acc1 = new Account();
acc1.Name = 'test account';
insert acc1;
system.debug('insert acc1 is success');
Opportunity Opp1 = new Opportunity();
Opp1.Name = 'testOpp';
Opp1.AccountId = acc1.Id;
Opp1.StageName = 'Closed Won';
Opp1.CloseDate = system.Today();
insert Opp1;
system.debug('insert opp1 success');
Product2 Pro1 = new Product2();
Pro1.Name = 'SLA: Bronze';
pro1.isActive = True;
Insert pro1;
system.debug('insert pro1 is success');
Pricebook2 pb = new pricebook2();
pb.Name = 'Standard Price Book 2009';
pb.description = 'Price Book 2009 Products';
pb.isActive = True;
insert pb;
system.debug('pb value is'+ pb.Id);
system.debug('insert pricebook2 is success');
Id pricebookId = Test.getStandardPricebookId();
PricebookEntry StandardPriceBookEntry = new PricebookEntry();
StandardPriceBookEntry.Pricebook2Id = pricebookId;
StandardPriceBookEntry.Product2Id = pro1.Id;
StandardPriceBookEntry.UnitPrice = 10000;
StandardPriceBookEntry.IsActive =True;
insert StandardPriceBookEntry;
system.debug('insert StandardPriceBookEntry');
PricebookEntry pbe = new PricebookEntry(pricebook2id=pb.id, product2id=pro1.id, unitprice=10000, isActive = True);
insert pbe;
system.debug('insert pbe is success');
Quote Quo1 = new Quote();
Quo1.OpportunityId = Opp1.Id;
Quo1.Pricebook2Id = pb.Id;
Quo1.Name = 'test Quo1';
insert Quo1;
system.debug('insert Quo1 success');
QuoteLineItem QLI = new QuoteLineItem();
QLI.Product2Id = pro1.Id;
QLI.QuoteId = Quo1.Id;
QLI.PricebookEntryId = Pbe.Id;
QLI.Quantity = 2;
QLI.UnitPrice = 150000.0;
insert QLI;
system.debug('insert QLI is success');
OpportunityLineItem OLI = new OpportunityLineItem();
OLI.UnitPrice = 150000;
OLI.OpportunityId = Opp1.Id;
OLI.PriceBookEntryId = Pbe.Id;
OLI.Quantity = 2;
insert OLI;
system.debug('insert OLI is success');
List<QuoteLineItem> QuoteLIList = [SELECT Id, Quote.issyncing, Quote.OpportunityId FROM QuoteLineItem WHERE Quote.issyncing = True];
for(QuoteLineItem q:QuoteLIList) {
system.assertEquals(Opp1.Id, q.Quote.OpportunityId, 'the value of q.Quote.OpportunityId value is not null');
}
if (QLI.Quote.isSyncing == True ) {
if (QLI.Product2id == OLI.Product2id) {
QLI.SyncCheck__c = 'check';
OLI.SyncCheck__c = QLI.SyncCheck__c;
system.assertEquals (OLI.product2Id, QLI.product2id);
system.assertEquals (OLI.SyncCheck__c, 'check' );
}
}
Update QLI;
}
}
Here my test class only provide the 53% only it's my knowledge in test class so please help me to get the full code coverage of my class and trigger. For answer's thanks in advance.