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
SFDC Panther 92SFDC Panther 92 

Non Static method cannot be referenced from a static context

I am trying to create a test class for my apex class but i am getting an error.Can anyon please help me with it.

Apex Class :- 

public without sharing class GlobalWebChatBotServiceClass {
    
    
    
    public  LiveChatTranscript returnChatTranscriptRecord(Id chatTranscriptRecordId) {
          List<LiveChatTranscript> chatTranscriptRecords=[SELECT Id,Visitor_Customer_Type__c,Visitor_Email__c,
                                                        Visitor_First_Name__c,Visitor_Last_Name__c,Visitor_Company__c,ContactId,Location FROM LiveChatTranscript
                                                        Where Id = :chatTranscriptRecordId];
        if(chatTranscriptRecords.size()>0) {
            return chatTranscriptRecords[0];
        } else {
            return null;
        }
    }


Test Class :-

@isTest
public class GlobalWebChatBotTestClass {

        
    @istest
    static void chattranscripttest(){
        
        LiveChatTranscript chat = new LiveChatTranscript (Visitor_Customer_Type__c = 'Supplier' , Visitor_Email__c = 'a@a.com' , 
                                         Visitor_First_Name__c = 'testa' ,Visitor_Last_Name__c = 'testb' , livechatvisitorid = '5717Z000001LxeQQAS',
                                          Visitor_Company__c = 'test',Location = 'test');
        insert chat;     
        
           //GlobalWebChatBotServiceClass livechat = new GlobalWebChatBotServiceClass();
           //livechat.returnChatTranscriptRecord();
                
        id chatlist =  GlobalWebChatBotServiceClass.returnChatTranscriptRecord(chat.Id);
        

    }
    
}
 
Best Answer chosen by SFDC Panther 92
AbhinavAbhinav (Salesforce Developers) 
Hey Sorry I missed to pass parameter when calling

LiveChatTranscript lct= livechat.returnChatTranscriptRecord(chat.Id);

Have update that previous answer as well , Now you can try. Let me know if there is any issue
 

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi Pramukh,

You are getting below error as you are calling non static method by ClassName.MethodName but that only work if method is static. For non static method we have to instantiate the class first by creating object.
You can try below  sample code:
 
@isTest
public class GlobalWebChatBotTestClass {

        
    @istest
    static void chattranscripttest(){
        
        LiveChatTranscript chat = new LiveChatTranscript (Visitor_Customer_Type__c = 'Supplier' , Visitor_Email__c = 'a@a.com' , 
                                         Visitor_First_Name__c = 'testa' ,Visitor_Last_Name__c = 'testb' , livechatvisitorid = '5717Z000001LxeQQAS',
                                          Visitor_Company__c = 'test',Location = 'test');
        insert chat;     
        
           GlobalWebChatBotServiceClass livechat = new GlobalWebChatBotServiceClass();
          
                
        Test.startTest();
        LiveChatTranscript  lct=  livechat.returnChatTranscriptRecord(chat.Id);
        Test.StopTest();

    }
    
}

If it helps Please mark it best answer.

THanks!
 
SFDC Panther 92SFDC Panther 92
Hi Abhinav

Thanks for your revert.
Even i tried this but affter that i was getting another error , i.e. Method does not exist or incorrect signature: void returnChatTranscriptRecord() from the type GlobalWebChatBotServiceClass.

Apex class code which i have shared is of service class and its return i am using in my other apex class.
AbhinavAbhinav (Salesforce Developers) 
could you tell me return type of you method  returnChatTranscriptRecord in your Class. I mean is it same as you have posted or is it modified?
 
AbhinavAbhinav (Salesforce Developers) 
Hey Sorry I missed to pass parameter when calling

LiveChatTranscript lct= livechat.returnChatTranscriptRecord(chat.Id);

Have update that previous answer as well , Now you can try. Let me know if there is any issue
 
This was selected as the best answer
SFDC Panther 92SFDC Panther 92
It Worked.. Thanks a lot Abhinav