You need to sign in to do that
Don't have an account?
Wik
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()]));
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()]));
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.
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
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>
<pre>
conn.create( shares );
</pre>
-Glyn
this line is trowing error.
Error Error: Compile Error: Invalid type: QueryResult at line 5 column 9
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