• SFDC BUG
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies
Hi All 

How can i improve my code coverage of my HTTP get method contoller...I have written but ist 73%and unable to improve
 
public with sharing class Popular {

    public Popular(ApexPages.StandardController controller) {

    }

    Public List< Object> mainitems {get; set;}
    Public Map < String, Object > mapitems {get; set;}
    Public List<Map < String, Object >> listmapitems {get; set;}
    public Popular() {

    }


    public PageReference webservcie(){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://retail.testlabs.com/v1/popular?client_id=Test&rails=sellers');
        //request.setHeader('Content-type', 'application/json');
        request.setMethod('GET');
        
        string autho = 'Bearer '+userInfo.getSessionId();
        request.setHeader('Authorization', autho);
        
        HttpResponse response = http.send(request);
        String jsonBody = response.getBody();
        List < Object > items = (List < Object > ) JSON.deserializeUntyped(jsonBody);
        system.debug('****' + items);
        for (Object o: items) {
            Map < String, Object > maprec = (Map < String, Object > ) o;
            String pgnu = (String) maprec.get('rail_name');
            system.debug('&&&&pgnu' + pgnu);
            mainitems = (List < Object > ) maprec.get('items');
            system.debug('&&&&items' + mainitems);
        }
        for (Object m: mainitems) {
            mapitems = (Map < String, Object > ) m;
            String strbrand= (String) mapitems.get('brand');
            system.debug('##&&items' + strbrand);
        }
        return null;
    }
   
}
 
@isTest
private class Popular_Test{
    static testMethod void test_webservcie_UseCase1(){
        
      
        
    PageReference pageRef = Page.Popular;
    
    Test.setCurrentPage(pageRef);
   
    if (Test.isRunningTest()) {  
       Test.setMock(HttpCalloutMock.class, new Popular_Mock());
       
       
       Popular obj01 = new Popular();
       obj01.webservcie();
       }
   
    
   
       
   
    
    }
}
=======================================
@isTest
global class Popular_Mock implements HttpCalloutMock{
  // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
       System.assertEquals('GET', req.getMethod());
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        String body = '[{ "item_id": "...................: "string"}]';//Pass the json 
        res.setBody(body);
        res.setHeader('Content-Type', 'application/xml');
        res.setStatusCode(200);
        return res;
    }
}

 
How can i improve code coverage of web service class 90% to 100%(line 19,58 and 64 is not cover)
global class Send_Challn_To_Accounts
{
public static blob b;

    WebService static string Send_Challn_To_Accounts(string id)
    {
     try
     {
              
          Challan__c qt=[Select id,Call_up_order_id__c,Division__c,Excise_Registration_No__c,Range__c,Freight_Charge_Applicable_Extra__c,Challan_id__c,Basic_Price_Each_item__c,Basic_Price_per_1000__c,Accounts_Email_Id__c,Sale_Confirmation_Order_ID__c,Account_Id__c,Account_Name__c,Company_Name__c,Account_Name__r.Name,Challan_Number__c,Sale_Confirmation_Order__r.name from Challan__c where id=:id];
        
        
            PageReference pdf =new PageReference('/apex/view_challan_form?id='+id);
            
            pdf.setRedirect(true);
           
            if(Test.isRunningTest() == false){

       b = pdf.getContent();
}
        
        //--------create a mail object to send a single email.--------
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         
         string[] toaddress =String.valueOf(qt.Accounts_Email_Id__c).Split(',');
            
            mail.setToAddresses(toaddress);
          //  mail.setToAddresses(new string[] {qt.email__c}); // Internal_Email_User_Id__c text fied
          
            mail.setBccAddresses(new String[] {'test@globalnest.com'});
            mail.setBccSender(true);
          
            //mail.setSenderDisplayName(qt.Account_Name__c);
            
            mail.setSubject('Challan For'+' '+qt.Company_Name__c+' : '+qt.Sale_Confirmation_Order__r.name);
            
        //------------- Create the email attachment-----------------
         Invoice_Dispatch_details__c invoice=new Invoice_Dispatch_details__c();
       //   invoice=[select id,Account_Lookup__c,Purchase_Order__c from Invoice_Dispatch_details__c where Challan__c=:id];
          
            
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName('Challan.pdf');
            efa.setBody(b);
        //-----------------------------------------------------------
            mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
         
      mail.setPlainTextBody('Hi Accounts Team, Please find the Challan for '+qt.Company_Name__c +' '+'attached to this mail.'+'\n'+'\n'+'\n'+'\n'+'Freight Charge Applicable Extra:'+qt.Freight_Charge_Applicable_Extra__c+'\n'  +'Account Name:'+qt.Company_Name__c+'\n'+'SCO no:'+qt.Sale_Confirmation_Order__r.name+'\n'+'Challan No:'+qt.Challan_Number__c+'\n'+'Basic Price (Each item):'+qt.Basic_Price_Each_item__c+'\n'+'Basic Price (per 1000):'+qt.Basic_Price_per_1000__c+'\n'+'Excise Registration No:'+qt.Excise_Registration_No__c+'\n'+'Division:'+qt.Division__c+'\n'+'Range:'+qt.Range__c+'\n'+'\n'+'Thanks & Regards'+'\n'+'Care Team');
        //------------------------Attach sales order to a contract--------------------
         Attachment formAttach = new Attachment();
         formAttach.Name = 'Challan'+qt.Account_Name__c+'.pdf';
         formAttach.body = b;
         formAttach.ContentType='application/pdf';
         formAttach.parentId =id;
         if (!Test.isRunningTest())
         {
          
           insert formAttach;
         }
        
            //send the email
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail } );
           
            return 'Challan has been sent';
        }
        catch(exception ex)
        {
           
          string ex1=string.valueof(ex);
          return ex1;
            
        }

    }
    
}
 
