function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Riby VargheseRiby Varghese 

Need Help::System.QueryException: invalid ID field: null

hi i got this error when using test class.
@isTest(seeAllData=false)

public class CaseLoyalityTest {
@istest
     static  void testSearch() 
        
    {   Case acc = new Case();
        ApexPages.StandardController stc = new ApexPages.Standardcontroller(acc);
           CaseLoyaltyCtrl clc=new CaseLoyaltyCtrl(stc);
        
           clc.GetCustomer();
           clc.SaveLoyalty();
    }   
   
    @istest
     static  void testSearch1()     
    { 
    
        Case acc = new Case();
         acc.Phone_Number__c='8989098989';
        acc.Pin__c='test';
 
        ApexPages.StandardController stc = new ApexPages.Standardcontroller(acc);
        CaseLoyaltyCtrl clc=new CaseLoyaltyCtrl(stc);
           
             Test.startTest();
            Test.setMock(HttpCalloutMock.class, new MockPlentiResponseGenerator());
            clc.PINReset();
            Test.stopTest();
         
    }  
     @istest
     static void testSearch2() 
        
    {
        case acc=new case();
        ApexPages.StandardController stc = new ApexPages.Standardcontroller(acc);
           CaseLoyaltyCtrl clc=new CaseLoyaltyCtrl(stc);
           clc.GetAppcode('1');
        clc.GetAppcode('2');
        clc.GetAppcode('3');
           
         
    }  
     @istest
     static void testSearch3() 
        
    {
        case acc=new case();
        acc.Action__c='Password Reset';
        acc.Password__c='hai';
        ApexPages.StandardController stc = new ApexPages.Standardcontroller(acc);
           CaseLoyaltyCtrl clc=new CaseLoyaltyCtrl(stc);
       // clc.RstPassword();
     
 Test.setMock(HttpCalloutMock.class, new MockPlentiResponseGenerator());
            clc.RstPassword();
            Test.stopTest();
        
       

 
    }  
}


