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
WikWik 

Help me debugging this

Public class AccShare{
QueryResult result = conn.query("SELECT Id FROM Account WHERE GroupId = '00Ge0000000GrKK' & Account_Type__c = 'SAM Managed'");
// Create a new AccountShare object
List<AccountShare> shares = new ArrayList<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()]));

Vinita_SFDCVinita_SFDC
Hello Wik,

What error are you getting? Can you try executing query separately "SELECT Id FROM Account WHERE GroupId = '00Ge0000000GrKK' & Account_Type__c = 'SAM Managed" in workbench, to check if query is causing any issue.
GlynAGlynA
Hi Wik,

I see a bunch of problems:

The code is directly in a class.  It needs to be in a method.

Apex does not use double-quotes.  Everything must be single-quotes and you must escape single-quotes that are inside a String.

There is no such thing as an ArrayList - just use List.

SOQL doesn't use "&" - use "AND".

Try the code in the following post.  I apologize for any typos - I was unable to compile the code.

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Blog: GlynATheApexGuy.blogspot.com
Twitter: @GlynAtClosedWon
GlynAGlynA
<pre>
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() ] ) );
    }
}
</pre>
GlynAGlynA
Whoops!  One more thing I didn't catch.  On line 19 - there is no "toArray" method on List.  A List is an array.  Just pass it into the create method:

<pre>
conn.create( shares );
</pre>

-Glyn
WikWik
QueryResult result = conn.query( 'SELECT Id FROM Account WHERE GroupId = \'00Ge0000000GrKK\' AND Account_Type__c = \'SAM Managed\'' );
this line is trowing error.

Error Error: Compile Error: Invalid type: QueryResult at line 5 column 9
GlynAGlynA
I don't know what "conn" is, so I assumed that "QueryResult" was a class that you defined - whatever is returned by "conn.query".  If this was a "normal" query, I would write:

List<Account> result = (List<Account>) Database.query( 'SELECT Id FROM Account WHERE GroupId = \'00Ge0000000GrKK\' AND Account_Type__c = \'SAM Managed\'' );

Or just:

List<Account> result = [SELECT Id FROM Account WHERE GroupId = '00Ge0000000GrKK' AND Account_Type__c = 'SAM Managed'];

What is "conn" and what does "conn.query" return?  Do you define "QueryResult"?

-Glyn
nitesh gadkarinitesh gadkari
Public class AccShare{
	List<Account> AccountList = [SELECT Id FROM Account WHERE GroupId = '00Ge0000000GrKK' AND Account_Type__c = 'SAM Managed'];


  public void AccountShareMethod{

  	List<AccountShare> shares = new ArrayList<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);
	}

	if (!share.isEmpty())
		insert share;
  }
	
}