• Amit Vyas 8
  • NEWBIE
  • 15 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies
Trigger is :
if(Trigger.isInsert){
                CompetitorRecordHandler.CreateRelatedCompetitor(Trigger.new);
            }

Handler class is below:
public static void CreateRelatedCompetitor(List<OpportunityLineItem> newList){
        
        List<Opportunity_Competitor__c> ocList = new List<Opportunity_Competitor__c>();
        
        for(OpportunityLineItem oli : newList){
            Opportunity_Competitor__c OppComp= new Opportunity_Competitor__c();
            
            OppComp.Forecast_Date__c=String.ValueOf(oli.ServiceDate);
            OppComp.Opportunity_Product__c=oli.Id;
            OppComp.Opportunity__c=oli.OpportunityId;
            OppComp.Product__c=oli.Product_Name__c;
            
            ocList.add(OppComp);
        }
        if(!ocList.isEmpty()){
            ocList.sort();
            insert ocList;
        }

I want to Sort my "ocList" on "Forecast_Date__c" basis. Can anyone help me?
I have written a trigger to delete my custom object records which have OLI lookup field for OpportunityLineItems once LineItem records are deleted.

trigger CompetitorRecord on OpportunityLineItem(after delete){
if(Trigger.isdelete){
                CompetitorRecordHandler.DeleteOpportunityCompetitor(Trigger.old);
            }
}

My handler class is:
public static void DeleteOpportunityCompetitor(List<OpportunityLineItem> newList){
        
         List<id> listIds = new List<id>(); 
        
        for(opportunityLineItem oli : newList){
            listIds.Add(oli.id);
        }
        system.debug('listIds'+listIds);
        List<Opportunity_Competitor__c> ocList = new List<Opportunity_Competitor__c>([SELECT id FROM Opportunity_Competitor__c WHERE Opportunity_Product__c IN :listIds]);
        if(!ocList.isEmpty()){
            delete ocList;
        }
    }

But the trigger is not working can someone help me to know if I'm missing something.
Can anyone help me with the Test class for the batch class(to delete records which are 180 days old) I have written, I'm getting 100% code coverage in sandbox but when I'm deploying the code in production and validating using "run specified test - giving the name of my test class"  I'm getting Error as "Your Code coverage is 71%, you need at least 75% coverage to complete this deployment" 

Here is my batch class and test class:

global class DeleteOpportunityProductSnapshotBatch implements Database.Batchable<sObject>, Schedulable    
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        return Database.getQueryLocator([SELECT Id FROM OpportunityLineItem_Snapshot__c WHERE CreatedDate != LAST_N_Days:180]);
    }
    global void execute(Database.BatchableContext BC, List<SObject> records)
    {
    delete records;
    }
    global void finish(Database.BatchableContext BC) { }
    global void execute(SchedulableContext ctx) 
    {
        Database.executeBatch(this);
    }
}

Test Class:
@isTest
public class DeleteOppProductSnapshotBatch_Test 
{
    public static testMethod void testMethod1()
    {
        OpportunityLineItem_Snapshot__c Ols = new OpportunityLineItem_Snapshot__c();
        Ols.Name = 'Test';
        Ols.CreatedDate = Date.today().addDays(-180);
        insert Ols;
        
        Test.startTest();
        DeleteOpportunityProductSnapshotBatch Dop = new DeleteOpportunityProductSnapshotBatch();
        id batchprocessid = Database.executeBatch(Dop);
        
        DeleteOpportunityProductSnapshotBatch sh1 = new DeleteOpportunityProductSnapshotBatch();
        String sch = '0 0 2 * * ?';
        system.schedule('Test Territory Check', sch, sh1);

        Test.stopTest();
        
        System.assert(batchprocessid != null);
    }
Trigger is :
if(Trigger.isInsert){
                CompetitorRecordHandler.CreateRelatedCompetitor(Trigger.new);
            }

Handler class is below:
public static void CreateRelatedCompetitor(List<OpportunityLineItem> newList){
        
        List<Opportunity_Competitor__c> ocList = new List<Opportunity_Competitor__c>();
        
        for(OpportunityLineItem oli : newList){
            Opportunity_Competitor__c OppComp= new Opportunity_Competitor__c();
            
            OppComp.Forecast_Date__c=String.ValueOf(oli.ServiceDate);
            OppComp.Opportunity_Product__c=oli.Id;
            OppComp.Opportunity__c=oli.OpportunityId;
            OppComp.Product__c=oli.Product_Name__c;
            
            ocList.add(OppComp);
        }
        if(!ocList.isEmpty()){
            ocList.sort();
            insert ocList;
        }

I want to Sort my "ocList" on "Forecast_Date__c" basis. Can anyone help me?
I have written a trigger to delete my custom object records which have OLI lookup field for OpportunityLineItems once LineItem records are deleted.

trigger CompetitorRecord on OpportunityLineItem(after delete){
if(Trigger.isdelete){
                CompetitorRecordHandler.DeleteOpportunityCompetitor(Trigger.old);
            }
}

My handler class is:
public static void DeleteOpportunityCompetitor(List<OpportunityLineItem> newList){
        
         List<id> listIds = new List<id>(); 
        
        for(opportunityLineItem oli : newList){
            listIds.Add(oli.id);
        }
        system.debug('listIds'+listIds);
        List<Opportunity_Competitor__c> ocList = new List<Opportunity_Competitor__c>([SELECT id FROM Opportunity_Competitor__c WHERE Opportunity_Product__c IN :listIds]);
        if(!ocList.isEmpty()){
            delete ocList;
        }
    }

But the trigger is not working can someone help me to know if I'm missing something.