@isTest
private class testSend_Challn_To_Accounts
{
  
  
  static testMethod void teSend_Challn_To_Accounts()
  {


        account a=new account(name='test',Region__c='East');
        insert a;
        
        Master_Product__c mp=new Master_Product__c(name='test name',Product_Code__c='c2');
        insert mp;
        
        contact c=new contact(firstname='Meenakshmi',lastname='Goswami',Designation__c='Developer',Accountid=a.id);
        insert c;

        Opportunity2__c op=new Opportunity2__c(Name='test1',Account__c=a.id,Master_Product__c=mp.id,Technical_Bid_date__c=date.Today(),Type_of_Business__c='Regular',Contact_Person__c=c.id);
        insert op;
     
        Opportunity_Product_Detail__c opd=new Opportunity_Product_Detail__c(Opportunity__c=op.id,Company__c=a.id,Quantity__c=1000,Height__c=12,Label_Height__c=12,Hologram_Height__c=14,Holographic_Film_Height__c=15,Multimax_Seal_Height__c=16,Sleeve_Height__c=90,Foil_Stamping_Height__c=17,Width__c=22,Label_Width__c=23,Foil_Width_for_Supplies__c=24,Hologram_Width__c=25,Holographic_Film_Width__c=27,Holographic_Strip_Width__c=28,Sleeve_Layflat_Width__c=29,Sleeve_Width__c=30,Foil_Stamping_Width__c=31,Make__c='ACR',Model__c='GT800');
        insert opd;
        
        Quote__c qt=new Quote__c(Opportunity_Product_Detail__c=opd.id,Purpose_of_Sales__c='SEZ',Basic_Price_Each_item__c=23,Declaration_form_be_provided__c='No',TTR_Height__c=13,TTR_Width__c=12);
        insert qt;
        
        Purchase_Order__c sco=new Purchase_Order__c(Quote__c=qt.id,Opportunity__c=op.id,New_Flash_Aidc_Email_sent__c='venky@globalnest.com');
        insert sco;
        
        Call_Up_Order__c call=new Call_Up_Order__c(Quote__c=qt.id,Name='test',Sale_Confirmation_Order__c=sco.id,Call_Up_Quantity__c=2);
        insert call;
                system.debug(' Call_Up_Orde details'+call);
        Challan__c c2=new Challan__c(Call_Up_Order__c=call.id,Sale_Confirmation_Order__c=sco.id,Challan_Date__c=system.today(),Challan_Number__c='1234');
        insert c2;
          Blob b = Blob.valueOf('Test Data');  
      
    Attachment attachment = new Attachment();  
    attachment.ParentId = c2.id;  
    attachment.Name = 'Test Attachment for Parent';  
    attachment.Body = b;  
      
    insert(attachment);  
              Send_Challn_To_Accounts   act = new Send_Challn_To_Accounts();
Send_Challn_To_Accounts.Send_Challn_To_Accounts(c2.id);
      
  }
}
Hi Guys

