You need to sign in to do that
Don't have an account?
Mohan kumar 167
Accountteam member was not inserted as opportunity team member after inserting the AccountShare
Hi,
I am unable to add the opportunity team member after AccountShare is inserted.After adding the account team member to account team, We have custom button after click on the button we have to share the account associated child acccounts and add account team member as opportunity team members after inserting the accounrShare. Following is my code. Please tell me the Issue in the below code
public without sharing Class RefreshAccountTeamCtrl{
Id accountId;
static Integer delRecs;
public RefreshAccountTeamCtrl(ApexPages.StandardController controller){
accountId = ApexPages.CurrentPage().getParameters().get('Id');
system.debug('DEBUG:Accountid '+accountId);
}
public PageReference refreshTeam(){
List<AccountTeamMember> accTeamLevelAList = [select id,
accountid,
teammemberrole,
accountaccesslevel,
userid
from AccountTeamMember
where accountid = :accountId and lastmodifieddate > yesterday];
system.debug('DEBUG:account team map size'+accTeamLevelAList.size());
Map<Id,AccountTeamMember> accTeamMap = new Map<Id,AccountTeamMember>();
if(accTeamLevelAList != null){
for(AccountTeamMember acc : accTeamLevelAList){
accTeamMap.put(acc.userid,acc);
}
}
system.debug('DEBUG:account team map '+accTeamMap);
Map<Id,Account> acc1Map =new Map<Id,Account>([select id,
parentid
from Account
where Parent__c =:accountId ]); // Get all the child accounts of level A
if(acc1Map.isEmpty() || acc1Map == null){
return null;
}
// Key is the id of each Level B account and
Map<Id, Account> acc2Map = new Map<Id, Account>([select id ,
parentid
from Account
where Parent__c IN : acc1Map.keySet()
]);
if(acc2Map.isEmpty() || acc2Map == null){
return null;
}
Map<Id, Account> acc3Map = new Map<Id, Account>([select id ,
parentid,ownerid
from Account
where Parent__c IN: acc2Map.keySet()
Order by createdDate]);
if(acc3Map.isEmpty() || acc3Map == null){
return null;
}
List<Id> accList = new List<Id>();
accList.addAll(acc3Map.keySet());
List<AccountTeamMember> accTeamDelList = new List<AccountTeamMember>();
system.debug('DEBUG: no of deleted records '+delrecs);
system.debug('DEBUG:before check delta '+accTeamMap.keySet());
// Check delta, if the inserted records are already there in the location accounts
// remove them from the map
system.debug('DEBUG: delta account '+accList[0]);
//accTeamMap = checkDelta(accList[0],accTeamMap); //commented as per case #00286815
system.debug('DEBUG:After check of delta '+accTeamMap.keySet());
//if(delrecs == 0 && accTeamMap.isEmpty()){
if( accTeamMap.isEmpty()){
// ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'No Records Added'));
return redirect();
}
//Integer size = delrecs + acc3Map.size()*accTeamMap.size()*2;
Integer size = acc3Map.size()*accTeamMap.size()*2;
system.debug('DEBUG: acc3Map size '+size);
// If the no of records are greater than 2000 to be inserted or deleted send it to batch
if(size > 2000){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Batch is currently in progress, Please Click on "Back" to go to the previous page. You will be notified once the batch is executed'));
runBatch(acc2Map.keySet(),accountId,accTeamMap,accTeamDelList);
return null;
}
/*if(accTeamDelList != null && !accTeamDelList.isEmpty()){
try{
Delete accTeamDelList;
}
Catch(Exception exe){
system.debug('DEBUG : exception caught while deleting '+exe.getMessage());
}
}*/
if(accTeamMap.isEmpty()){
return redirect();
}
// getting all the share access records for acc team members.
Map<Id,AccessLevel> accslevelMap = getSharerecords(accountId,accTeamMap.keySet());
List<String> accountShareJSONList = new List<String>();
//List<AccountShare> insertShareList = new List<AccountShare>();
List<AccountTeamMember> insertTeamList = new List<AccountTeamMember>();
for(Id userId : accslevelMap.keySet()){
for(Id accId : acc3Map.keySet()){
String access;
AccountTeamMember acc = new AccountTeamMember(userid = userId,
AccountId = accId,
TeamMemberRole = accTeamMap.get(userid).TeamMemberRole);
// AccountAccessLevel = accslevelMap.get(userid).accLevel);
insertTeamList.add(acc);
if(userId != acc3Map.get(accId).ownerId){
if(accslevelMap.get(userid).accLevel == 'Read' || accslevelMap.get(userid).accLevel == 'Edit'){
access = accslevelMap.get(userid).accLevel;
}
else{
access = 'Edit';
}
AccountShare accShare = new AccountShare(UserOrGroupId = userId,
AccountId = accId,
AccountAccessLevel = access ,
OpportunityAccessLevel = accslevelMap.get(userid).oppLevel,
ContactAccessLevel = accslevelMap.get(userid).conLevel,
CaseAccessLevel = accslevelMap.get(userid).caslevel);
accountShareJSONList.add(JSON.serialize(accShare));
//insertShareList.add(accShare);
}
}
}
//system.debug('DEBUG:insertSharelist '+insertShareList);
system.debug('DEBUG:isnertTeamList'+insertTeamList);
if(insertTeamList != null && !insertTeamList.isEmpty()){
insert insertTeamList;
}
if(accountShareJSONList != null && !accountShareJSONList.isEmpty() && !System.isBatch()){
doFutureCall(accountShareJSONList);
}
//insert insertShareList;
PageReference pg = new PageReference('/'+accountId);
//pg.setRedirect(true);
return redirect();
}
@future
static void doFutureCall(List<String> accountShareJSONList){
List<AccountShare> accShareList = new List<AccountShare>();
for(String str: accountShareJSONList){
AccountShare acc = (AccountShare)JSON.deserialize(str, AccountShare.class);
accShareList.add(acc);
}
if(accShareList != null && !accShareList.isEmpty()){
Database.SaveResult[] srshareList = Database.insert(accShareList,false);
for (Database.SaveResult sr : srshareList) {
if (!sr.isSuccess()) {
for(Database.Error err : sr.getErrors()) {
System.debug('Share records failed with following errors '+err.getMessage());
}
}
}
}
}
// Deletes all the extra accountteam members which are extras in location accounts
/*public static List<AccountTeamMember> deleteExtraRecs(Set<Id> accIdSet, Set<Id> accTeamIdSet){
if(accIdSet.isEmpty() && accTeamIdSet.isEmpty()){
return null;
}
List<AccountTeamMember> accTeamLocDelList = [select id, userid
from AccountTeamMember
where accountid IN : accIdSet
AND userid NOT IN :accTeamIdSet];
system.debug('DEBUG:acc team location List '+accTeamLocDelList);
delrecs = accTeamLocDelList.size();
if(!accTeamLocDelList.isEmpty() && accTeamLocDelList != null){
return accTeamLocDelList;
}
else{
return accTeamLocDelList;
}
}*/
// Gets the delta from the first account and check if anything have to be removed from the account team map
public static Map<Id, AccountTeamMember> checkDelta(Id accId, Map<Id, AccountTeamMember> accTeamMap){
if(accId == null && accTeamMap.isEmpty()){
return null;
}
List<AccountTeamMember> accTeammemberList = [select id,
userid
from AccountTeamMember
where accountid = : accId ];
system.debug('DEBUG:account team member List '+accTeammemberList);
if(accTeammemberList != null){
for(AccountTeamMember acct : accTeammemberList){
if(accTeamMap.keySet().contains(acct.userId)){
accTeamMap.remove(acct.userid);
}
}
}
system.debug('DEBUG:accTeamMap in delta '+accTeamMap);
return accTeamMap;
}
// Gets all the share records of each teammember along with the access
public static Map<Id,AccessLevel> getSharerecords(Id accId, Set<Id> acTeamsIdSet){
system.debug('DEBUG:acTeamIdSet'+acTeamsIdSet);
system.debug('DEBUG:accid'+accId);
List<AccountShare> accShareList = [select id ,
accountid,
userorgroupId ,
accountaccesslevel,
opportunityaccesslevel,
caseaccesslevel,
contactaccesslevel
from AccountShare
where userorgroupid IN : acTeamsIdSet and accountid = : accId];
Map<Id,AccessLevel> accesslevelMap = new Map<Id, AccessLevel>(); // Key is the userId and Values is a wrapper which has all access levels
for(AccountShare accShare : accShareList ){
//wrpList.add(new AccessLevel(accShare.AccountAccessLevel, accShare.ContactAccessLevel,accShare.OpportunityAccessLevel,accShare.caseAccessLevel));
accesslevelMap.put(accshare.userorgroupid, new AccessLevel(accShare.AccountAccessLevel, accShare.ContactAccessLevel,accShare.OpportunityAccessLevel,accShare.caseAccessLevel));
//wrpList = new List<AccessLevel>();
}
system.debug('DEBUG:accessLevelMap '+accesslevelMap);
return accesslevelMap;
}
public class AccessLevel{
public String accLevel;
public String oppLevel;
public String conLevel;
public String caslevel;
public AccessLevel(String accountaccessLevel, String contactAccessLevel, String opportunityaccessLvel,String caseAccesslevel){
this.accLevel = accountaccessLevel ;
this.conLevel = contactAccessLevel ;
this.oppLevel = opportunityaccessLvel;
this.casLevel = caseAccesslevel ;
}
}
public PageReference rediRect(){
PageReference pg = new PageReference('/'+accountid);
pg.setRedirect(true);
return pg;
}
public void runBatch(Set<Id> accIdSet,
Id accid, Map<Id,
AccountTeamMember> accTmMap,
List<AccountTeamMember> accTeamDelList ){
RefreshAccountTeamBatch rAccTeam = new RefreshAccountTeamBatch(accIdSet,accid,accTmMap,accTeamDelList);
Database.executeBatch(rAccTeam,100);
}
}
I am unable to add the opportunity team member after AccountShare is inserted.After adding the account team member to account team, We have custom button after click on the button we have to share the account associated child acccounts and add account team member as opportunity team members after inserting the accounrShare. Following is my code. Please tell me the Issue in the below code
public without sharing Class RefreshAccountTeamCtrl{
Id accountId;
static Integer delRecs;
public RefreshAccountTeamCtrl(ApexPages.StandardController controller){
accountId = ApexPages.CurrentPage().getParameters().get('Id');
system.debug('DEBUG:Accountid '+accountId);
}
public PageReference refreshTeam(){
List<AccountTeamMember> accTeamLevelAList = [select id,
accountid,
teammemberrole,
accountaccesslevel,
userid
from AccountTeamMember
where accountid = :accountId and lastmodifieddate > yesterday];
system.debug('DEBUG:account team map size'+accTeamLevelAList.size());
Map<Id,AccountTeamMember> accTeamMap = new Map<Id,AccountTeamMember>();
if(accTeamLevelAList != null){
for(AccountTeamMember acc : accTeamLevelAList){
accTeamMap.put(acc.userid,acc);
}
}
system.debug('DEBUG:account team map '+accTeamMap);
Map<Id,Account> acc1Map =new Map<Id,Account>([select id,
parentid
from Account
where Parent__c =:accountId ]); // Get all the child accounts of level A
if(acc1Map.isEmpty() || acc1Map == null){
return null;
}
// Key is the id of each Level B account and
Map<Id, Account> acc2Map = new Map<Id, Account>([select id ,
parentid
from Account
where Parent__c IN : acc1Map.keySet()
]);
if(acc2Map.isEmpty() || acc2Map == null){
return null;
}
Map<Id, Account> acc3Map = new Map<Id, Account>([select id ,
parentid,ownerid
from Account
where Parent__c IN: acc2Map.keySet()
Order by createdDate]);
if(acc3Map.isEmpty() || acc3Map == null){
return null;
}
List<Id> accList = new List<Id>();
accList.addAll(acc3Map.keySet());
List<AccountTeamMember> accTeamDelList = new List<AccountTeamMember>();
system.debug('DEBUG: no of deleted records '+delrecs);
system.debug('DEBUG:before check delta '+accTeamMap.keySet());
// Check delta, if the inserted records are already there in the location accounts
// remove them from the map
system.debug('DEBUG: delta account '+accList[0]);
//accTeamMap = checkDelta(accList[0],accTeamMap); //commented as per case #00286815
system.debug('DEBUG:After check of delta '+accTeamMap.keySet());
//if(delrecs == 0 && accTeamMap.isEmpty()){
if( accTeamMap.isEmpty()){
// ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'No Records Added'));
return redirect();
}
//Integer size = delrecs + acc3Map.size()*accTeamMap.size()*2;
Integer size = acc3Map.size()*accTeamMap.size()*2;
system.debug('DEBUG: acc3Map size '+size);
// If the no of records are greater than 2000 to be inserted or deleted send it to batch
if(size > 2000){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Batch is currently in progress, Please Click on "Back" to go to the previous page. You will be notified once the batch is executed'));
runBatch(acc2Map.keySet(),accountId,accTeamMap,accTeamDelList);
return null;
}
/*if(accTeamDelList != null && !accTeamDelList.isEmpty()){
try{
Delete accTeamDelList;
}
Catch(Exception exe){
system.debug('DEBUG : exception caught while deleting '+exe.getMessage());
}
}*/
if(accTeamMap.isEmpty()){
return redirect();
}
// getting all the share access records for acc team members.
Map<Id,AccessLevel> accslevelMap = getSharerecords(accountId,accTeamMap.keySet());
List<String> accountShareJSONList = new List<String>();
//List<AccountShare> insertShareList = new List<AccountShare>();
List<AccountTeamMember> insertTeamList = new List<AccountTeamMember>();
for(Id userId : accslevelMap.keySet()){
for(Id accId : acc3Map.keySet()){
String access;
AccountTeamMember acc = new AccountTeamMember(userid = userId,
AccountId = accId,
TeamMemberRole = accTeamMap.get(userid).TeamMemberRole);
// AccountAccessLevel = accslevelMap.get(userid).accLevel);
insertTeamList.add(acc);
if(userId != acc3Map.get(accId).ownerId){
if(accslevelMap.get(userid).accLevel == 'Read' || accslevelMap.get(userid).accLevel == 'Edit'){
access = accslevelMap.get(userid).accLevel;
}
else{
access = 'Edit';
}
AccountShare accShare = new AccountShare(UserOrGroupId = userId,
AccountId = accId,
AccountAccessLevel = access ,
OpportunityAccessLevel = accslevelMap.get(userid).oppLevel,
ContactAccessLevel = accslevelMap.get(userid).conLevel,
CaseAccessLevel = accslevelMap.get(userid).caslevel);
accountShareJSONList.add(JSON.serialize(accShare));
//insertShareList.add(accShare);
}
}
}
//system.debug('DEBUG:insertSharelist '+insertShareList);
system.debug('DEBUG:isnertTeamList'+insertTeamList);
if(insertTeamList != null && !insertTeamList.isEmpty()){
insert insertTeamList;
}
if(accountShareJSONList != null && !accountShareJSONList.isEmpty() && !System.isBatch()){
doFutureCall(accountShareJSONList);
}
//insert insertShareList;
PageReference pg = new PageReference('/'+accountId);
//pg.setRedirect(true);
return redirect();
}
@future
static void doFutureCall(List<String> accountShareJSONList){
List<AccountShare> accShareList = new List<AccountShare>();
for(String str: accountShareJSONList){
AccountShare acc = (AccountShare)JSON.deserialize(str, AccountShare.class);
accShareList.add(acc);
}
if(accShareList != null && !accShareList.isEmpty()){
Database.SaveResult[] srshareList = Database.insert(accShareList,false);
for (Database.SaveResult sr : srshareList) {
if (!sr.isSuccess()) {
for(Database.Error err : sr.getErrors()) {
System.debug('Share records failed with following errors '+err.getMessage());
}
}
}
}
}
// Deletes all the extra accountteam members which are extras in location accounts
/*public static List<AccountTeamMember> deleteExtraRecs(Set<Id> accIdSet, Set<Id> accTeamIdSet){
if(accIdSet.isEmpty() && accTeamIdSet.isEmpty()){
return null;
}
List<AccountTeamMember> accTeamLocDelList = [select id, userid
from AccountTeamMember
where accountid IN : accIdSet
AND userid NOT IN :accTeamIdSet];
system.debug('DEBUG:acc team location List '+accTeamLocDelList);
delrecs = accTeamLocDelList.size();
if(!accTeamLocDelList.isEmpty() && accTeamLocDelList != null){
return accTeamLocDelList;
}
else{
return accTeamLocDelList;
}
}*/
// Gets the delta from the first account and check if anything have to be removed from the account team map
public static Map<Id, AccountTeamMember> checkDelta(Id accId, Map<Id, AccountTeamMember> accTeamMap){
if(accId == null && accTeamMap.isEmpty()){
return null;
}
List<AccountTeamMember> accTeammemberList = [select id,
userid
from AccountTeamMember
where accountid = : accId ];
system.debug('DEBUG:account team member List '+accTeammemberList);
if(accTeammemberList != null){
for(AccountTeamMember acct : accTeammemberList){
if(accTeamMap.keySet().contains(acct.userId)){
accTeamMap.remove(acct.userid);
}
}
}
system.debug('DEBUG:accTeamMap in delta '+accTeamMap);
return accTeamMap;
}
// Gets all the share records of each teammember along with the access
public static Map<Id,AccessLevel> getSharerecords(Id accId, Set<Id> acTeamsIdSet){
system.debug('DEBUG:acTeamIdSet'+acTeamsIdSet);
system.debug('DEBUG:accid'+accId);
List<AccountShare> accShareList = [select id ,
accountid,
userorgroupId ,
accountaccesslevel,
opportunityaccesslevel,
caseaccesslevel,
contactaccesslevel
from AccountShare
where userorgroupid IN : acTeamsIdSet and accountid = : accId];
Map<Id,AccessLevel> accesslevelMap = new Map<Id, AccessLevel>(); // Key is the userId and Values is a wrapper which has all access levels
for(AccountShare accShare : accShareList ){
//wrpList.add(new AccessLevel(accShare.AccountAccessLevel, accShare.ContactAccessLevel,accShare.OpportunityAccessLevel,accShare.caseAccessLevel));
accesslevelMap.put(accshare.userorgroupid, new AccessLevel(accShare.AccountAccessLevel, accShare.ContactAccessLevel,accShare.OpportunityAccessLevel,accShare.caseAccessLevel));
//wrpList = new List<AccessLevel>();
}
system.debug('DEBUG:accessLevelMap '+accesslevelMap);
return accesslevelMap;
}
public class AccessLevel{
public String accLevel;
public String oppLevel;
public String conLevel;
public String caslevel;
public AccessLevel(String accountaccessLevel, String contactAccessLevel, String opportunityaccessLvel,String caseAccesslevel){
this.accLevel = accountaccessLevel ;
this.conLevel = contactAccessLevel ;
this.oppLevel = opportunityaccessLvel;
this.casLevel = caseAccesslevel ;
}
}
public PageReference rediRect(){
PageReference pg = new PageReference('/'+accountid);
pg.setRedirect(true);
return pg;
}
public void runBatch(Set<Id> accIdSet,
Id accid, Map<Id,
AccountTeamMember> accTmMap,
List<AccountTeamMember> accTeamDelList ){
RefreshAccountTeamBatch rAccTeam = new RefreshAccountTeamBatch(accIdSet,accid,accTmMap,accTeamDelList);
Database.executeBatch(rAccTeam,100);
}
}