• Avinash dhanke 24
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
 public static void populatedLookup(Lead leadobj1){
       Id recordTypeId1 = Schema.Sobjecttype.Lead.getRecordTypeInfosByName().get('IM-Onboarding').getRecordTypeId();
       Id recordTypeId2 = Schema.Sobjecttype.Lead.getRecordTypeInfosByName().get('IM Onboarding-QDE').getRecordTypeId();
      List<Lead> leadList=[Select Id,FirstName,LastName,PAN__c,Stage__c,Status,Client_Type__c,Channel_Partner__c,Channel__c,RecordType.Name FROM Lead Where
                            (RecordType.Name=:recordTypeId1 OR RecordType.Name =:recordTypeId1) And Id =:leadObj1.id];   
            For(lead leadObj:leadList){
                if(leadObj.Channel_Partner__c!=null){
                    leadObj.Channel_Partner__c=leadObj.Channel__c;
                }
            }
        }
                           
Hi All
I am trying to create apex test class for invocable method but i am not getting 75 plus code coverage. the following are my apex code

global with sharing class BoaOpportunityClone {
    
    @InvocableMethod
    global static List<Apex_Result> Opportunityreplicate(List<string> oppId){ 
        Savepoint sp = Database.setSavepoint();
        
        String opportunityId = '';
        String oppRecType = '';
        String accName = '';
        
        List<Apex_Result> cloneStat = new List<Apex_Result>();
        Apex_Result ar = new Apex_Result();
        
        try{
            
            String [] oppDetails = oppId[0].split('-');
            System.debug('oppRecType' + oppDetails[1]);
            System.debug('order' + oppDetails.size());
            
            Opportunity oldOpp = [SELECT Id, RecordType.DeveloperName FROM Opportunity WHERE Id=: oppDetails[0]];
            //purchase to purchase
            if(oldOpp.RecordType.DeveloperName == 'Purchase_Order' && oppDetails[1] == 'Purchase Order'){ // change condition 
                opportunityId = oppDetails[0];
                oppRecType = oppDetails[1];
                accName = oppDetails[2];
                System.debug('purchase');
                Opportunity opp1 = [SELECT AccountId,Id,Amount,BOA_Order__c,Internal_Note__c,CurrencyIsoCode,city__c,CloseDate,Country__c,customer_order_number__c,default_Location__c,Description,ExpectedRevenue,Lead_Time__c,
                                    Name,On_hold__c,Order_Date__c,Order_Placed_By__c,Order_Source__c,OwnerId,Parent_Order__c,Payment_type__c,postal_code__c,Probability,RecordTypeId,reference__c,
                                    StageName,state__c,Street__c,subtotal__c,tax_total__c,TotalOpportunityQuantity,Type FROM Opportunity Where Id=:opportunityId ];  
                
                
                Account acc = [SELECT override_GST_rate__c,Remaining_Credit_Balance__c,Sea_Freight_Lead_Time__c,Air_Freight_Lead_Time__c FROM Account WHERE Id =: opp1.AccountId];
                
                Decimal daysToAddToCloseDate = 0;
                if(opp1.Lead_Time__c == 'Sea Freight Lead Time'){
                    daysToAddToCloseDate = acc.Sea_Freight_Lead_Time__c == null ? 0 : acc.Sea_Freight_Lead_Time__c;
                }
                else if(opp1.Lead_Time__c == 'Air Freight Lead Time'){
                    daysToAddToCloseDate = acc.Air_Freight_Lead_Time__c == null ? 0 : acc.Air_Freight_Lead_Time__c;
                }
                
                ObjectAutoNumber__c t1 = ObjectAutoNumber__c.getInstance('Opportunity');
                
                t1.Purchase_Order_No__c += decimal.valueof('1');
                
                update t1;
                
                Opportunity Oppt1 = new Opportunity();  
                Oppt1.Name=string.valueof(math.round(t1.Purchase_Order_No__c));
                Oppt1.OwnerId = opp1.OwnerId;
                Oppt1.AccountId = opp1.AccountId;
                Oppt1.Order_Date__c = date.today();
                Date cd = opp1.CloseDate;
                cd = cd.addDays(Integer.valueOf(daysToAddToCloseDate));
                Oppt1.CloseDate = cd;
                Oppt1.Lead_Time__c = opp1.Lead_Time__c;
                Oppt1.default_Location__c = opp1.default_Location__c;
                // Oppt1.tax_total__c = 0.00;
                //   Oppt1.Amount = opp1.Amount;
                // Oppt1.subtotal__c = 0.00;
                Oppt1.StageName = 'Prospecting';
                Oppt1.Parent_Order__c = opp1.Id;
                Oppt1.CurrencyIsoCode = opp1.CurrencyIsoCode;
                Oppt1.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Purchase_Order').getRecordTypeId();
                insert Oppt1;
                
                
                
                Account accId = [select id from account where name=:accName]; 
                if(accId.id !=null)
                {
                    oppt1.AccountId = accId.id;
                }
                update oppt1;  // Working
                
                
                // changes
                
                List<Id> prodId = new List<Id>();
                
                Map<Id,Stock_Supplier__c> stockSuppl = new Map<Id,Stock_Supplier__c>();
                
                List<OpportunityLineItem> oliList = [Select Product2Id,PricebookEntryId,UnitPrice,Quantity,Discount,OpportunityId,Description FROM OpportunityLineItem WHERE OpportunityId =: opp1.Id];
                for(OpportunityLineItem oli : oliList){
                    prodId.add(oli.Product2Id);
                }
                
                System.debug('oliList ' + oliList);
                
                // Stock supplier records based on supplier and stock selected
                for(Stock_Supplier__c m : [SELECT Id, Discount_Percentage__c, Pack_Price__c, Supplier_Code__c, Stock_Item__c, Stock_Item__r.EOQ__c FROM Stock_Supplier__c WHERE Stock_Item__c IN : prodId AND Supplier_Name__c =: accId.Id]){
                    if(m != null){
                        stockSuppl.put(m.Stock_Item__c,m);                   
                    }
                }
                
                System.debug('stockSuppl' + stockSuppl);
                
                List<OpportunityLineItem> newOli = new List<OpportunityLineItem>();
                //if(stockSuppl.size() > 0){
                for(OpportunityLineItem e : oliList){             
                    OpportunityLineItem m = new OpportunityLineItem();
                    if(stockSuppl.get(e.product2Id) != null){                        
                        m.PricebookEntryId = e.PricebookEntryId;
                        m.UnitPrice = stockSuppl.get(e.product2Id).Pack_Price__c;
                        m.Quantity = Decimal.valueOf((stockSuppl.get(e.product2Id).Stock_Item__r.EOQ__c == null) ? String.valueOf(e.Quantity) : String.valueOf(stockSuppl.get(e.product2Id).Stock_Item__r.EOQ__c));
                        m.Discount = stockSuppl.get(e.product2Id).Discount_Percentage__c;
                        m.Description = e.Description == null ? '' : e.Description;
                        m.OpportunityId = Oppt1.Id;
                    }
                    else{                        
                        m.PricebookEntryId = e.PricebookEntryId;
                        m.UnitPrice = e.UnitPrice;
                        m.Quantity = e.Quantity;
                        m.Discount = e.Discount == null ? 0 : e.Discount;
                        m.Description = e.Description == null ? '' : e.Description;
                        m.OpportunityId = Oppt1.Id;
                    }
                    newOli.add(m);        
                    
                    
                }
                //}
                
                System.debug('newoliList ' + newOli);            
                insert newOli;      
                Account acct = [SELECT override_GST_rate__c,Remaining_Credit_Balance__c FROM Account WHERE Id =: oppt1.AccountId];
                Decimal TotalVal = 0.0;
                Decimal taxPercent = 0.0;
                Decimal taxAmt = 0.0;
                Decimal totalAmountWithTax = 0.0;
                if(String.isNotBlank(acct.override_GST_rate__c) && acct.override_GST_rate__c != 'Auto Select Rate'){
                    String gstRate = acct.override_GST_rate__c;
                    String[] gr = gstRate.split('%');
                    taxPercent = Decimal.valueOf(gr[0]);
                    
                }
                System.debug('m' + newOli);
                for(OpportunityLineItem r : newOli){
                    Decimal prodTotal = (Double.valueOf(r.UnitPrice) * Double.valueOf(r.Quantity)) - ((Double.valueOf(r.UnitPrice) * Double.valueOf(r.quantity)) * (Double.valueOf(r.Discount)/100));
                    TotalVal += prodTotal;
                    taxAmt = TotalVal * (taxPercent/100);
                    totalAmountWithTax = TotalVal + taxAmt;
                    
                    oppt1.subtotal__c = TotalVal;
                    oppt1.tax_total__c = taxAmt;
                    
                    update oppt1;
                }
                
                ar.ReturnCode = '1';
                ar.cloneOppId = Oppt1.Id;     
                cloneStat.add(ar);
                return cloneStat;
                
            }
              catch(Exception e){
            Database.rollback(sp);
            ar.ReturnCode = '0';
            ar.cloneOppId = opportunityId;
            cloneStat.add(ar);
            ErrorLogController.createErrorLog(e.getTypeName(),e.getMessage(),'BoaOpportunityClone.Opportunityreplicate',e.getLineNumber());
            return cloneStat;
        }


following are my test class
@istest 

public class BoacloneTest {
    testMethod static void BoacloneTest(){
       Account acc = new Account();
        acc.Name= 'test';
        acc.Base_Price__c='retail';
        acc.Region__c = 'Auckland';
        acc.Phone = '78965412';
        acc.Email__c= 'abc@gmail.com';
        insert acc;
        
        Opportunity opp =new Opportunity();
        Opp.Name=string.valueof('1');
        opp.AccountId = acc.Id;
        opp.Order_Date__c= date.today();
        opp.CloseDate = date.today();
        opp.Lead_Time__c = 'Sea Freight Lead Time';
        opp.StageName = 'Prospecting';
        Opp.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Purchase_Order').getRecordTypeId();
        insert opp;
        
        
        Product2 newProduct = new Product2();
        newProduct.Name = 'newProduct';
        newProduct.ProductCode = 'VSC24B100';
        newProduct.IsActive = true;
        insert newProduct;
        
        
        Id PricebookId =  test.getStandardPricebookId();
        PricebookEntry newPBE = new PricebookEntry();
        newPBE.Pricebook2Id = PricebookId;
        newPBE.Product2Id = newProduct.Id;
        newPBE.UnitPrice = 600;
        newPBE.IsActive = True;
        insert newPBE;
        
        OpportunityLineItem newOppLineItem = new OpportunityLineItem();
        newOppLineItem.OpportunityId = opp.Id;
        newOpplineItem.PricebookEntryId = newPBE.Id;
        newOpplineItem.Quantity = 1;
        newOpplineItem.TotalPrice = newPBE.UnitPrice;
        
        insert newOppLineItem;
        /*
Stock_Supplier__c stock = new Stock_Supplier__c();
// stock.Id= newProduct.Id;
stock.Supplier_Name__c = 'test';
stock.Supplier_Code__c = 'VS4B100';
insert stock;
*/
        
        Pricing_Policy__c pp = new Pricing_Policy__c();
        pp.Name = 'test2';
        pp.Start_Date__c = date.today();
        pp.Start_Date__c = date.newInstance(2018, 09, 10);
        pp.IS_Active__c = True;
        insert pp;
        
        Pricing_Policy_Rules__c pricerule = new Pricing_Policy_Rules__c();
        pricerule.Pricing_Policy__c = pp.Id;
        pricerule.Start_Date__c = date.today();
        pricerule.Stop_Date__c = date.newInstance(2019, 08, 11);
        insert pricerule;
        
        Pricing_Policy_with_Customer__c ppwcustom =  new Pricing_Policy_with_Customer__c();
        ppwcustom.Name = 'test3';
        ppwcustom.Customers__c = acc.Id;
        ppwcustom.Pricing_Policy__c = pp.Id;
        insert ppwcustom;
        
        
    }
    @istest private static void opportunityReplicateTest(){
        Account acc = new Account();
        acc.Name= 'test';
        acc.Base_Price__c='retail';
        acc.Region__c = 'Auckland';
        acc.Phone = '78965412';
        acc.Email__c= 'abc@gmail.com';
        insert acc;
       
        Opportunity opp =new Opportunity();
        opp.Name=string.valueof('1');
        opp.AccountId = acc.Id;
        opp.Order_Date__c= date.today();
        opp.CloseDate = date.today();
        opp.Lead_Time__c = 'Sea Freight Lead Time';
        opp.StageName = 'Prospecting';
        opp.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Purchase_Order').getRecordTypeId();
        insert opp; 
        
        List<String> oppt= new List<String>();       
        
        List<Opportunity> oppList = [SELECT Account.Name,Id,RecordTypeId FROM Opportunity Where id=:opp.Id];
        for(Opportunity o : oppList){
            oppt.add(o.Id);
            oppt.add(o.Account.Name);
            oppt.add(o.RecordTypeId);
        }
        
       
        BoaOpportunityClone.Opportunityreplicate(oppt);
        
        
    }
    
    
}

please give mi solution
hello all,

i am tring to write trigger on attachment object to count no.of attachment  on an object and display count to on fileld that is on opportunity but i am not able to find count propr here below is my code
trigger CountAttachment on Attachment (after insert,after update,after delete,after undelete) {
    Map<Id,List<Attachment>> parent = new Map<Id,List<Attachment>>();
  set<id> attids = new set<id>();
     
   if(Trigger.new<>null){
       for(Attachment c:Trigger.new){
           Opportunity l;
           if(c.ParentId != null)
               attids.add(c.parentid);
       }
           
   }else if(Trigger.old != null){
       for(Attachment c:Trigger.old){
           if(c.ParentId<>null)      
               attids.add(Trigger.oldMap.get(c.id).parentid);
       }
   }
   if(attids.size()>0){
       try{
           List<Attachment> a = new List<Attachment>();
           Map<id,Opportunity> testmap = new Map<id,Opportunity>([select id,Count_Attachment__c from Opportunity where id IN: attids]);
           a = [select id,parentid from Attachment where parentid IN:attids];
           
           for(Attachment at: a){
               List<Attachment> llist = new List<Attachment>();
               if(parent.get(at.parentid) == null){
                   llist = new List<Attachment>();
                   llist.add(at);
                   parent.put(at.parentid,llist);
               }else if(parent.get(at.parentid) != null){
                   llist = new List<Attachment>();
                   llist = parent.get(at.parentid);
                   llist.add(at);
                   parent.put(at.parentid,llist);
               }
           }
           
           for(Id i: attids){
               if(testmap.get(i) != null && parent.get(i) != null){
                  testmap.get(i).Count_Attachment__c = parent.get(i).size(); 
               
               }else if(testmap.get(i) != null && parent.get(i) == null){
                  testmap.get(i).Count_Attachment__c = 0; 
               }
           }
       
           update testmap.values();
           System.Debug(testmap.values());
       }catch(Exception e){}
    }

    
}
hello all,

i am tring to write trigger on attachment object to count no.of attachment  on an object and display count to on fileld that is on opportunity but i am not able to find count propr here below is my code
trigger CountAttachment on Attachment (after insert,after update,after delete,after undelete) {
    Map<Id,List<Attachment>> parent = new Map<Id,List<Attachment>>();
  set<id> attids = new set<id>();
     
   if(Trigger.new<>null){
       for(Attachment c:Trigger.new){
           Opportunity l;
           if(c.ParentId != null)
               attids.add(c.parentid);
       }
           
   }else if(Trigger.old != null){
       for(Attachment c:Trigger.old){
           if(c.ParentId<>null)      
               attids.add(Trigger.oldMap.get(c.id).parentid);
       }
   }
   if(attids.size()>0){
       try{
           List<Attachment> a = new List<Attachment>();
           Map<id,Opportunity> testmap = new Map<id,Opportunity>([select id,Count_Attachment__c from Opportunity where id IN: attids]);
           a = [select id,parentid from Attachment where parentid IN:attids];
           
           for(Attachment at: a){
               List<Attachment> llist = new List<Attachment>();
               if(parent.get(at.parentid) == null){
                   llist = new List<Attachment>();
                   llist.add(at);
                   parent.put(at.parentid,llist);
               }else if(parent.get(at.parentid) != null){
                   llist = new List<Attachment>();
                   llist = parent.get(at.parentid);
                   llist.add(at);
                   parent.put(at.parentid,llist);
               }
           }
           
           for(Id i: attids){
               if(testmap.get(i) != null && parent.get(i) != null){
                  testmap.get(i).Count_Attachment__c = parent.get(i).size(); 
               
               }else if(testmap.get(i) != null && parent.get(i) == null){
                  testmap.get(i).Count_Attachment__c = 0; 
               }
           }
       
           update testmap.values();
           System.Debug(testmap.values());
       }catch(Exception e){}
    }

    
}