- Narmadha Chandrasekar 4
- NEWBIE
- 10 Points
- Member since 2016
-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
3Questions
-
1Replies
too many soql queries 201 batch class
This is my batch class and im getting System.LimitException: Too many SOQL queries: 201 on line 59
global class ShipToCountBatch implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
recordtype RECORD_TYPE = [select id from recordtype where SobjectType = 'Account' and name = 'Customer Sold To' limit 1];
String query = 'SELECT Id, Name FROM Account where Active__c != \'X\'AND RecordTypeId ='+'\''+ RECORD_TYPE.Id+'\'' ;
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope)
{
List<Account> lAccountsToUpdate = new List<Account>{};
List<Account> lShiptoAccountsToUpdate = new List<Account>{};
for ( Account a : scope)
{
List<Account> al = new List<Account>{};
List<Id> accountIds= new List<Id>();
al = [SELECT Id, SAP_Account_Id__c FROM Account where ParentId = :a.Id];
if( al.size()> 0 ){
for ( Integer i = 0 ; i < al.size(); i++ )
{
accountIds.add(al[i].Id);
}
accountIds.add(a.Id);
}
Account oAccountToUpdate = new Account();
oAccountToUpdate = a;
Sobject so1 = [SELECT COUNT(Id) shipToCount from Account where Id = :accountIds];
Sobject so2 = [SELECT SUM(Adjusted_Net_Invoice_Price__c) currInvoicePrice,
SUM(Customer_Margin_CM1__c) currCM,
SUM(Quantity_Converted__c) currVolume
from Sales_History__c
where Sales_Account__r.Sold_To_Account__r.Id = :accountIds
and Division__c != 'CATALYST'
and Current_Year_Invoice__c = 'Y'];
Sobject so3 = [SELECT SUM(Adjusted_Net_Invoice_Price__c) prevInvoicePrice,
SUM(Customer_Margin_CM1__c) prevCM,
SUM(Quantity_Converted__c) prevVolume
from Sales_History__c
where Sales_Account__r.Sold_To_Account__r.Id = :accountIds
and Division__c != 'CATALYST'
and Previous_Year_Invoice__c = 'Y'];
oAccountToUpdate.Ship_To_Accounts__c = (Integer)so1.get('shipToCount');
if (!String.isBlank(String.valueOf(so2.get('currInvoicePrice')) )) {
oAccountToUpdate.YTD_LYB_Sales__c = Decimal.valueOf(String.valueOf(so2.get('currInvoicePrice'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so2.get('currCM')) )) {
oAccountToUpdate.YTD_LYB_CM1__c = Decimal.valueOf(String.valueOf(so2.get('currCM'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so2.get('currVolume')) )) {
oAccountToUpdate.YTD_LYB_Volume__c = Decimal.valueOf(String.valueOf(so2.get('currVolume'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so3.get('prevCM')))) {
oAccountToUpdate.Prior_Year_LYB_CM1__c = Decimal.valueOf(String.valueOf(so3.get('prevCM'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so3.get('prevVolume')) )) {
oAccountToUpdate.Prior_Year_LYB_Volume__c = Decimal.valueOf(String.valueOf(so3.get('prevVolume'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so3.get('prevInvoicePrice')) )) {
oAccountToUpdate.Prior_Year_LYB_Sales__c = Decimal.valueOf(String.valueOf(so3.get('prevInvoicePrice'))).setScale(2);
}
lAccountsToUpdate.add(oAccountToUpdate);
List<Account> shipToAccountList = [SELECT Id,ParentId,Name,Parent.Name,YTD_LYB_CM1__c,YTD_LYB_Vol_KGS__c
FROM Account
WHERE ParentId = :a.Id];
if(shipToAccountList.size()>0){
for (integer i=0;i<shipToAccountList.size();i++)
{
Account shiptoAccountToUpdate = new Account();
shiptoAccountToUpdate = shipToAccountList[i];
Sobject so4 = [SELECT SUM(Adjusted_Net_Invoice_Price__c) currInvoicePrice,
SUM(Customer_Margin_CM1__c) currCM,
SUM(Quantity_Converted__c) currVolume
from Sales_History__c
where Ship_To_Account__c = :shipToAccountList[i].Id
and Division__c != 'CATALYST'
and Current_Year_Invoice__c = 'Y'];
List<Sales_History__c> listsh = [SELECT Id from Sales_History__c where Ship_To_Account__c = :accountIds];
Sobject so5 = [SELECT SUM(Adjusted_Net_Invoice_Price__c) prevInvoicePrice,
SUM(Customer_Margin_CM1__c) prevCM,
SUM(Quantity_Converted__c) prevVolume
from Sales_History__c
where Ship_To_Account__c = :shipToAccountList[i].Id
and Division__c != 'CATALYST'
and Previous_Year_Invoice__c = 'Y'];
if (!String.isBlank(String.valueOf(so4.get('currInvoicePrice')) )) {
shiptoAccountToUpdate.YTD_LYB_Sales__c = Decimal.valueOf(String.valueOf(so4.get('currInvoicePrice'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so4.get('currCM')) )) {
shiptoAccountToUpdate.YTD_LYB_CM1__c = Decimal.valueOf(String.valueOf(so4.get('currCM'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so4.get('currVolume')) )) {
shiptoAccountToUpdate.YTD_LYB_Volume__c = Decimal.valueOf(String.valueOf(so4.get('currVolume'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so5.get('prevCM')))) {
shiptoAccountToUpdate.Prior_Year_LYB_CM1__c = Decimal.valueOf(String.valueOf(so5.get('prevCM'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so5.get('prevVolume')) )) {
shiptoAccountToUpdate.Prior_Year_LYB_Volume__c = Decimal.valueOf(String.valueOf(so5.get('prevVolume'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so5.get('prevInvoicePrice')) )) {
shiptoAccountToUpdate.Prior_Year_LYB_Sales__c = Decimal.valueOf(String.valueOf(so5.get('prevInvoicePrice'))).setScale(2);
}
lShiptoAccountsToUpdate.add(shiptoAccountToUpdate);
}
}
}
update lShiptoAccountsToUpdate;
update lAccountsToUpdate;
}
global void finish(Database.BatchableContext BC)
{
}
}
global class ShipToCountBatch implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
recordtype RECORD_TYPE = [select id from recordtype where SobjectType = 'Account' and name = 'Customer Sold To' limit 1];
String query = 'SELECT Id, Name FROM Account where Active__c != \'X\'AND RecordTypeId ='+'\''+ RECORD_TYPE.Id+'\'' ;
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope)
{
List<Account> lAccountsToUpdate = new List<Account>{};
List<Account> lShiptoAccountsToUpdate = new List<Account>{};
for ( Account a : scope)
{
List<Account> al = new List<Account>{};
List<Id> accountIds= new List<Id>();
al = [SELECT Id, SAP_Account_Id__c FROM Account where ParentId = :a.Id];
if( al.size()> 0 ){
for ( Integer i = 0 ; i < al.size(); i++ )
{
accountIds.add(al[i].Id);
}
accountIds.add(a.Id);
}
Account oAccountToUpdate = new Account();
oAccountToUpdate = a;
Sobject so1 = [SELECT COUNT(Id) shipToCount from Account where Id = :accountIds];
Sobject so2 = [SELECT SUM(Adjusted_Net_Invoice_Price__c) currInvoicePrice,
SUM(Customer_Margin_CM1__c) currCM,
SUM(Quantity_Converted__c) currVolume
from Sales_History__c
where Sales_Account__r.Sold_To_Account__r.Id = :accountIds
and Division__c != 'CATALYST'
and Current_Year_Invoice__c = 'Y'];
Sobject so3 = [SELECT SUM(Adjusted_Net_Invoice_Price__c) prevInvoicePrice,
SUM(Customer_Margin_CM1__c) prevCM,
SUM(Quantity_Converted__c) prevVolume
from Sales_History__c
where Sales_Account__r.Sold_To_Account__r.Id = :accountIds
and Division__c != 'CATALYST'
and Previous_Year_Invoice__c = 'Y'];
oAccountToUpdate.Ship_To_Accounts__c = (Integer)so1.get('shipToCount');
if (!String.isBlank(String.valueOf(so2.get('currInvoicePrice')) )) {
oAccountToUpdate.YTD_LYB_Sales__c = Decimal.valueOf(String.valueOf(so2.get('currInvoicePrice'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so2.get('currCM')) )) {
oAccountToUpdate.YTD_LYB_CM1__c = Decimal.valueOf(String.valueOf(so2.get('currCM'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so2.get('currVolume')) )) {
oAccountToUpdate.YTD_LYB_Volume__c = Decimal.valueOf(String.valueOf(so2.get('currVolume'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so3.get('prevCM')))) {
oAccountToUpdate.Prior_Year_LYB_CM1__c = Decimal.valueOf(String.valueOf(so3.get('prevCM'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so3.get('prevVolume')) )) {
oAccountToUpdate.Prior_Year_LYB_Volume__c = Decimal.valueOf(String.valueOf(so3.get('prevVolume'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so3.get('prevInvoicePrice')) )) {
oAccountToUpdate.Prior_Year_LYB_Sales__c = Decimal.valueOf(String.valueOf(so3.get('prevInvoicePrice'))).setScale(2);
}
lAccountsToUpdate.add(oAccountToUpdate);
List<Account> shipToAccountList = [SELECT Id,ParentId,Name,Parent.Name,YTD_LYB_CM1__c,YTD_LYB_Vol_KGS__c
FROM Account
WHERE ParentId = :a.Id];
if(shipToAccountList.size()>0){
for (integer i=0;i<shipToAccountList.size();i++)
{
Account shiptoAccountToUpdate = new Account();
shiptoAccountToUpdate = shipToAccountList[i];
Sobject so4 = [SELECT SUM(Adjusted_Net_Invoice_Price__c) currInvoicePrice,
SUM(Customer_Margin_CM1__c) currCM,
SUM(Quantity_Converted__c) currVolume
from Sales_History__c
where Ship_To_Account__c = :shipToAccountList[i].Id
and Division__c != 'CATALYST'
and Current_Year_Invoice__c = 'Y'];
List<Sales_History__c> listsh = [SELECT Id from Sales_History__c where Ship_To_Account__c = :accountIds];
Sobject so5 = [SELECT SUM(Adjusted_Net_Invoice_Price__c) prevInvoicePrice,
SUM(Customer_Margin_CM1__c) prevCM,
SUM(Quantity_Converted__c) prevVolume
from Sales_History__c
where Ship_To_Account__c = :shipToAccountList[i].Id
and Division__c != 'CATALYST'
and Previous_Year_Invoice__c = 'Y'];
if (!String.isBlank(String.valueOf(so4.get('currInvoicePrice')) )) {
shiptoAccountToUpdate.YTD_LYB_Sales__c = Decimal.valueOf(String.valueOf(so4.get('currInvoicePrice'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so4.get('currCM')) )) {
shiptoAccountToUpdate.YTD_LYB_CM1__c = Decimal.valueOf(String.valueOf(so4.get('currCM'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so4.get('currVolume')) )) {
shiptoAccountToUpdate.YTD_LYB_Volume__c = Decimal.valueOf(String.valueOf(so4.get('currVolume'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so5.get('prevCM')))) {
shiptoAccountToUpdate.Prior_Year_LYB_CM1__c = Decimal.valueOf(String.valueOf(so5.get('prevCM'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so5.get('prevVolume')) )) {
shiptoAccountToUpdate.Prior_Year_LYB_Volume__c = Decimal.valueOf(String.valueOf(so5.get('prevVolume'))).setScale(2);
}
if (!String.isBlank(String.valueOf(so5.get('prevInvoicePrice')) )) {
shiptoAccountToUpdate.Prior_Year_LYB_Sales__c = Decimal.valueOf(String.valueOf(so5.get('prevInvoicePrice'))).setScale(2);
}
lShiptoAccountsToUpdate.add(shiptoAccountToUpdate);
}
}
}
update lShiptoAccountsToUpdate;
update lAccountsToUpdate;
}
global void finish(Database.BatchableContext BC)
{
}
}
-
- Narmadha Chandrasekar 4
- September 27, 2019
- Like
- 0
- Continue reading or reply
map key not found in map visualforce
I'm trying to display a value from map but getting the error
Map key 0011J00001IxhTrQAJ not found in map
Error is in expression '{!regionMap[par]}' in page viewaccounthierarchy
Please find my code below:
Class:
public with sharing class ViewAccountHierarchy {
public Account acc { get; private set; }
public Account baseAccount { get; private set; }
public List<Account> parentAccountList { get; private set; }
public List<Account> grandparentAccountList { get; private set; }
public Map<Id,list<Account>> parentChildMap { get; private set; }
public Map<Id,String> parentMap { get; private set; }
public Map<String,Set<String>> salesAccountMap { get; private set; }
public Map<Id,String> regionMap { get; private set; }
public String grandparent{ get; private set; }
public Id grandparentId{ get; private set; }
public String baseAccountName{ get; private set; }
public String baseAccountParent{ get; private set; }
public ApexPages.StandardController controller { get; private set; }
public ViewAccountHierarchy(ApexPages.StandardController controller) {
this.acc = (Account)controller.getRecord();
this.baseAccount = new Account();
this.parentAccountList = new List<Account>();
this.grandparentAccountList = new List<Account>();
this.parentChildMap = new Map<Id,list<Account>>();
this.parentMap = new Map<Id,String>();
this.salesAccountMap = new Map<String,Set<String>>();
this.regionMap = new Map<Id,String>();
Account baseAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :acc.id];
this.baseAccountName = baseAccount.Name;
this.baseAccountParent = baseAccount.ParentId;
if(baseAccountParent!=null){
Account parentAccount = new Account();
parentAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :baseAccount.ParentId];
parentAccountList.add(parentAccount);
if(parentAccount.ParentId!=null){
Account grandparentAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :parentAccount.ParentId];
grandparentAccountList.add(grandparentAccount);
}else{
grandparentAccountList.add(parentAccount);
}
}else{
grandparentAccountList.add(baseAccount);
}
this.grandparent = grandparentAccountList[0].Name;
this.grandparentid = grandparentAccountList[0].id;
if(grandparentAccountList.size()>0)
{
List<Account> parentAccountList = [SELECT Id,ParentId,Name,Parent.Name,Region__c
FROM Account
WHERE ParentId = :grandparentAccountList[0].Id];
if(parentAccountList.size()>0){
for(integer i=0;i<parentAccountList.size();i++){
List<Account> childAccountList = [SELECT Id,ParentId,Name,Parent.Name,YTD_LYB_CM1__c,YTD_LYB_Vol_KGS__c
FROM Account
WHERE ParentId = :parentAccountList[i].Id];
List<Sales_Account__c> salesAccountList = [SELECT Id,Seller__c,seller__r.Name
FROM Sales_Account__c
WHERE Sold_To_Account__c = :parentAccountList[i].Id];
Set<String> salesAccountSet = new Set<String>();
for(integer j=0;j<salesAccountList.size();j++){
salesAccountSet.add(salesAccountList[j].seller__r.Name);
}
parentMap.put(parentAccountList[i].Id,parentAccountList[i].Name);
parentChildMap.put(parentAccountList[i].Id,childAccountList);
regionMap.put(parentAccountList[i].Id,parentAccountList[i].Region__c);
salesAccountMap.put(parentAccountList[i].Id,salesAccountSet);
}
}
}
}
}
VF Page
<apex:repeat value="{!parentMap}" var="par">
<tr>
<td> {!regionMap[par]} </td>
</tr>
</apex:repeat>:
Map key 0011J00001IxhTrQAJ not found in map
Error is in expression '{!regionMap[par]}' in page viewaccounthierarchy
Please find my code below:
Class:
public with sharing class ViewAccountHierarchy {
public Account acc { get; private set; }
public Account baseAccount { get; private set; }
public List<Account> parentAccountList { get; private set; }
public List<Account> grandparentAccountList { get; private set; }
public Map<Id,list<Account>> parentChildMap { get; private set; }
public Map<Id,String> parentMap { get; private set; }
public Map<String,Set<String>> salesAccountMap { get; private set; }
public Map<Id,String> regionMap { get; private set; }
public String grandparent{ get; private set; }
public Id grandparentId{ get; private set; }
public String baseAccountName{ get; private set; }
public String baseAccountParent{ get; private set; }
public ApexPages.StandardController controller { get; private set; }
public ViewAccountHierarchy(ApexPages.StandardController controller) {
this.acc = (Account)controller.getRecord();
this.baseAccount = new Account();
this.parentAccountList = new List<Account>();
this.grandparentAccountList = new List<Account>();
this.parentChildMap = new Map<Id,list<Account>>();
this.parentMap = new Map<Id,String>();
this.salesAccountMap = new Map<String,Set<String>>();
this.regionMap = new Map<Id,String>();
Account baseAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :acc.id];
this.baseAccountName = baseAccount.Name;
this.baseAccountParent = baseAccount.ParentId;
if(baseAccountParent!=null){
Account parentAccount = new Account();
parentAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :baseAccount.ParentId];
parentAccountList.add(parentAccount);
if(parentAccount.ParentId!=null){
Account grandparentAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :parentAccount.ParentId];
grandparentAccountList.add(grandparentAccount);
}else{
grandparentAccountList.add(parentAccount);
}
}else{
grandparentAccountList.add(baseAccount);
}
this.grandparent = grandparentAccountList[0].Name;
this.grandparentid = grandparentAccountList[0].id;
if(grandparentAccountList.size()>0)
{
List<Account> parentAccountList = [SELECT Id,ParentId,Name,Parent.Name,Region__c
FROM Account
WHERE ParentId = :grandparentAccountList[0].Id];
if(parentAccountList.size()>0){
for(integer i=0;i<parentAccountList.size();i++){
List<Account> childAccountList = [SELECT Id,ParentId,Name,Parent.Name,YTD_LYB_CM1__c,YTD_LYB_Vol_KGS__c
FROM Account
WHERE ParentId = :parentAccountList[i].Id];
List<Sales_Account__c> salesAccountList = [SELECT Id,Seller__c,seller__r.Name
FROM Sales_Account__c
WHERE Sold_To_Account__c = :parentAccountList[i].Id];
Set<String> salesAccountSet = new Set<String>();
for(integer j=0;j<salesAccountList.size();j++){
salesAccountSet.add(salesAccountList[j].seller__r.Name);
}
parentMap.put(parentAccountList[i].Id,parentAccountList[i].Name);
parentChildMap.put(parentAccountList[i].Id,childAccountList);
regionMap.put(parentAccountList[i].Id,parentAccountList[i].Region__c);
salesAccountMap.put(parentAccountList[i].Id,salesAccountSet);
}
}
}
}
}
VF Page
<apex:repeat value="{!parentMap}" var="par">
<tr>
<td> {!regionMap[par]} </td>
</tr>
</apex:repeat>:
-
- Narmadha Chandrasekar 4
- September 26, 2019
- Like
- 0
- Continue reading or reply
create a VF page to display Account Parent,child and grandchild names
create a VF page to display Account Parent,child and grandchild names like the screenshot below.Please help
-
- Narmadha Chandrasekar 4
- August 14, 2019
- Like
- 0
- Continue reading or reply
map key not found in map visualforce
I'm trying to display a value from map but getting the error
Map key 0011J00001IxhTrQAJ not found in map
Error is in expression '{!regionMap[par]}' in page viewaccounthierarchy
Please find my code below:
Class:
public with sharing class ViewAccountHierarchy {
public Account acc { get; private set; }
public Account baseAccount { get; private set; }
public List<Account> parentAccountList { get; private set; }
public List<Account> grandparentAccountList { get; private set; }
public Map<Id,list<Account>> parentChildMap { get; private set; }
public Map<Id,String> parentMap { get; private set; }
public Map<String,Set<String>> salesAccountMap { get; private set; }
public Map<Id,String> regionMap { get; private set; }
public String grandparent{ get; private set; }
public Id grandparentId{ get; private set; }
public String baseAccountName{ get; private set; }
public String baseAccountParent{ get; private set; }
public ApexPages.StandardController controller { get; private set; }
public ViewAccountHierarchy(ApexPages.StandardController controller) {
this.acc = (Account)controller.getRecord();
this.baseAccount = new Account();
this.parentAccountList = new List<Account>();
this.grandparentAccountList = new List<Account>();
this.parentChildMap = new Map<Id,list<Account>>();
this.parentMap = new Map<Id,String>();
this.salesAccountMap = new Map<String,Set<String>>();
this.regionMap = new Map<Id,String>();
Account baseAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :acc.id];
this.baseAccountName = baseAccount.Name;
this.baseAccountParent = baseAccount.ParentId;
if(baseAccountParent!=null){
Account parentAccount = new Account();
parentAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :baseAccount.ParentId];
parentAccountList.add(parentAccount);
if(parentAccount.ParentId!=null){
Account grandparentAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :parentAccount.ParentId];
grandparentAccountList.add(grandparentAccount);
}else{
grandparentAccountList.add(parentAccount);
}
}else{
grandparentAccountList.add(baseAccount);
}
this.grandparent = grandparentAccountList[0].Name;
this.grandparentid = grandparentAccountList[0].id;
if(grandparentAccountList.size()>0)
{
List<Account> parentAccountList = [SELECT Id,ParentId,Name,Parent.Name,Region__c
FROM Account
WHERE ParentId = :grandparentAccountList[0].Id];
if(parentAccountList.size()>0){
for(integer i=0;i<parentAccountList.size();i++){
List<Account> childAccountList = [SELECT Id,ParentId,Name,Parent.Name,YTD_LYB_CM1__c,YTD_LYB_Vol_KGS__c
FROM Account
WHERE ParentId = :parentAccountList[i].Id];
List<Sales_Account__c> salesAccountList = [SELECT Id,Seller__c,seller__r.Name
FROM Sales_Account__c
WHERE Sold_To_Account__c = :parentAccountList[i].Id];
Set<String> salesAccountSet = new Set<String>();
for(integer j=0;j<salesAccountList.size();j++){
salesAccountSet.add(salesAccountList[j].seller__r.Name);
}
parentMap.put(parentAccountList[i].Id,parentAccountList[i].Name);
parentChildMap.put(parentAccountList[i].Id,childAccountList);
regionMap.put(parentAccountList[i].Id,parentAccountList[i].Region__c);
salesAccountMap.put(parentAccountList[i].Id,salesAccountSet);
}
}
}
}
}
VF Page
<apex:repeat value="{!parentMap}" var="par">
<tr>
<td> {!regionMap[par]} </td>
</tr>
</apex:repeat>:
Map key 0011J00001IxhTrQAJ not found in map
Error is in expression '{!regionMap[par]}' in page viewaccounthierarchy
Please find my code below:
Class:
public with sharing class ViewAccountHierarchy {
public Account acc { get; private set; }
public Account baseAccount { get; private set; }
public List<Account> parentAccountList { get; private set; }
public List<Account> grandparentAccountList { get; private set; }
public Map<Id,list<Account>> parentChildMap { get; private set; }
public Map<Id,String> parentMap { get; private set; }
public Map<String,Set<String>> salesAccountMap { get; private set; }
public Map<Id,String> regionMap { get; private set; }
public String grandparent{ get; private set; }
public Id grandparentId{ get; private set; }
public String baseAccountName{ get; private set; }
public String baseAccountParent{ get; private set; }
public ApexPages.StandardController controller { get; private set; }
public ViewAccountHierarchy(ApexPages.StandardController controller) {
this.acc = (Account)controller.getRecord();
this.baseAccount = new Account();
this.parentAccountList = new List<Account>();
this.grandparentAccountList = new List<Account>();
this.parentChildMap = new Map<Id,list<Account>>();
this.parentMap = new Map<Id,String>();
this.salesAccountMap = new Map<String,Set<String>>();
this.regionMap = new Map<Id,String>();
Account baseAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :acc.id];
this.baseAccountName = baseAccount.Name;
this.baseAccountParent = baseAccount.ParentId;
if(baseAccountParent!=null){
Account parentAccount = new Account();
parentAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :baseAccount.ParentId];
parentAccountList.add(parentAccount);
if(parentAccount.ParentId!=null){
Account grandparentAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :parentAccount.ParentId];
grandparentAccountList.add(grandparentAccount);
}else{
grandparentAccountList.add(parentAccount);
}
}else{
grandparentAccountList.add(baseAccount);
}
this.grandparent = grandparentAccountList[0].Name;
this.grandparentid = grandparentAccountList[0].id;
if(grandparentAccountList.size()>0)
{
List<Account> parentAccountList = [SELECT Id,ParentId,Name,Parent.Name,Region__c
FROM Account
WHERE ParentId = :grandparentAccountList[0].Id];
if(parentAccountList.size()>0){
for(integer i=0;i<parentAccountList.size();i++){
List<Account> childAccountList = [SELECT Id,ParentId,Name,Parent.Name,YTD_LYB_CM1__c,YTD_LYB_Vol_KGS__c
FROM Account
WHERE ParentId = :parentAccountList[i].Id];
List<Sales_Account__c> salesAccountList = [SELECT Id,Seller__c,seller__r.Name
FROM Sales_Account__c
WHERE Sold_To_Account__c = :parentAccountList[i].Id];
Set<String> salesAccountSet = new Set<String>();
for(integer j=0;j<salesAccountList.size();j++){
salesAccountSet.add(salesAccountList[j].seller__r.Name);
}
parentMap.put(parentAccountList[i].Id,parentAccountList[i].Name);
parentChildMap.put(parentAccountList[i].Id,childAccountList);
regionMap.put(parentAccountList[i].Id,parentAccountList[i].Region__c);
salesAccountMap.put(parentAccountList[i].Id,salesAccountSet);
}
}
}
}
}
VF Page
<apex:repeat value="{!parentMap}" var="par">
<tr>
<td> {!regionMap[par]} </td>
</tr>
</apex:repeat>:
- Narmadha Chandrasekar 4
- September 26, 2019
- Like
- 0
- Continue reading or reply