• kaushik Sabapathy
  • NEWBIE
  • 15 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 1
    Replies
Hi all,
try
        {
           for(OpportunityLineItem opp:oli1) 
                
         {
          OpportunityLineItem Oldopp=opp;
         
           if(Trigger.isUpdate)                  
       
         {
        
         oldOpp = (OpportunityLineItem ) Trigger.oldMap.get(opp.Id);     
         
         }                                       
         if(filterStages.contains(opp.Product_Type__c)&&filterStages.contains(opp.TAV_Category__c)&&filterStages.contains(opp.Interval__c)&&filterStages.contains(opp.Line_Transaction_Type__c)){
         if(opp.Term__c<=12)
         {
         opp.TAV__c=(opp.Monthly_Net_Price__c-oldOpp.Monthly_Net_Price__c)*opp.Term__c;
if(opp.TAV__c<0)
opp.TAV__c=0;
}
         else
         {
         if(opp.Term__c<=12)
         {
         opp.TAV__c=(opp.Monthly_Net_Price__c-oldOpp.Monthly_Net_Price__c)*12;
         if(opp.TAV__c<0)
opp.TAV__c=0;
         }
          }
                   }

The test class covers 77% of the code but  im not able to cover the apex class for the above part. Below is the entire piece of code
Public class c15
{
Static Boolean isCalculated=false;
Public Static void tavcal(OpportunityLineItem[] oli)
{
if(isCalculated==false){
//String result;SELECT Id, Name, (SELECT Id, Name FROM Relationship_Name__r) FROM Parent_Object_API_Name__c
//List<Opportunity> direct2 = [Select Id,Type,(Select Id, Product_Type__c,Line_Transaction_Type__c,Monthly_Net_Price__c,TAV_Category__c,TAV__c,Term__c,Interval__c FROM OpportunityLineItems) From Opportunity];
  Map < String, OpportunityLine__c> customSettingsMap =OpportunityLine__c.getAll();
           String filterStages = customSettingsMap.get('New value').Product_Type__c;
           String secondStages=customSettingsMap.get('second value').Product_Type__c;
           String thirdStages=customSettingsMap.get('third value').Product_Type__c;
          // String fourthStages=customSettingsMap.get('initial criteria').Product_Type__c;
         //  String initialvalue=customSettingsMap.get('initial criteria').Product_Type__c;
//           CustomSettingName__c mc = CustomSettingName__c.getValues(data_set_name);

       System.debug('Filtered Stages == '  + filterStages);
       System.debug('Filtered Stages == '  +secondStages );
       
         

       
       //if(allParentRecords.size() > 0 && allParentRecords.containsKey(record.OpportunityId))
      
        try
        {
        if(Trigger.isUpdate||Trigger.isInsert)
        
        {
         for(OpportunityLineItem opp1:oli)
         {
        // OpportunityLineItem oldOpp = (OpportunityLineItem)Trigger.oldMap.get(opp1.Id);
          
                                       
         if(filterStages.contains(opp1.Product_Type__c)&&filterStages.contains(opp1.TAV_Category__c)&&filterStages.contains(opp1.Interval__c)&&(opp1.Line_Transaction_Type__c==null)){
         if(opp1.Term__c<=12)
         opp1.TAV__c=opp1.Monthly_Net_Price__c*opp1.Term__c;
         else
         opp1.TAV__c=opp1.Monthly_Net_Price__c*12;
                       }
         else  if(secondStages.contains(opp1.Product_Type__c)&&secondStages.contains(opp1.TAV_Category__c)&&(opp1.Interval__c==null))
         {
        
         opp1.TAV__c=opp1.Monthly_Net_Price__c;
         }
         else 
         {
         if(thirdStages.contains(opp1.Product_Type__c)&&thirdStages.contains(opp1.TAV_Category__c)&&thirdStages.contains(opp1.Interval__c)&&opp1.Line_Transaction_Type__c==null){
         if(opp1.Term__c>12)
          opp1.TAV__c=((opp1.Monthly_Net_Price__c *36) / 0.48) * 0.2;
         else 
          opp1.TAV__c=opp1.Monthly_Net_Price__c;
          }
         }
                      
         
         }
         }
              }
         catch (System.NullPointerException e) {

}


         isCalculated = true;
    //  update opp;
       //  listof.add(opp);
        
        
        
        
        
           
       //  update listof; 
         
          
}/* end of for loop*/            
} /*end of if*/    
Public Static void tavcal1(OpportunityLineItem[] oli1)
{
if(isCalculated==false){
//String result;
//List<OpportunityLineItem> direct2=[SELECT Product_Type__c,Line_Transaction_Type__c,Monthly_Net_Price__c,TAV_Category__c,TAV__c,Term__c,Interval__c FROM OpportunityLineItem where Opportunity.Type='Renewal'];
  Map < String, OpportunityLine__c> customSettingsMap =OpportunityLine__c.getAll();
           String filterStages = customSettingsMap.get('Renewal').Product_Type__c;
           String secondStages=customSettingsMap.get('second value').Product_Type__c;
           String thirdStages=customSettingsMap.get('third value').Product_Type__c;
//           CustomSettingName__c mc = CustomSettingName__c.getValues(data_set_name);

       System.debug('Filtered Stages == '  + filterStages);
       System.debug('Filtered Stages == '  +secondStages );
      

       
       //if(allParentRecords.size() > 0 && allParentRecords.containsKey(record.OpportunityId))
       
         
        
         
        try
        {
           for(OpportunityLineItem opp:oli1) 
                
         {
          OpportunityLineItem Oldopp=opp;
         
           if(Trigger.isUpdate)                  
       
         {
        
         oldOpp = (OpportunityLineItem ) Trigger.oldMap.get(opp.Id);     
         }                                       
         if(filterStages.contains(opp.Product_Type__c)&&filterStages.contains(opp.TAV_Category__c)&&filterStages.contains(opp.Interval__c)&&filterStages.contains(opp.Line_Transaction_Type__c)){
         if(opp.Term__c<=12)
         {
         opp.TAV__c=(opp.Monthly_Net_Price__c-oldOpp.Monthly_Net_Price__c)*opp.Term__c;
if(opp.TAV__c<0)
opp.TAV__c=0;
}
         else
         {
         if(opp.Term__c<=12)
         {
         opp.TAV__c=(opp.Monthly_Net_Price__c-oldOpp.Monthly_Net_Price__c)*12;
         if(opp.TAV__c<0)
opp.TAV__c=0;
         }
          }
                   }
                   
                   if(trigger.isInsert||trigger.isUpdate)
                   {
          if(secondStages.contains(opp.Product_Type__c)&&secondStages.contains(opp.TAV_Category__c)&&opp.Interval__c==null)
         {
        
         opp.TAV__c=opp.Monthly_Net_Price__c;
         }
         else 
         {
         if(thirdStages.contains(opp.Product_Type__c)&&thirdStages.contains(opp.TAV_Category__c)&&thirdStages.contains(opp.Interval__c)&&thirdStages.contains(opp.Line_Transaction_Type__c)){
         if(opp.Term__c>=12)
          opp.TAV__c=((opp.Monthly_Net_Price__c *36) / 0.48) * 0.2;
         else 
          opp.TAV__c=opp.Monthly_Net_Price__c;
          }
         }
                      
         }
         }
         
         }
         
         catch (System.NullPointerException e) {

}
         isCalculated = true;
       //  update opp;
           
         
         
         
}
}  

}

 This is the test method I assumed that it may work and modified it several times :-) for the code part which needs to be covered.
 
static testMethod void tavcalTest7() {
       Product2 newProd = new Product2(Name = 'test product', family = 'test family');
insert newProd;
Id pricebookId = Test.getStandardPricebookId();

PriceBookEntry pbEntry = new PriceBookEntry(
    UnitPrice = 300,
    PriceBook2Id = pricebookId,
    Product2Id = newProd.Id,
    IsActive = true);

insert pbEntry ;
Opportunity oppObj = new Opportunity(Name = 'TestOpp',Amount = 2000.00,CloseDate=Date.today(),StageName='Close Won',Type='Renewal');
insert oppObj;

insertcustomsetting();
//List<OpportunityLineItem> listopp=new List<OpportunityLineItem>();

OpportunityLineItem oppLine = new OpportunityLineItem(pricebookentryid=pbEntry.Id,TotalPrice=2000.00,Quantity = 2.00,OpportunityID = oppObj.Id,
Product_Type__c='Subscription',
TAV_Category__c='MRR',
Interval__c ='Monthly',
Monthly_Net_Price__c=300,
Term__c=2,
Line_Transaction_Type__c='No-change');
insert oppLine;



           // oppLine = [SELECT Product_Type__c,TAV_Category__c,Interval__c ,Monthly_Net_Price__c,Term__c,TAV__c FROM OpportunityLineItem WHERE Id =:oppLine.Id];
 //system.assertEquals(checkString, [SELECT Description FROM Account WHERE Id = :acct.Id].Description);
   // system.assertEquals(checkString, [SELECT Description FROM Contact WHERE Id = :c.Id].Description);
Test.starttest();
oppLine = [SELECT Product_Type__c,TAV_Category__c,Interval__c ,Monthly_Net_Price__c,Term__c,TAV__c FROM OpportunityLineItem WHERE Id =:oppLine.Id];
oppLine.Monthly_Net_Price__c=200;
update oppLine;
System.assertEquals(200,[SELECT TAV__c FROM OpportunityLineItem WHERE Id =:oppLine.Id].TAV__c);
Test.stoptest();
}

Please guide me to achieve a better Solution
Thaks,
kaushik
Hi,
Im a beginner in Saleforce. Need help in creating test class  for opportunityLineItem(for inserting values in opportunityLineItem).
I had a scenario in which i have to calculate TAV based on different input. I have created a apex class for it  and it is  working fine.
I have used custom setting in getting the details
​Renewal :Subscription;MRR;Credit;Monthly;Pre-Pay;No-change;Upgrade;Downgrade;Reprice;Product Change
second value:Hardware;Software;Appliance;SW/HW
third value:Support and Maintenance;S&M:Annual
Public Static void tavcal1()
{
if(isCalculated==false){
String result;
List<OpportunityLineItem> direct2=[SELECT Product_Type__c,Line_Transaction_Type__c,Monthly_Net_Price__c,TAV_Category__c,TAV__c,Term__c,Interval__c FROM OpportunityLineItem where Opportunity.Type='Renewal'];
  Map < String, OpportunityLine__c> customSettingsMap =OpportunityLine__c.getAll();
           String filterStages = customSettingsMap.get('Renewal').Product_Type__c;
           String secondStages=customSettingsMap.get('second value').Product_Type__c;
           String thirdStages=customSettingsMap.get('third value').Product_Type__c;
//           CustomSettingName__c mc = CustomSettingName__c.getValues(data_set_name);

       System.debug('Filtered Stages == '  + filterStages);
       System.debug('Filtered Stages == '  +secondStages );
         
         
         for(OpportunityLineItem opp:direct2)
         {
        try
        {
         if(Trigger.isUpdate||Trigger.isInsert)
         {
         OpportunityLineItem oldOpp = (OpportunityLineItem)Trigger.oldMap.get(opp.Id);
          
                                       
         if(filterStages.contains(opp.Product_Type__c)&&filterStages.contains(opp.TAV_Category__c)&&filterStages.contains(opp.Interval__c)&&filterStages.contains(opp.Line_Transaction_Type__c)){
         if(opp.Term__c<=12)
         {
         opp.TAV__C=(opp.Monthly_Net_Price__c-oldOpp.Monthly_Net_Price__c)*opp.Term__C;
if(opp.TAV__C<0)
opp.TAV__C=0;
}
 
         else
         {
         if(opp.Term__c<=12)
         {
         opp.TAV__c=(opp.Monthly_Net_Price__c-oldOpp.Monthly_Net_Price__c)*12;
         if(opp.TAV__C<0)
opp.TAV__C=0;
         }
          }
                       }
         else  if(secondStages.contains(opp.Product_Type__c)&&secondStages.contains(opp.TAV_Category__c)&&(opp.Interval__c==null))
         {
        
         opp.TAV__c=opp.Monthly_Net_Price__c;
         }
         else 
         {
         if(thirdStages.contains(opp.Product_Type__c)&&thirdStages.contains(opp.TAV_Category__c)&&thirdStages.contains(opp.Interval__c)){
         if(opp.Term__c>12)
          opp.TAV__c=((opp.Monthly_Net_Price__c *36) / 0.48) * 0.2;
         else 
          opp.TAV__c=opp.Monthly_Net_Price__c;
          }
         }
                      
         
         }
         
         }
         catch (System.NullPointerException e) {

}
         isCalculated = true;
         update opp;
         }   
         
         
}/* end of for loop*/            
} /*end of if*/    
I dont know how to insert opportunity line item as it was asking product id to be inserted
 i have tried to create a Product id but was not successful
Please suggest to insert a new record on opportunityLineItem



Regards,
Kaushik 
Hi all,
          I want to create records for "OpportunityLineItem".Unlike other standard object,creating records for ooportunityLineItem is quite different.
eg:to create a contact record for an account,I click on the Account record and add related contact(or) I click on the contact tab lookup an account for that contact.
             But for creating opportunityLineItem from opportunity it is different,When i click on add product I cannot see OpportunityLineItem fields which are in pageLayout but not in the detail page of OpportunityLineItem(opportunity Product).
I also tried to create record by defining field value for opportunityLineItem,but I am getting"
Price Book Entry ID: Id Value Of Incorrect Type" this error.
Please help me to create OpportunityLineItem record in whichever method you know.
Hi
Im a new to salesforce. i have some questions regarding single sobject and list of sobject.
in the below code it uses contact cont = c. clone  is it for a single contact ??
And what clone method actually does for the below code
public class AddPrimaryContact implements Queueable
{
    private Contact c;
    private String state;
    public  AddPrimaryContact(Contact c, String state)
    {
        this.c = c;
        this.state = state;
    }
    public void execute(QueueableContext context) 
    {
         List<Account> ListAccount = [SELECT ID, Name ,(Select id,FirstName,LastName from contacts ) FROM ACCOUNT WHERE BillingState = :state LIMIT 200];
         List<Contact> lstContact = new List<Contact>();
         for (Account acc:ListAccount)
         {
                 Contact cont = c.clone(false,false,false,false);
                 cont.AccountId =  acc.id;
                 lstContact.add( cont );
         }
         
         if(lstContact.size() >0 )
         {
             insert lstContact;
         }
             
    }

}
Hi all,
          I want to create records for "OpportunityLineItem".Unlike other standard object,creating records for ooportunityLineItem is quite different.
eg:to create a contact record for an account,I click on the Account record and add related contact(or) I click on the contact tab lookup an account for that contact.
             But for creating opportunityLineItem from opportunity it is different,When i click on add product I cannot see OpportunityLineItem fields which are in pageLayout but not in the detail page of OpportunityLineItem(opportunity Product).
I also tried to create record by defining field value for opportunityLineItem,but I am getting"
Price Book Entry ID: Id Value Of Incorrect Type" this error.
Please help me to create OpportunityLineItem record in whichever method you know.