• Tom Ford 15
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies
Hi,
How can i exit the below Loops? 
For each insertion we need to loop through and assign the line item schedule record ID to each insertion (and vice versa). The code is working but i'm getting errors `Too many SOQL queries: 101` when users try to attibute a large number of insertions. 
The reason for this is to create a backend relationship between an insertion and line item schedule; so if an insertion record is delted so is the associated scheudle.

User-added image
Hi,
I've inherited a Sales Cloud org, that until recently had the following trigger working. Now the Original Total Cost is no longer populated at the trigger points.
However the Error at deletion is still being sucessfuly fired.
 
trigger OLITrigger on OpportunityLineItem (before insert, before update, before delete) {
    
 //Code block to enable bypassing of triggers using field off users record  
    String currentUserId = UserInfo.getUserId();
    User currentUser = [SELECT Bypass_Triggers__c FROM User WHERE Id = :currentUserId LIMIT 1];
    if (currentUser.Bypass_Triggers__c) return;
        
    If(System.Trigger.isInsert){
    
      For(OpportunityLineItem oli: Trigger.New){
    
        oli.Original_Total_Cost__c = oli.TotalPrice;
        
      }
  }
    
  If(System.Trigger.isUpdate){

       For(OpportunityLineItem oli : [SELECT TotalPrice,Original_Total_Cost__c,Opportunity.StageName FROM OpportunityLineItem Where Id IN: Trigger.New]){
           
      if(oli.Opportunity.StageName == 'Closed Won')
     {
         //Do Nothing
     }
           
      else  if(Trigger.oldmap.get(oli.id).TotalPrice == Trigger.newmap.get(oli.id).TotalPrice)
           {
               // Do nothing  
            }
                  
     else if(Trigger.oldmap.get(oli.id).TotalPrice != Trigger.newmap.get(oli.id).TotalPrice)
     {
          oli.Original_Total_Cost__c = Trigger.oldmap.get(oli.id).TotalPrice;
     }
       }
  }
       
  If(System.Trigger.isBefore &&System.Trigger.isDelete){
    
  For(OpportunityLineItem oli : Trigger.old)
    {
        if(oli.number_of_invoice_lines__c == 0 || oli.number_of_invoice_lines__c == null){

        list<Line_Item_Details__c> LineItemDetailsId=[Select Id FROM Line_Item_Details__c Where id =:oli.Line_Item_Details__c];     
        delete LineItemDetailsId;
        }
        
        else if(oli.number_of_invoice_lines__c >= 1) 
        {
            oli.addError('You can not delete this Product as it has Invoiced Schedules');
        }
       
    }
    }

}

 

Hi, I'm new to APEX and getting a little out of my depth. I have a few triggers that essentially prevent users from deleting records when they match critera. I've written the test class below and have 91% coverage in Sandbox. 

When i try to deploy into Production i get this error:

Code Coverage Failure
Your code coverage is 25%. You need at least 75% coverage to complete this deployment.

I'm aware it could be down to bad coding but Can anyone help?