Need to improve my code coverage of my trigger 75% to 100%
trigger DocumentAttached on Purchase_Order__c (before insert , before update) 
{ 
    for(Purchase_Order__c tsk : Trigger.new){ 
        Attachment[] attList = [select id, name, body from Attachment where ParentId = :Trigger.new[0].id]; 
        if(attList.size() > 0) { 
            tsk.PO_Uploaded__c =true; 
        }
    }
}

 
How can i improve code coverage of web service class 90% to 100%(line 19,58 and 64 is not cover)
global class Send_Challn_To_Accounts
{
public static blob b;

    WebService static string Send_Challn_To_Accounts(string id)
    {
     try
     {
              
          Challan__c qt=[Select id,Call_up_order_id__c,Division__c,Excise_Registration_No__c,Range__c,Freight_Charge_Applicable_Extra__c,Challan_id__c,Basic_Price_Each_item__c,Basic_Price_per_1000__c,Accounts_Email_Id__c,Sale_Confirmation_Order_ID__c,Account_Id__c,Account_Name__c,Company_Name__c,Account_Name__r.Name,Challan_Number__c,Sale_Confirmation_Order__r.name from Challan__c where id=:id];
        
        
            PageReference pdf =new PageReference('/apex/view_challan_form?id='+id);
            
            pdf.setRedirect(true);
           
            if(Test.isRunningTest() == false){

       b = pdf.getContent();
}
        
        //--------create a mail object to send a single email.--------
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         
         string[] toaddress =String.valueOf(qt.Accounts_Email_Id__c).Split(',');
            
            mail.setToAddresses(toaddress);
          //  mail.setToAddresses(new string[] {qt.email__c}); // Internal_Email_User_Id__c text fied
          
            mail.setBccAddresses(new String[] {'test@globalnest.com'});
            mail.setBccSender(true);
          
            //mail.setSenderDisplayName(qt.Account_Name__c);
            
            mail.setSubject('Challan For'+' '+qt.Company_Name__c+' : '+qt.Sale_Confirmation_Order__r.name);
            
        //------------- Create the email attachment-----------------
         Invoice_Dispatch_details__c invoice=new Invoice_Dispatch_details__c();
       //   invoice=[select id,Account_Lookup__c,Purchase_Order__c from Invoice_Dispatch_details__c where Challan__c=:id];
          
            
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName('Challan.pdf');
            efa.setBody(b);
        //-----------------------------------------------------------
            mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
         
      mail.setPlainTextBody('Hi Accounts Team, Please find the Challan for '+qt.Company_Name__c +' '+'attached to this mail.'+'\n'+'\n'+'\n'+'\n'+'Freight Charge Applicable Extra:'+qt.Freight_Charge_Applicable_Extra__c+'\n'  +'Account Name:'+qt.Company_Name__c+'\n'+'SCO no:'+qt.Sale_Confirmation_Order__r.name+'\n'+'Challan No:'+qt.Challan_Number__c+'\n'+'Basic Price (Each item):'+qt.Basic_Price_Each_item__c+'\n'+'Basic Price (per 1000):'+qt.Basic_Price_per_1000__c+'\n'+'Excise Registration No:'+qt.Excise_Registration_No__c+'\n'+'Division:'+qt.Division__c+'\n'+'Range:'+qt.Range__c+'\n'+'\n'+'Thanks & Regards'+'\n'+'Care Team');
        //------------------------Attach sales order to a contract--------------------
         Attachment formAttach = new Attachment();
         formAttach.Name = 'Challan'+qt.Account_Name__c+'.pdf';
         formAttach.body = b;
         formAttach.ContentType='application/pdf';
         formAttach.parentId =id;
         if (!Test.isRunningTest())
         {
          
           insert formAttach;
         }
        
            //send the email
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail } );
           
            return 'Challan has been sent';
        }
        catch(exception ex)
        {
           
          string ex1=string.valueof(ex);
          return ex1;
            
        }

    }
    
}
 
