You need to sign in to do that
Don't have an account?
soql in for loop help pls
Also i am getting error "Save error: Invalid bind expression type of SOBJECT:Territory for column of type String"
against the highlighted soql .(Territory_vod__c in: relatedIdlist)
list<Id> terrIdList = new list<Id>();
Map<Id,Id> groupIdAndTerrIdMap = new Map<Id,Id>();
Map<Id,Set<Id>> terrIdAndSetOfNonTargetPractionerAccIdMap = new Map<Id,Set<Id>>();
Map<Id,Set<Id>> terrIdAndSetOfTargetPractionerAccIdMap = new Map<Id,Set<Id>>();
Map<Id,Set<Id>> terrIdAndSetOfPharmacyAccIdMap = new Map<Id,Set<Id>>();
Set<Id> finalaccIdSet = new Set<Id>();
for(Account_Statistic__c accStatic : districtAccList){
Id tID = Id.valueOf(accStatic.Territory_Id__c);
terrIdList.add(tId);
}
list<Territory> tlist = new list<Territory>();
tlist = [Select t.ParentTerritoryId, t.Id From Territory t where t.ParentTerritoryId in:terrIdList];
map<Id,list<Territory>> tmap = new map<Id,list<Territory>>{};
for(Territory t:tlist){
if(tmap.containsKey(t.ParentTerritoryId)){
tmap.get(t.ParentTerritoryId).add(t);
}else{
list<Territory> plist = new list<Territory>();
plist.add(t);
tmap.put(t.ParentTerritoryId,plist);
}
}
list<Territory>relatedIdlist= new list<Territory>();
for(Account_Statistic__c accStaticRec:districtAccList)
{
relatedIdlist = tmap.get(accStaticRec.Territory_Id__c);
if(relatedIdlist != NULL && relatedIdlist.size() > 0){
for(Group groupRec : [SELECT Id,RelatedId FROM Group WHERE RelatedId IN :relatedIdlist] ){
groupIdAndTerrIdMap.put(groupRec.Id,groupRec.RelatedId );
}
}
if(groupIdAndTerrIdMap != NULL && groupIdAndTerrIdMap.size() > 0){
for(AccountShare accShare : [SELECT Id,AccountId,Account.RecordTypeId,Account.RecordType.Name,Account.Target__c, UserOrGroupId FROM AccountShare WHERE (RowCause = 'Territory' OR RowCause = 'TerritoryManual') AND UserOrGroupId IN :groupIdAndTerrIdMap.keySet() AND (Account.RecordType.Name = 'Practitioner' OR Account.RecordType.Name = 'Pharmacy')] ){
if(accShare.Account.RecordType.Name == 'Practitioner'){
if(accShare.Account.Target__c){
Id RelatedId = groupIdAndTerrIdMap.get(accShare.UserOrGroupId );
if(!terrIdAndSetOfTargetPractionerAccIdMap.containsKey(RelatedId) )
terrIdAndSetOfTargetPractionerAccIdMap.put(RelatedId,new Set<Id>());
terrIdAndSetOfTargetPractionerAccIdMap.get(RelatedId).add(accShare.AccountId);
}else{
Id RelatedId = groupIdAndTerrIdMap.get(accShare.UserOrGroupId );
if(!terrIdAndSetOfNonTargetPractionerAccIdMap.containsKey(RelatedId) )
terrIdAndSetOfNonTargetPractionerAccIdMap.put(RelatedId,new Set<Id>());
terrIdAndSetOfNonTargetPractionerAccIdMap.get(RelatedId).add(accShare.AccountId);
}
}else if(accShare.Account.RecordType.Name == 'Pharmacy'){
Id RelatedId = groupIdAndTerrIdMap.get(accShare.UserOrGroupId );
if(!terrIdAndSetOfPharmacyAccIdMap.containsKey(RelatedId) )
terrIdAndSetOfPharmacyAccIdMap.put(RelatedId,new Set<Id>());
terrIdAndSetOfPharmacyAccIdMap.get(RelatedId).add(accShare.AccountId);
}
}
}
Set<Id> accIdSet = new Set<Id>();
if(accStaticRec.Account_Type__c == 'Target'){
accIdSet = terrIdAndSetOfTargetPractionerAccIdMap.get(accStaticRec.Territory_Id__c);
}else if(accStaticRec.Account_Type__c == 'Non-Target'){
accIdSet = terrIdAndSetOfNonTargetPractionerAccIdMap.get(accStaticRec.Territory_Id__c);
}else if(accStaticRec.Account_Type__c == 'Pharmacy'){
accIdSet = terrIdAndSetOfPharmacyAccIdMap.get(accStaticRec.Territory_Id__c);
}
if(accIdSet != NULL && accIdSet.size() > 0){
AggregateResult[] groupedResultCall = [SELECT Account_vod__c,count(Id) FROMCall2_vod__cWHERE Call_Date_vod__c >= :MonthStartDate AND Call_Date_vod__c <= :MonthEndDate AND Account_Type__c =:accStaticRec.Account_Type__c
AND Territory_vod__c in: relatedIdlist AND Status_vod__c = 'Submitted_vod' AND (Call_Type_vod__c != 'Call Only' OR Call_Type_vod__c != 'Event Only' OR Call_Type_vod__c != 'Event Detail')
AND Account_vod__c IN :accIdSet
AND Activity_Type__c <> 'Staff'
GROUP BY Account_vod__c ];
System.debug('::::groupedResultCall:::::'+groupedResultCall);
Map<Id,Integer> AccIdAndCountOfCallMap = new Map<Id,Integer>();
Integer totalCallsCount = 0;
for (AggregateResult ar : groupedResultCall) {
String accId = String.ValueOf(ar.get('Account_vod__c'));
finalaccIdSet.add(Id.valueOf(accId));
}
}
if(accStaticRec.Reach_Status__c == 'Reached'){
accStaticRec.Accounts__c = finalaccIdSet.size();
}else if(accStaticRec.Reach_Status__c == 'Not Reached'){
if(finalaccIdSet.size() == accIdSet.size()){
accStaticRec.Accounts__c = 0;
}else{
accStaticRec.Accounts__c = accIdSet.size() - finalaccIdSet.size();
}
accStaticListUpdate.add(accStaticRec);
String mapKey = accStaticRec.Territory_Id__c+accStaticRec.Account_Type__c+accStaticRec.Reach_Status__c;
ProcessedDistrictMap.put(mapKey,new Map<String,Decimal>());
ProcessedDistrictMap.get(mapKey).put('#Accounts',accStaticRec.Accounts__c);
}
}