@istest
public class TriggerTests{
@istest static void testOLITrigger() {
    System.Test.startTest();
Date ddate = Date.Today();
Account a2 = new Account(Name ='Test Account');
insert a2;
// Get the Standard PriceBook Id.
Id pricebookId = Test.getStandardPricebookId();       
Opportunity opp = new Opportunity(
    Name='Testing Opp',                            
    AccountId= a2.Id,
    StageName = 'Discovery',
    Type = 'New Business', 
    CurrencyIsoCode = 'USD',
    Region__c = 'New York',
    CloseDate = ddate);
insert opp;      
//Create your product
Product2 prod = new Product2(
     Name = 'TW Print',
    Family = 'The Week',
     Brand__c = 'The Week',
Ad_Type__c  = 'Display Advertising', 
    Media_Type__c = 'Print',
     ProductCode = 'TWP',
     isActive = true,
    CanUseRevenueSchedule = true
);
insert prod;
//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
     Pricebook2Id = pricebookId,
     Product2Id = prod.Id,
     UnitPrice = 100.00,
     IsActive = true
);
insert pbEntry;
    //Create Line Item Details
Line_Item_Details__c LID = new Line_Item_Details__c(
    CurrencyIsoCode = 'USD',
    Print_Ad_Size__c = '1 Full Page',
    Quantity__c = 5,
    Rate__c = 123,
    Total_Net_Cost__c = 123*5,
    OppProductInvoiced__c = False
);
insert LID;       
//Create Opportunity Line Item. 
OpportunityLineItem oli = new OpportunityLineItem(
     OpportunityId = opp.Id,
     UnitPrice = 123,
     Quantity = 5,
     PricebookEntryId = pbEntry.Id,
    ServiceDate = ddate,
    Digital_End_Date__c = ddate +7,     
    Print_Frequency__c  = 'Consecutive Issues',
    Line_Item_Details__c = LID.id,
    Original_Total_Cost__c = 123*5,
    number_of_invoice_lines__c = 1
);
insert oli;  
//Create Test Issue
Issue__c Iss = new Issue__c(
    Name = 'Test Issue',
    Brand__c = 'The Week',
    Product_Code__c = 'TWM',
    On_Sale_Date__c = ddate - 7,
    Issue_counter__c = 2020001    
);
insert Iss;
//Create Insertion
Insertion__c Ins = new Insertion__c(
    Issue_lookup__c = Iss.id,
    Opportunity__c = opp.id,
    Line_Item_Details__C = LID.id
);
insert Ins;
//Create Opportunity Line Item Schedule. 
OpportunityLineItemSchedule olis = new OpportunityLineItemSchedule (
    OpportunityLineItemId = oli.id,
    ScheduleDate = ddate,
    Revenue =  123,
    Type = 'Revenue',
    Invoiced__c = TRUE
);
insert olis;   
  Try{
        olis.Revenue = 1234;
        olis.ScheduleDate = ddate + 2;
        update olis;       
    }
    catch(exception e){
              system.assert(e.getMessage().contains('You can not Amend this Schedule as it has been Invoiced'));                
}
    Try{
        delete olis;
    }
      catch(exception e){
                system.assert(e.getMessage().contains('You can not Delete this Schedule as it has been Invoiced'));                
}
    Try{
        delete oli;
    }
    catch(exception e){
                system.assert(e.getMessage().contains('You can not delete this Product as it has Invoiced Schedules'));  
    }
    Try{
        delete Ins;
    }
    catch(exception e){
                system.assert(e.getMessage().contains('You can not delete this Insertion as it has Closed'));  
    }          
System.Test.stopTest() ; 
}

//Test For Line Item Details Trigger    

@isTest static void testLIDTrigger() {
System.Test.startTest();
    //Create Line Item Details
    Line_Item_Details__c LID = new Line_Item_Details__c(
    CurrencyIsoCode = 'USD',
    Print_Ad_Size__c = '1 Full Page',
    Quantity__c = 5,
    Rate__c = 123,
    Total_Net_Cost__c = 123*5,
    OppProductInvoiced__c = TRUE
);
insert LID;
    
      try
    {
    delete LID;
        }
    catch(Exception e)
    { 
}
}
}

 

 

 

Hi,
How can i exit the below Loops? 
For each insertion we need to loop through and assign the line item schedule record ID to each insertion (and vice versa). The code is working but i'm getting errors `Too many SOQL queries: 101` when users try to attibute a large number of insertions. 
The reason for this is to create a backend relationship between an insertion and line item schedule; so if an insertion record is delted so is the associated scheudle.

User-added image
Hi,
I've inherited a Sales Cloud org, that until recently had the following trigger working. Now the Original Total Cost is no longer populated at the trigger points.
However the Error at deletion is still being sucessfuly fired.
 
trigger OLITrigger on OpportunityLineItem (before insert, before update, before delete) {
    
 //Code block to enable bypassing of triggers using field off users record  
    String currentUserId = UserInfo.getUserId();
    User currentUser = [SELECT Bypass_Triggers__c FROM User WHERE Id = :currentUserId LIMIT 1];
    if (currentUser.Bypass_Triggers__c) return;
        
    If(System.Trigger.isInsert){
    
      For(OpportunityLineItem oli: Trigger.New){
    
        oli.Original_Total_Cost__c = oli.TotalPrice;
        
      }
  }
    
  If(System.Trigger.isUpdate){

       For(OpportunityLineItem oli : [SELECT TotalPrice,Original_Total_Cost__c,Opportunity.StageName FROM OpportunityLineItem Where Id IN: Trigger.New]){
           
      if(oli.Opportunity.StageName == 'Closed Won')
     {
         //Do Nothing
     }
           
      else  if(Trigger.oldmap.get(oli.id).TotalPrice == Trigger.newmap.get(oli.id).TotalPrice)
           {
               // Do nothing  
            }
                  
     else if(Trigger.oldmap.get(oli.id).TotalPrice != Trigger.newmap.get(oli.id).TotalPrice)
     {
          oli.Original_Total_Cost__c = Trigger.oldmap.get(oli.id).TotalPrice;
     }
       }
  }
       
  If(System.Trigger.isBefore &&System.Trigger.isDelete){
    
  For(OpportunityLineItem oli : Trigger.old)
    {
        if(oli.number_of_invoice_lines__c == 0 || oli.number_of_invoice_lines__c == null){

        list<Line_Item_Details__c> LineItemDetailsId=[Select Id FROM Line_Item_Details__c Where id =:oli.Line_Item_Details__c];     
        delete LineItemDetailsId;
        }
        
        else if(oli.number_of_invoice_lines__c >= 1) 
        {
            oli.addError('You can not delete this Product as it has Invoiced Schedules');
        }
       
    }
    }

}

 

