You need to sign in to do that
Don't have an account?
SFDC 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);
}
}
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);
}
}
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
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:
If it helps Please mark it best answer.
THanks!
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.
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