@isTest
private class testSend_Challn_To_Accounts
{
  
  
  static testMethod void teSend_Challn_To_Accounts()
  {


        account a=new account(name='test',Region__c='East');
        insert a;
        
        Master_Product__c mp=new Master_Product__c(name='test name',Product_Code__c='c2');
        insert mp;
        
        contact c=new contact(firstname='Meenakshmi',lastname='Goswami',Designation__c='Developer',Accountid=a.id);
        insert c;

        Opportunity2__c op=new Opportunity2__c(Name='test1',Account__c=a.id,Master_Product__c=mp.id,Technical_Bid_date__c=date.Today(),Type_of_Business__c='Regular',Contact_Person__c=c.id);
        insert op;
     
        Opportunity_Product_Detail__c opd=new Opportunity_Product_Detail__c(Opportunity__c=op.id,Company__c=a.id,Quantity__c=1000,Height__c=12,Label_Height__c=12,Hologram_Height__c=14,Holographic_Film_Height__c=15,Multimax_Seal_Height__c=16,Sleeve_Height__c=90,Foil_Stamping_Height__c=17,Width__c=22,Label_Width__c=23,Foil_Width_for_Supplies__c=24,Hologram_Width__c=25,Holographic_Film_Width__c=27,Holographic_Strip_Width__c=28,Sleeve_Layflat_Width__c=29,Sleeve_Width__c=30,Foil_Stamping_Width__c=31,Make__c='ACR',Model__c='GT800');
        insert opd;
        
        Quote__c qt=new Quote__c(Opportunity_Product_Detail__c=opd.id,Purpose_of_Sales__c='SEZ',Basic_Price_Each_item__c=23,Declaration_form_be_provided__c='No',TTR_Height__c=13,TTR_Width__c=12);
        insert qt;
        
        Purchase_Order__c sco=new Purchase_Order__c(Quote__c=qt.id,Opportunity__c=op.id,New_Flash_Aidc_Email_sent__c='venky@globalnest.com');
        insert sco;
        
        Call_Up_Order__c call=new Call_Up_Order__c(Quote__c=qt.id,Name='test',Sale_Confirmation_Order__c=sco.id,Call_Up_Quantity__c=2);
        insert call;
                system.debug(' Call_Up_Orde details'+call);
        Challan__c c2=new Challan__c(Call_Up_Order__c=call.id,Sale_Confirmation_Order__c=sco.id,Challan_Date__c=system.today(),Challan_Number__c='1234');
        insert c2;
          Blob b = Blob.valueOf('Test Data');  
      
    Attachment attachment = new Attachment();  
    attachment.ParentId = c2.id;  
    attachment.Name = 'Test Attachment for Parent';  
    attachment.Body = b;  
      
    insert(attachment);  
              Send_Challn_To_Accounts   act = new Send_Challn_To_Accounts();
Send_Challn_To_Accounts.Send_Challn_To_Accounts(c2.id);
      
  }
}