Hi, I'm new to APEX and getting a little out of my depth. I have a few triggers that essentially prevent users from deleting records when they match critera. I've written the test class below and have 91% coverage in Sandbox. 

When i try to deploy into Production i get this error:

Code Coverage Failure
Your code coverage is 25%. You need at least 75% coverage to complete this deployment.

I'm aware it could be down to bad coding but Can anyone help?

@istest
public class TriggerTests{
@istest static void testOLITrigger() {
    System.Test.startTest();
Date ddate = Date.Today();
Account a2 = new Account(Name ='Test Account');
insert a2;
// Get the Standard PriceBook Id.
Id pricebookId = Test.getStandardPricebookId();       
Opportunity opp = new Opportunity(
    Name='Testing Opp',                            
    AccountId= a2.Id,
    StageName = 'Discovery',
    Type = 'New Business', 
    CurrencyIsoCode = 'USD',
    Region__c = 'New York',
    CloseDate = ddate);
insert opp;      
//Create your product
Product2 prod = new Product2(
     Name = 'TW Print',
    Family = 'The Week',
     Brand__c = 'The Week',
Ad_Type__c  = 'Display Advertising', 
    Media_Type__c = 'Print',
     ProductCode = 'TWP',
     isActive = true,
    CanUseRevenueSchedule = true
);
insert prod;
//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
     Pricebook2Id = pricebookId,
     Product2Id = prod.Id,
     UnitPrice = 100.00,
     IsActive = true
);
insert pbEntry;
    //Create Line Item Details
Line_Item_Details__c LID = new Line_Item_Details__c(
    CurrencyIsoCode = 'USD',
    Print_Ad_Size__c = '1 Full Page',
    Quantity__c = 5,
    Rate__c = 123,
    Total_Net_Cost__c = 123*5,
    OppProductInvoiced__c = False
);
insert LID;       
//Create Opportunity Line Item. 
OpportunityLineItem oli = new OpportunityLineItem(
     OpportunityId = opp.Id,
     UnitPrice = 123,
     Quantity = 5,
     PricebookEntryId = pbEntry.Id,
    ServiceDate = ddate,
    Digital_End_Date__c = ddate +7,     
    Print_Frequency__c  = 'Consecutive Issues',
    Line_Item_Details__c = LID.id,
    Original_Total_Cost__c = 123*5,
    number_of_invoice_lines__c = 1
);
insert oli;  
//Create Test Issue
Issue__c Iss = new Issue__c(
    Name = 'Test Issue',
    Brand__c = 'The Week',
    Product_Code__c = 'TWM',
    On_Sale_Date__c = ddate - 7,
    Issue_counter__c = 2020001    
);
insert Iss;
//Create Insertion
Insertion__c Ins = new Insertion__c(
    Issue_lookup__c = Iss.id,
    Opportunity__c = opp.id,
    Line_Item_Details__C = LID.id
);
insert Ins;
//Create Opportunity Line Item Schedule. 
OpportunityLineItemSchedule olis = new OpportunityLineItemSchedule (
    OpportunityLineItemId = oli.id,
    ScheduleDate = ddate,
    Revenue =  123,
    Type = 'Revenue',
    Invoiced__c = TRUE
);
insert olis;   
  Try{
        olis.Revenue = 1234;
        olis.ScheduleDate = ddate + 2;
        update olis;       
    }
    catch(exception e){
              system.assert(e.getMessage().contains('You can not Amend this Schedule as it has been Invoiced'));                
}
    Try{
        delete olis;
    }
      catch(exception e){
                system.assert(e.getMessage().contains('You can not Delete this Schedule as it has been Invoiced'));                
}
    Try{
        delete oli;
    }
    catch(exception e){
                system.assert(e.getMessage().contains('You can not delete this Product as it has Invoiced Schedules'));  
    }
    Try{
        delete Ins;
    }
    catch(exception e){
                system.assert(e.getMessage().contains('You can not delete this Insertion as it has Closed'));  
    }          
System.Test.stopTest() ; 
}

//Test For Line Item Details Trigger    

@isTest static void testLIDTrigger() {
System.Test.startTest();
    //Create Line Item Details
    Line_Item_Details__c LID = new Line_Item_Details__c(
    CurrencyIsoCode = 'USD',
    Print_Ad_Size__c = '1 Full Page',
    Quantity__c = 5,
    Rate__c = 123,
    Total_Net_Cost__c = 123*5,
    OppProductInvoiced__c = TRUE
);
insert LID;
    
      try
    {
    delete LID;
        }
    catch(Exception e)
    { 
}
}
}