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
pallam krushnaveni 18pallam krushnaveni 18 

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 
Best Answer chosen by pallam krushnaveni 18
AshishkAshishk
Change your line no 325 to DataHelper.GetGenericObjectByAccount(String.format( '(\'\'{0}\'\')', new List<String> { String.join( new List<Id>(new Set<id>{go.Account__c}) , '\',\'') }));

It should work.

 

All Answers

Steven NsubugaSteven Nsubuga
Try adding a full colon to the query string, see below
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;
    }

 
Suraj GharatSuraj Gharat
What is the SOQL query that is getting passed to Database.query() method as a string ?

Also "IN" clause takes values in round parenthesis, FYI.
pallam krushnaveni 18pallam krushnaveni 18
Thanks a lot for the response, I can not change the code bcos this code is calling in n no of places, could you please provide test classs for the existing code 
AshishkAshishk
Source class is accepting string of ids not a single id, you need to use below approach to send ids to query then it should work.

See sample code below.
Map<id,Account> accMap=new Map<id,Account>([Select id from Account limit 2]);

public String inClausify(Set<Id> ids) {
    String inClause = String.format( '(\'\'{0}\'\')', 
                         new List<String> { String.join( new List<Id>(ids) , '\',\'') });
    return inClause;
}

string str='Select id, name from Account where id in   '+inClausify(accMap.keySet());
system.debug(Database.query(str));


Add go.Accountid in set and send that set to inClausify method and pass that string to method GetGenericObjectByAccount, it should work.

Thanks.


 
pallam krushnaveni 18pallam krushnaveni 18
Hi All, 

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 
 
AshishkAshishk
Can you paste your test method code?
pallam krushnaveni 18pallam krushnaveni 18
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();
    }
AshishkAshishk
Change your line no 325 to DataHelper.GetGenericObjectByAccount(String.format( '(\'\'{0}\'\')', new List<String> { String.join( new List<Id>(new Set<id>{go.Account__c}) , '\',\'') }));

It should work.

 
This was selected as the best answer