You need to sign in to do that
Don't have an account?

while writing test classes getting errror
my Apex code :
public static List<GenericObject__c> GetGenericObjectByAccount(String accountIds)
{
Map<String, Schema.SObjectField> fldObjMap = schema.SObjectType.GenericObject__c .fields.getMap();
List<Schema.SObjectField> fldObjMapValues = fldObjMap.values();
String theQuery = GenerateFields(fldObjMapValues );
// Finalize query string
theQuery += ' FROM GenericObject__c WHERE Account__c in ' + accountIds ;
// Make dynamic call
GenericObject__c[] accList = Database.query(theQuery);
return accList;
}
Test class :
static testMethod void test_GetGenericObjectByAccount(){
test.startTest();
Account act=new Account();
act.name='test';
act.type='Industry';
insert act;
Opportunity opp=new opportunity();
opp.IsPrivate=true;
opp.name='zensar test';
opp.Description='description';
opp.StageName='zen stage';
opp.CloseDate=date.today();
insert opp;
GenericObject__c go=new GenericObject__c();
go.Account__c=act.id;
go.opportunity__c=opp.id;
insert go;
list<GenericObject__c > go1=[select id,name,account__c from GenericObject__c];
//string theQuery = ' FROM GenericObject__c WHERE Account__c in ' + act.id;
//string query= 'Select a.Id, a.Name, a.OwnerId from GenericObject__c a Where a.Account__C IN \''+ act.id+'\'';
like no : 325 :DataHelper.GetGenericObjectByAccount(go.id );
test.stopTest();
}
Error :
System.QueryException: expecting a colon, found 'go.id'
Stack TraceClass.DataHelper.GetGenericObjectByAccount: line 733, column 1
Class.DataHelper_test.test_GetGenericObjectByAccount: line 325, column 1
Please help me to resolve this issue
public static List<GenericObject__c> GetGenericObjectByAccount(String accountIds)
{
Map<String, Schema.SObjectField> fldObjMap = schema.SObjectType.GenericObject__c .fields.getMap();
List<Schema.SObjectField> fldObjMapValues = fldObjMap.values();
String theQuery = GenerateFields(fldObjMapValues );
// Finalize query string
theQuery += ' FROM GenericObject__c WHERE Account__c in ' + accountIds ;
// Make dynamic call
GenericObject__c[] accList = Database.query(theQuery);
return accList;
}
Test class :
static testMethod void test_GetGenericObjectByAccount(){
test.startTest();
Account act=new Account();
act.name='test';
act.type='Industry';
insert act;
Opportunity opp=new opportunity();
opp.IsPrivate=true;
opp.name='zensar test';
opp.Description='description';
opp.StageName='zen stage';
opp.CloseDate=date.today();
insert opp;
GenericObject__c go=new GenericObject__c();
go.Account__c=act.id;
go.opportunity__c=opp.id;
insert go;
list<GenericObject__c > go1=[select id,name,account__c from GenericObject__c];
//string theQuery = ' FROM GenericObject__c WHERE Account__c in ' + act.id;
//string query= 'Select a.Id, a.Name, a.OwnerId from GenericObject__c a Where a.Account__C IN \''+ act.id+'\'';
like no : 325 :DataHelper.GetGenericObjectByAccount(go.id );
test.stopTest();
}
Error :
System.QueryException: expecting a colon, found 'go.id'
Stack TraceClass.DataHelper.GetGenericObjectByAccount: line 733, column 1
Class.DataHelper_test.test_GetGenericObjectByAccount: line 325, column 1
Please help me to resolve this issue
It should work.
All Answers
Also "IN" clause takes values in round parenthesis, FYI.
See sample code below.
Add go.Accountid in set and send that set to inClausify method and pass that string to method GetGenericObjectByAccount, it should work.
Thanks.
Thanks a lot for the response
Error MessageSystem.QueryException: Only variable references are allowed in dynamic SOQL/SOSL.Stack TraceClass.DataHelper.GetGenericObjectByAccount: line 733, column 1
Class.DataHelper_test.test_GetGenericObjectByAccount: line 334, column 1
After including above approach i am facing this issue
{
Map<String, Schema.SObjectField> fldObjMap = schema.SObjectType.GenericObject__c .fields.getMap();
List<Schema.SObjectField> fldObjMapValues = fldObjMap.values();
String theQuery = GenerateFields(fldObjMapValues );
// Finalize query string
theQuery += ' FROM GenericObject__c WHERE Account__c in ' + accountIds ;
// Make dynamic call
GenericObject__c[] accList = Database.query(theQuery);
return accList;
}
Test class :
static testMethod void test_GetGenericObjectByAccount(){
test.startTest();
Account act=new Account();
act.name='test';
act.type='Industry';
insert act;
Opportunity opp=new opportunity();
opp.IsPrivate=true;
opp.name='zensar test';
opp.Description='description';
opp.StageName='zen stage';
opp.CloseDate=date.today();
insert opp;
GenericObject__c go=new GenericObject__c();
go.Account__c=act.id;
go.opportunity__c=opp.id;
insert go;
list<GenericObject__c > go1=[select id,name,account__c from GenericObject__c];
//string theQuery = ' FROM GenericObject__c WHERE Account__c in ' + act.id;
//string query= 'Select a.Id, a.Name, a.OwnerId from GenericObject__c a Where a.Account__C IN \''+ act.id+'\'';
like no : 325 :DataHelper.GetGenericObjectByAccount(go.id );
test.stopTest();
}
It should work.