This the batch takes all the Users on a User's UserAccountTeamMember and adds them to the to the AccountShare and AccountTeamMemeber objects for all the accounts the User owns. It's a long story as to why I had to write this.
global class ClientTeamBatch implements Database.batchable<sObject>, Database.Stateful{
global final String Query;
global ClientTeamBatch(){
Query = 'Select Id, OwnerID from Account where OwnerID in (select OwnerID from UserAccountTeamMember)';
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> AccountList){
List<UserAccountTeamMember> accList = new List<UserAccountTeamMember>();
List<AccountShare> AccntShares = new List<AccountShare>();
List<AccountTeamMember> ATMS = new List<AccountTeamMember>();
AccountShare a = New AccountShare( );
AccountTeamMember atm = New AccountTeamMember();
for (Account r : AccountList){
accList = [select Id, UserId, OwnerId, AccountAccessLevel, TeamMemberRole, ContactAccessLevel, CaseAccessLevel, OpportunityAccessLevel from UserAccountTeamMember where OwnerID = :r.OwnerID and UserId not in (Select UserOrGroupID from AccountShare where AccountID = :r.id)];
system.debug(accList);
if (AccList.size() > 0){
for ( UserAccountTeamMember u: accList){
// Add AccountShare Record
a.UserOrGroupId = u.UserID;
a.AccountId = r.ID;
a.AccountAccessLevel = u.AccountAccessLevel;
a.ContactAccessLevel = u.ContactAccessLevel;
a.CaseAccessLevel = u.CaseAccessLevel;
a.OpportunityAccessLevel = u.OpportunityAccessLevel;
AccntShares.add(a);
//Add AccountTeamMemeber Record
atm.UserId = u.UserID;
atm.AccountID = r.ID;
atm.TeamMemberRole = u.TeamMemberRole;
ATMS.add(atm);
}
}
}
if (accntShares.size() > 0){
try{
insert AccntShares;
insert ATMS;
}catch(DMLException e){
System.debug('DMLException:'+e.getMessage());
}
}
}
global void finish(Database.BatchableContext BC){
AsyncApexJob aaj = [select Id, ApexClassId, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors,
CompletedDate, MethodName, ExtendedStatus
from AsyncApexJob where Id=:BC.getJobId()];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
if (aaj.NumberofErrors>0){
//Send Mail on failure if specified in the custom setting.
mail.setToAddresses(new String[] { 'xxxx@xxxx.com' });
mail.setSubject('FAILED : ClientTeamBatch Job Run');
mail.setPlainTextBody('The batch Apex job processed ' + aaj.TotalJobItems +
' batches with '+ aaj.NumberOfErrors + ' failures. ExtendedStatus: ' + aaj.ExtendedStatus);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
This the batch takes all the Users on a User's UserAccountTeamMember and adds them to the to the AccountShare and AccountTeamMemeber objects for all the accounts the User owns. It's a long story as to why I had to write this.
global class ClientTeamBatch implements Database.batchable<sObject>, Database.Stateful{
global final String Query;
global ClientTeamBatch(){
Query = 'Select Id, OwnerID from Account where OwnerID in (select OwnerID from UserAccountTeamMember)';
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> AccountList){
List<UserAccountTeamMember> accList = new List<UserAccountTeamMember>();
List<AccountShare> AccntShares = new List<AccountShare>();
List<AccountTeamMember> ATMS = new List<AccountTeamMember>();
AccountShare a = New AccountShare( );
AccountTeamMember atm = New AccountTeamMember();
for (Account r : AccountList){
accList = [select Id, UserId, OwnerId, AccountAccessLevel, TeamMemberRole, ContactAccessLevel, CaseAccessLevel, OpportunityAccessLevel from UserAccountTeamMember where OwnerID = :r.OwnerID and UserId not in (Select UserOrGroupID from AccountShare where AccountID = :r.id)];
system.debug(accList);
if (AccList.size() > 0){
for ( UserAccountTeamMember u: accList){
// Add AccountShare Record
a.UserOrGroupId = u.UserID;
a.AccountId = r.ID;
a.AccountAccessLevel = u.AccountAccessLevel;
a.ContactAccessLevel = u.ContactAccessLevel;
a.CaseAccessLevel = u.CaseAccessLevel;
a.OpportunityAccessLevel = u.OpportunityAccessLevel;
AccntShares.add(a);
//Add AccountTeamMemeber Record
atm.UserId = u.UserID;
atm.AccountID = r.ID;
atm.TeamMemberRole = u.TeamMemberRole;
ATMS.add(atm);
}
}
}
if (accntShares.size() > 0){
try{
insert AccntShares;
insert ATMS;
}catch(DMLException e){
System.debug('DMLException:'+e.getMessage());
}
}
}
global void finish(Database.BatchableContext BC){
AsyncApexJob aaj = [select Id, ApexClassId, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors,
CompletedDate, MethodName, ExtendedStatus
from AsyncApexJob where Id=:BC.getJobId()];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
if (aaj.NumberofErrors>0){
//Send Mail on failure if specified in the custom setting.
mail.setToAddresses(new String[] { 'xxxx@xxxx.com' });
mail.setSubject('FAILED : ClientTeamBatch Job Run');
mail.setPlainTextBody('The batch Apex job processed ' + aaj.TotalJobItems +
' batches with '+ aaj.NumberOfErrors + ' failures. ExtendedStatus: ' + aaj.ExtendedStatus);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
This the batch takes all the Users on a User's UserAccountTeamMember and adds them to the to the AccountShare and AccountTeamMemeber objects for all the accounts the User owns. It's a long story as to why I had to write this.
All Answers
This the batch takes all the Users on a User's UserAccountTeamMember and adds them to the to the AccountShare and AccountTeamMemeber objects for all the accounts the User owns. It's a long story as to why I had to write this.
global Database.Querylocator start(Database.BatchableContext bc){
String query='select Name,Industry from Account';
return database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc , List<Account> scope) {
List<Account> AccountToInsert = new List<Account>{};
for(Account a :scope)
{
Account newAcc = new Account();
System.debug('Account Industry[' + a.Industry + '], Name[' + a.name + ']');
newAcc.Name=a.Name + ' ' + 'Hello' ;
newAcc.Industry =a.Industry;
AccountToInsert.add(newAcc);
}
insert AccountToInsert;
}
global void finish(Database.BatchableContext bc) {
}