trigger UserTrigger on User (after insert) {
Map<Id,Id> userAccMap=new Map<Id,Id>();
Id novatechprofileId;
if(trigger.isInsert && trigger.isafter){
try{
novatechprofileId=[SELECT Id FROM Profile WHERE NAME='Novatech Customer Community Plus Login User' limit 1].Id;
}catch(QueryException qe){
system.debug('unable to get profile for novatech due to '+qe);
}
//query new user details
for(User u:[SELECT Id, ProfileId, IsActive,ContactId,AccountId FROM User WHERE Id IN:Trigger.newMap.keyset()]){
//get user profile
if(novatechprofileId!=null && novatechprofileId==u.ProfileId){
if(u.AccountId!=null)
userAccMap.put(u.Id,u.AccountId);
}
}
}
//change the user
UserTriggerHelper.shareRecords(userAccMap);
}
here is the helper class
public class UserTriggerHelper {
@future
public static void shareRecords(Map<Id,Id> userAccMap){
System.debug('userAccMap:'+userAccMap);
//get accounts from contacts
List<Account> parentaccs=[SELECT Id,parentId from Account WHERE Id IN:userAccMap.values()];
List<Account> childaccs=[SELECT Id,parentId FROM Account WHERE ParentId IN:userAccMap.values()];
childaccs.addAll(parentaccs);
System.debug('child acclist: '+childaccs);
List<AccountShare> outacclist=new List<AccountShare>();
//Share Account access to it's children
for(Id userId:userAccMap.keySet()){
//get all children
for(Account child:childaccs){
//check if accid is belongs to useraccmap
if(child.parentid!=null && (userAccMap.get(userId)==child.parentid)
|| (userAccMap.get(userId)==child.Id)) {
outacclist.add(new AccountShare(AccountId=child.Id,
UserOrGroupId=userId,
AccountAccessLevel='Edit',
OpportunityAccessLevel='None',
CaseAccessLevel='Edit',
ContactAccessLevel='Read'));
}
}
}
System.debug('sharedlist: '+outacclist);
try{
insert outacclist;
}catch(Exception e){
System.debug('Unable to share records due to '+e+'\n'+e.getStackTraceString());
}
}
}
Map<Id,Id> userAccMap=new Map<Id,Id>();
Id novatechprofileId;
if(trigger.isInsert && trigger.isafter){
try{
novatechprofileId=[SELECT Id FROM Profile WHERE NAME='Novatech Customer Community Plus Login User' limit 1].Id;
}catch(QueryException qe){
system.debug('unable to get profile for novatech due to '+qe);
}
//query new user details
for(User u:[SELECT Id, ProfileId, IsActive,ContactId,AccountId FROM User WHERE Id IN:Trigger.newMap.keyset()]){
//get user profile
if(novatechprofileId!=null && novatechprofileId==u.ProfileId){
if(u.AccountId!=null)
userAccMap.put(u.Id,u.AccountId);
}
}
}
//change the user
UserTriggerHelper.shareRecords(userAccMap);
}
here is the helper class
public class UserTriggerHelper {
@future
public static void shareRecords(Map<Id,Id> userAccMap){
System.debug('userAccMap:'+userAccMap);
//get accounts from contacts
List<Account> parentaccs=[SELECT Id,parentId from Account WHERE Id IN:userAccMap.values()];
List<Account> childaccs=[SELECT Id,parentId FROM Account WHERE ParentId IN:userAccMap.values()];
childaccs.addAll(parentaccs);
System.debug('child acclist: '+childaccs);
List<AccountShare> outacclist=new List<AccountShare>();
//Share Account access to it's children
for(Id userId:userAccMap.keySet()){
//get all children
for(Account child:childaccs){
//check if accid is belongs to useraccmap
if(child.parentid!=null && (userAccMap.get(userId)==child.parentid)
|| (userAccMap.get(userId)==child.Id)) {
outacclist.add(new AccountShare(AccountId=child.Id,
UserOrGroupId=userId,
AccountAccessLevel='Edit',
OpportunityAccessLevel='None',
CaseAccessLevel='Edit',
ContactAccessLevel='Read'));
}
}
}
System.debug('sharedlist: '+outacclist);
try{
insert outacclist;
}catch(Exception e){
System.debug('Unable to share records due to '+e+'\n'+e.getStackTraceString());
}
}
}
No comments:
Post a Comment