• krishna p 36
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 0
    Replies
Unable to fetch account name in below query 
SELECT id,Name  FROM Account where Name ='test'
when passing the account id it fetch the result

please help me Nmae is encrpted field.
 I have a problem test coverage covers only 34%,the below apex class method 'importCSVFile' in the for loop not covering the run the test class,please suggest 
Apex class merge accounts
----------------
public class MergeAccountsController {
    public Blob csvFileBody{get;set;}
    public string csvAsString{get;set;}
    public String[] csvFileLines{get;set;}
    public List<account> acclist{get;set;}
    public Boolean isChecked{ get; set;}
    public boolean hide {get;set;}
    List<Account> accDup = new List<Account>();
    String[] csvRecordData;
    String priID, secID, terID;
    
  public MergeAccountsController(){
      csvFileLines = new String[]{};
      acclist = New List<Account>();
      hide = true;
    }
    
  public void click(){
      if(isChecked == true){
          importCSVFile();
          restore(secID);
      }
      else{
          importCSVFile();
      }
  }

  public void importCSVFile(){
        hide = false;
        try{
            csvAsString = csvFileBody.toString();
            csvFileLines = csvAsString.split('\r\n');
        }
        catch(Exception e){
            ApexPages.Message csvError = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importing data. Please make sure input csv file is correct');
            ApexPages.addMessage(csvError);
        }
        
        for(Integer i=1; i<csvFileLines.size(); i++){
            csvRecordData = csvFileLines[i].split(',');
            Integer row = i+1;
            try{
            if(csvRecordData.size() == 2) {
                  priID = csvRecordData[0];
                  secID = csvRecordData[1];
                  Account pri, sec;
                
                  try{
                    pri = [SELECT ID from Account Where ID = :priID];
                  }
                  catch(QueryException qe){
                  ApexPages.Message recordError = new ApexPages.Message(ApexPages.severity.ERROR,'Account ' +priID+ ' in Row ' +row+ ' not found');
                  ApexPages.addMessage(recordError);
                  }
                  try{
                    sec = [SELECT ID from Account Where ID = :secID];
                    }
                  catch(QueryException qe){         
                    ApexPages.Message recordError = new ApexPages.Message(ApexPages.severity.ERROR,'Account ' +secID+ ' in Row ' +row+ ' not found');
                    ApexPages.addMessage(recordError);
                  }
                
                merge pri sec;
                }
            
              else if(csvRecordData.size() == 3) {
                  priID = csvRecordData[0];
                  secID = csvRecordData[1];
                  terID = csvRecordData[2];
                  Account pri, sec, ter;
                    
                  try{
                    pri = [SELECT ID from Account Where ID = :priID];
                  }
                  catch(QueryException qe){
                    ApexPages.Message recordError = new ApexPages.Message(ApexPages.severity.ERROR,'Account ' +priID+ ' in Row ' +row+ ' not found');
                    ApexPages.addMessage(recordError);
                  }
                  try{
                    sec = [SELECT ID from Account Where ID = :secID];
                  }
                  catch(QueryException qe){
                    ApexPages.Message recordError = new ApexPages.Message(ApexPages.severity.ERROR,'Account ' +secID+ ' in Row ' +row+ ' not found');
                    ApexPages.addMessage(recordError);
                    }
                  try{
                    ter = [SELECT ID from Account Where ID = :terID];
                  }
                  catch(QueryException qe){
                    ApexPages.Message recordError = new ApexPages.Message(ApexPages.severity.ERROR,'Account ' +terID+ ' in Row ' +row+ ' not found');
                    ApexPages.addMessage(recordError);
                  }
                                    
            accDup.add(sec);
            accDup.add(ter);
                    
                  try{
                     merge pri accDup;  
                  }
                  catch(DmlException e){
                      continue;
                  }  
              }
            }
            catch(NullPointerException npe){
                continue;
            }
        }
        ApexPages.Message complete = new ApexPages.Message(ApexPages.severity.INFO,'Process Complete');
        ApexPages.addMessage(complete);
    }
    public void restore(String acc){
        Account[] sec = [SELECT Id FROM Account WHERE ID = :secID ALL ROWS];
        undelete sec;
    }
}

test class : 
@isTest
Public class MergeAccountstest{
static String str = 'Name,BillingCountry,BillingCity,BillingStreet,BillingState,BillingPostalCode\n test22,india,Chennai,test,Tamil Nadu,588543 \n test222,india,Chennai,test2,Tamil Nadu,588544';       

//system.debug('------str ------'+str );
    public static String[] csvFileLines;
    public static Blob csvFileBody;
    public static String[] csvRecordData;
    public static Boolean isChecked =true;
    public static  testMethod void importCSVFile() {
        Account a = new Account(name='test',BillingCountry='india',BillingCity='Chennai',BillingStreet='test',BillingState='Tamil Nadu',BillingPostalCode='588543');
        insert a;
        Account a1 = new Account(name='test1',BillingCountry='india',BillingCity='Chennai',BillingStreet='test',BillingState='Tamil Nadu',BillingPostalCode='588543');
        insert a1;
        test.startTest();
        //merge a a1;
        MergeAccountsController m1=new MergeAccountsController();
         if(isChecked == true){
          m1.importCSVFile();
          //m1.restore(a1);
      }
      else{
          m1.importCSVFile();
      }
        //m1.importCSVFile();
        m1.click();
        m1.restore('a');
        test.stopTest();
        
    }

    static testmethod void testfileupload(){
        Test.startTest();       
        List<sObject> acct = Test.loadData(Account.sObjectType, 'mergeaccount');
        System.debug( '----acct ---' +acct );
        System.assert(acct.size() == 5);   
        csvFileBody = Blob.valueOf(str );
        System.debug( '----csvFileBody ---' +csvFileBody );
        String csvAsString = acct.toString();
        System.debug( '----csvAsString ---' +csvAsString ); 
        csvFileLines = csvAsString.split('\n');
        
        System.debug( '----csvFileLines---' +csvFileLines );
        
         for(Integer i=1; i<csvFileLines.size(); i++){
            csvRecordData = csvFileLines[i].split(',');
           }
           

        MergeAccountsController importData = new MergeAccountsController();
        importData.csvFileBody = csvFileBody;
        importData.importCSVFile();
        Test.stopTest();
    } 

    static testmethod void testfileuploadNegative(){
        Test.startTest();       
        csvFileBody = Blob.valueOf(str);
        String csvAsString = csvFileBody.toString();
        csvFileLines = csvAsString.split('\n'); 

        MergeAccountsController  importData = new MergeAccountsController();
        importData.importCSVFile();
        Test.stopTest();
    }
}
Any one please provide certification dumps