Apexclass
global with sharing class CaseLoyaltyCtrl{
      
    private ApexPages.StandardController stdCtrl;
    public  Case thisCase;
    Account thisAccount;
    Account selectedAccount;
    public String actionSelected {get; set;}
    public String accountLevelStatus {get; set;}
    PageReference pageRef;
    public String accountId {get; set;}
    public String cardNumber {get; set;}
    public String phoneNumber {get; set;}
    public String accountName {get; set;}
    string customerID {get;set;}
    
    public class CRCSearch
    {
        public String crcid {get; set;}
    }
    
    public class PasswordResetModel
    {
        public String crcId{get;set;}
        public String appCode{get;set;}
        public Boolean resetPassword{get;set;}
        public Boolean sendSMS{get;set;}
        public Boolean sendEmail{get;set;}
        public String templateType{get;set;}
    }
    public OmniOperations.Customer GetCustomer(){  // Customer search to get the customer and update the customer details
        
        // SearchOmniCtrl searchObj = new SearchOmniCtrl();
        // searchObj.doSearch();
        OmniOperations.Customer tempCust = new OmniOperations.Customer();
        return tempCust;
    }
    
    
    public CaseLoyaltyCtrl(ApexPages.StandardController stdController) { // Constructor
          stdCtrl=stdController;
        thisCase = (Case)stdController.getRecord();
        customerID = ApexPages.currentPage().getParameters().get('accid');
        System.debug('customerID==='+customerID);
        //if(!customerID.equalsIgnoreCase(null))
        if(!string.isEmpty(customerID))   
        {
           setCustomerName();
        }
        
        //thisAccount = (Account)stdController.getRecords();
        // System.debug('thisAccount.Name'+thisAccount.Name);
    }
     public void AccountPopulated()
 {
  Case cont=(Case) stdCtrl.getRecord();
  string soql = 'select id,Card_Number__c from Account  where id =\''+cont.Accountid+'\'';
  cont.Account  = Database.query(soql);
  //cont.Account=[select id, Site,phone__c from Account where id  = '0011F0000063GTiQAM'];
  cont.Card_Number__c =cont.Account.Card_Number__c;
 }
    private void setCustomerName()
    {
        if (!customerID.equalsIgnoreCase('') || customerID != null)
        {
            string soql = 'select Name from Account where id =\''+customerID+'\'';
            System.debug('soql'+soql);
            try
            {
                Account acc =  Database.query(soql);
                accountName = acc.Name;
            }
            catch(exception e){}
            
        }
    }
    public PageReference SaveLoyalty(){
        Boolean resStatus= false;
        Integer flag = 0; // flag to save case if no action is selected belore saving. Case can be also saved even if no action is selected.
        
        if(String.valueOf(thisCase.Action__c)  == 'Opt out of SMS')
        {
            flag = 1;
            resStatus = SetSMSOption();
            System.debug('IN SMS resStatus'+ resStatus);
        }
        else if(String.valueOf(thisCase.Action__c) == 'Pin Reset')
        {
            flag = 1;
            resStatus = PINReset();
            System.debug('IN PIN RST resStatus'+resStatus);
        }
        else if(String.valueOf(thisCase.Action__c) == 'Password Reset')
        {
            flag = 1;
            resStatus = RstPassword();
            System.debug('IN PASSWORD RST');
        }
        else if(String.valueOf(thisCase.Action__c) == 'Replacement Card')
        {
            flag = 1;
            resStatus = ReplaceCard();
            System.debug('IN REPLCMNT CARD resStatus');
        }
        else if(String.valueOf(thisCase.Action__c) == 'Account Status Update')
        {
            flag = 1;
            resStatus = SetAccountStatus();
            System.debug('IN ACU resStatus');
        }
        else
        {
            flag = 0;
            System.debug('No Action Selected');
        }
                
        PageReference p ; 
        if(resStatus)
        {
        try
        {
            
            thisCase.RecordTypeId=[Select Id From RecordType where DeveloperName ='Loyalty_SEG_Rewards' and SobjectType='case' limit 1].id;
            System.debug('thisCase.RecordTypeId===='+thisCase.RecordTypeId);
            System.debug('thisCase==='+thisCase);
            
            thisCase.AccountId=thisCase.accountid;
            if(thisCase.id == null)
            {
                Database.saveResult s = Database.insert(thisCase);
                p= new PageReference('/'+s.id);
                System.debug('s.id  1==='+s.id);
            }
            else
            {
                Database.saveResult s = Database.update(thisCase);
                p= new PageReference('/'+s.id);
                System.debug('s.id  2==='+s.id);
                
            }
            
            
        }
        catch(Exception e){}
        
        return p; 
           
        }
        else
        {
             //p= new PageReference();
        }
        return p; 
    }
    
    public Boolean RstPassword(){
        System.debug('thisCase.accountid====='+thisCase);
        String soql = 'select Card_Number__c,Member_ID__c,Chain_Id__c from account where id=\''+thisCase.accountid+'\'';
        
       
        //OmniOperations.Customer cust = new  OmniOperations.Customer();
       // cust.crcId = crcid;
        String reqBodyCust = '';
        /* reqBodyCust = JSON.serialize(cust,true);
        OmniOperations omniOps = new OmniOperations();
        HttpResponse result = omniOps.searchPlenti(reqBodyCust);
        System.debug('RstPassword Customer ====='+result.getBody());
        
        String reqBody = '';*/
        System.debug('RstPassword RES====='+thisCase.Action__c);
        
        CaseLoyaltyOperations loyaltyOps = new CaseLoyaltyOperations();
        PasswordResetModel passwordmodel = new PasswordResetModel();
         list<Account> rslt = Database.query(soql);
        passwordmodel.crcid = String.valueOf(rslt[0].Card_Number__c);
        string appCodenNum = GetAppcode(String.valueOf(rslt[0].Chain_Id__c));
        if(String.isNotEmpty(appCodenNum))
            passwordmodel.appCode = appCodenNum;
        passwordmodel.resetPassword = true;
        passwordmodel.sendSMS = true;
        passwordmodel.sendEmail = true; 
        passwordmodel.templateType = 'ResetPasswordAndCode';
       // OmniOperations.Customer customer = new OmniOperations.Customer();
        //fill the above customer object with necessary values.
      //  reqBody = JSON.serialize(result.getBody(),true);
       // System.debug('RstPassword reqBody====='+reqBody);
        
        HttpResponse res = loyaltyOps.ResetPassword(passwordmodel.crcId,passwordmodel.appCode,passwordmodel.resetPassword,passwordmodel.sendSMS,passwordmodel.sendEmail,passwordmodel.templateType);
        
        System.debug('RstPassword RES====='+res);
        
        if(res.getStatusCode() == 200)
        {
            return true;
        }
        else
        {
            return false;
        }        
    }
    
    public Boolean SetSMSOption(){
        System.debug('thisCase====='+thisCase);
        
        CaseLoyaltyOperations caseOps = new CaseLoyaltyOperations();
        OmniOperations.Customer customer = new OmniOperations.Customer();
        String soql = 'select Card_Number__c from account where id=\''+thisCase.accountid+'\'';
        list<Account> resu = Database.query(soql);
        String crcid = String.valueOf(resu[0].Card_Number__c);
        System.debug('crcid====='+crcid);
        HttpResponse res = caseOps.SetSMSOptInOut(crcid ,true);
        if(res.getStatusCode() == 200)
        {
            return true;
        }
        else
        {
            return false;
        }
        
    }
    
    public Boolean PINReset(){
        CaseLoyaltyOperations.PINReset pinrst = new CaseLoyaltyOperations.PINReset();
        CaseLoyaltyOperations caseOps = new CaseLoyaltyOperations();
        pinrst.Alias  =  Utils.extractDigitsFromPhone(thisCase.Phone_Number__c );
        //pinrst.Alias  = thisCase.Phone_Number__c;
        pinrst.AliasType = 3;
        pinrst.PinPassword = thisCase.Pin__c;
        pinrst.Source = 'CallCenter';
        
        String reqBody = JSON.serialize(pinrst,true);
        System.debug('PIN reqBody====='+reqBody);
        HttpResponse res = caseOps.ResetPIN(reqBody);
        System.debug('PIN RES====='+res.getBody());
        if(res.getStatusCode() == 200)
        {
            return true;
        }
        else
        {
            return false;
        }
        
    }
    
    public Boolean ReplaceCard(){
        
        System.debug('thisCase.accountid====='+thisCase);
        
        String soql = 'select Card_Number__c,Member_ID__c,Chain_Id__c,SEG_Rewards_Card_Number__c from account where id=\''+thisCase.accountid+'\'';
        
        list<Account> rslt = Database.query(soql);
        String crcid = String.valueOf(rslt[0].Card_Number__c);
        string memberID = String.valueOf(rslt[0].Member_ID__c);
        string SEGCardNumber = String.valueOf(rslt[0].SEG_Rewards_Card_Number__c);
        System.debug('thisCase.Replacement_Card__c==='+thisCase.Replacement_Card__c);
        
        //START: Below code is to prepare customer request obj
        CustomerSave.Customer customerObj = new CustomerSave.Customer();
        CustomerSave.Membership membershipObj = new  CustomerSave.Membership();
        List<CustomerSave.MemberAlias> memberaliasObj = new  List<CustomerSave.MemberAlias>();
        CustomerSave.MemberAlias tempMembr = new CustomerSave.MemberAlias();
        
        customerObj.crcId = crcid;
        customerObj.chainId = rslt[0].Chain_Id__c;
        customerObj.memberId = memberID;
        
        if(!string.isEmpty(SEGCardNumber) || !SEGCardNumber.equalsIgnoreCase('null'))
        {
            CustomerSave.MemberAlias membrSEGOld = new CustomerSave.MemberAlias();
            membrSEGOld.aliasNumber  =  SEGCardNumber;
            membrSEGOld.memberId = memberID;
            membrSEGOld.aliasType = '2';
            membrSEGOld.aliasStatus = '1';
            membrSEGOld.lastUpdateDate = null;
            membrSEGOld.lastUpdateSource = 'CUSTOMERCALLCENTER';
            memberaliasObj.Add(membrSEGOld);
        }
        
        tempMembr.aliasNumber  =  String.valueOf(thisCase.Replacement_Card__c);
        tempMembr.memberId = memberID;
        tempMembr.aliasType = '2';
        tempMembr.aliasStatus = '0';
        tempMembr.lastUpdateDate = null;
        tempMembr.lastUpdateSource = 'CUSTOMERCALLCENTER';
        
        memberaliasObj.Add(tempMembr);
        membershipObj.custAliasRecords = memberaliasObj;
        membershipObj.memberId = memberID;
        customerObj.membership = membershipObj;
        //STOP: Below code is to prepare customer request obj
        
        String reqBody = JSON.serialize(customerObj,true);
        System.debug('REPLCMNT CARD reqBody==='+reqBody);
        
        //OmniOperations omniOps = new OmniOperations();
        //HttpResponse res = omniOps.searchPlenti(reqBody);
        
        
        CaseLoyaltyOperations caseOps = new CaseLoyaltyOperations();
        HttpResponse result = caseOps.ReplaceCard(reqBody);
        //Update on ODS
        
        if(result.getStatusCode() == 200)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public Boolean SetAccountStatus(){
        String soql = 'select Card_Number__c,Member_ID__c,Chain_Id__c,PIN_Status__c from account where id=\''+thisCase.accountid+'\'';
        list<Account> rslt = Database.query(soql);
        String crcid = String.valueOf(rslt[0].Card_Number__c);
        string chainId = String.valueOf(rslt[0].Chain_Id__c);
        String memberId = String.valueOf(rslt[0].Member_ID__c);
        boolean pinStatus = Boolean.valueOf(rslt[0].PIN_Status__c);
        System.Debug('PIN Status'+String.valueOf(rslt[0].PIN_Status__c));
        // in case of active we have to check PIN is set or not for the customer
        String enrollStatus = Utils.setEnrollmentStatusForLoyaltyCase(thisCase.Account_Level_Status__c);
        System.Debug('enrollStatus'+enrollStatus);
        //
        
        CustomerSave.Customer customerObj = new CustomerSave.Customer();
        CustomerSave.Membership membershipObj = new CustomerSave.Membership();
        
        
        customerObj.crcId = crcid;
        customerObj.memberId = memberId;
        customerObj.chainId = Decimal.valueOf(chainId);
        //customerObj.firstName = fname;
        //customerObj.lastName = lname;
        membershipObj.memberId = memberId;
        if (enrollStatus == 'E')
        {
            if(pinStatus)
            {
                membershipObj.enrollmentStatus = 'E';
            } 
            else
            {
                membershipObj.enrollmentStatus = 'P';
            }
        }
        else if (enrollStatus == 'B')
        {
            membershipObj.enrollmentStatus = 'B';
        }
        else if (enrollStatus == 'C')
        {
            membershipObj.enrollmentStatus = 'C';
        }
        membershipObj.lastUpdateSource = 'CUSTOMERCALLCENTER';
        customerObj.membership = membershipObj;
        
        String reqBody = JSON.serialize(customerObj,true);
        System.debug('SetAccountStatus  reqBody==='+reqBody);
        CaseLoyaltyOperations caseops = new CaseLoyaltyOperations();
        HttpResponse res = caseops.AccountStatusSet(reqBody);
        System.debug('SetAccountStatus  resBody==='+res.getBody());
        if(res.getStatusCode() == 200)
        {
            return true;
        }
        else
        {
            return false;
        }
        //update on ods.
    }
    
    public String GetAppcode(string appcodeNum){
        if(appcodeNum == '1')//WinDixie
        {
            return '003014';
        }
            
        else if(appcodeNum == '2')//Bi-Lo
        {
            return '013014';
        }
        else if(appcodeNum == '3')//Harveys
        {
            return '023014';
        }
        return null;
    }
    
    @RemoteAction
    global static Account getAccount(String accountID) {
        Account account ;
        string soql = 'SELECT Phone__c FROM Account WHERE Id=\''+accountID+'\'';
        account = Database.query(soql);
        return account;
    }
}
VamsiVamsi
Hi,

You need to insert the Case first before passing it to the standard controller in the Test class