+ Start a Discussion
WikWik 

Error: Compile Error: Invalid type: QueryResult at line 5 column 9

Hi,

The following line trows error

QueryResult result = conn.query( 'SELECT Id FROM Account WHERE GroupId = \'00Ge0000000GrKK\' AND Account_Type__c = \'SAM Managed\'' );

Help me fix the same
hitesh90hitesh90
Hi wik,

You have written wrong format for getting List of Account record using dynamic SOQL query.

see below Example:
List<Account> result = database.query( 'SELECT Id FROM Account WHERE Account_Type__c = \'SAM Managed\'' );

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/
WikWik
share.setAccountId(rec.getId());

even the above line is throwing error

Error: Compile Error: Method does not exist or incorrect signature: [SObject].getId() at line 12 column 32
hitesh90hitesh90
can you please post the full class code here?
WikWik
public class AccShare
{
    public void method()
    {
        QueryResult result = conn.query( 'SELECT Id FROM Account WHERE GroupId = \'00Ge0000000GrKK\' AND Account_Type__c = \'SAM Managed\'' );
        // Create a new AccountShare object
        List<AccountShare> shares = new List<AccountShare>();
        for ( sObject rec : result.getRecords() )
        {
            AccountShare share = new AccountShare();
            share.setAccountId(rec.getId());
            //Set the portal user Id to share the accounts with
            share.setUserOrGroupId( '00Ge0000000GrGX' );
            share.setAccountAccessLevel( 'Edit' );
            share.setOpportunityAccessLevel( 'Read' );
            share.setCaseAccessLevel( 'Edit' );
            shares.add( share );
        }
        conn.create( shares.toArray( new AccountShare[ shares.size() ] ) );
hitesh90hitesh90
Hi wik,

Try to use following class code to fulfill your requirement.

Apex Class:
public class AccShare{   
    public void method(){
        List<Account> result = database.query( 'SELECT Id FROM Account WHERE GroupId = \'00Ge0000000GrKK\' AND Account_Type__c = \'SAM Managed\'' );
        // Create a new AccountShare object
        List<AccountShare> shares = new List<AccountShare>();
        for ( sObject rec : result)
        {
            AccountShare share = new AccountShare();
            share.AccountId = rec.Id;
            //Set the portal user Id to share the accounts with
            share.UserOrGroupId = '00Ge0000000GrGX';
            share.AccountAccessLevel = 'Edit';
            share.OpportunityAccessLevel = 'Read';
            share.CaseAccessLevel = 'Edit';
            shares.add( share );
        }
        insert shares;       
    }
}

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/
WikWik
hey thanks a lot
but this piece of code is not serving my purpose.

i want to share Accounts of a particular public group with ACCount Type as SAM Managed with a particular user.
Scott.MScott.M
This is probably a dumb question but is the sharing model for Account set to private? If it's not the share objects for account won't exist.