• Maria Alberi
  • NEWBIE
  • 40 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 8
    Replies
Hi all, I' ve got some Live Agent question, hope someone can give me an answer...
I have configured live agent in a Salesforce org. I have 4 or 5 agents that work online all day. Sometimes happens that one of the agent  becomes offline in Live agent chat console, after some time that he doesn't work in current chats, but
meanwhile he has been working in other Salesforce tabs.
How is it possible such a thing?
Besides, I made a report that counts for every live agent transcript, chat duration in hours, minutes, and second. I know chat duration is calculated between start time and end time. Is it possible to calculate duration between start time and the end of communication (last message sent/recived from agent)? So, is it possible to obtain from transcript record date/time of sending or reaching  last chat message? 
Thank you very much..
Maria
Hi all, another Live Agent issue :(
I configured my live agent environment with a pre-chat page and a post-chat one.
In my pre-chat page I create a Lead L record, who fires a trigger that creates a person customer linked to L, an opportunity and a case C related to the customer. After lead creation, the agent can see L page open in the console. Some case and lead fields are populated by pre-chat code and others are populated through apex triggers that fire on customer and case.

But at the end of the chat, when a visitor or an agent closes chat, I want to force the agent to fill some mandatory fields in the Case C that were not populated during previous steps. Is it possible to run a script in L page, that check every  tot number of seconds if the chat has finished, and when the chat is closed (at the same time, the Live chat Transcript has created), sends an alert to the agent with something like this "click here to fill C (link to the case C ) fields?".

If it's not possible such solution, can I try another one more simple?
Thank you very much in advance for your help. 
Regards
Maria
 
Hi all, I have live agent configured in my Sf org. Is it possibile to limit chat availability only on specific times? And is it possible, for a visitor, to leave a message if the agent is not available? I want to know if it's possibile WITHOUT using external liveagent app but ONLY salesforce org.
If yes, how could I do this?
Thank you in advance, Maria
Hi all, I have a big problem, hope someone can help me. I successfully configured Live agent in a salesforce production org. After it, I edited my main chat button by setting a pre-chat page. Here is the screenshot of configuration of my button.User-added image
And this is the code of the visualforce pre-chat page (LiveAgentPreChatPage): this is accessible through force.com Site I created for this scope.

<apex:page showHeader="false">

<!-- This script takes the endpoint URL parameter passed from the deployment page and makes it the action for the form -->
<script type='text/javascript'>
(function() {
function handlePageLoad() {
var endpointMatcher = new RegExp("[\\?\\&]endpoint=([^&#]*)");
document.getElementById('prechatForm').setAttribute('action',
decodeURIComponent(endpointMatcher.exec(document.location.search)[1]));
} if (window.addEventListener) {
window.addEventListener('load', handlePageLoad, false);
} else { window.attachEvent('onload', handlePageLoad, false);
}})();
</script>

<h1>Live Agent Pre-Chat Form</h1>

<!-- Form that gathers information from the chat visitor and sets the values to Live Agent Custom Details used later in the example -->
<form method='post' id='prechatForm'>
    First name: <input type='text' name='liveagent.prechat:ContactFirstName' id='firstName' /><br />
    Last name: <input type='text' name='liveagent.prechat:ContactLastName' id='lastName' /><br />
    Email: <input type='text' name='liveagent.prechat:ContactEmail' id='email' /><br />
    Phone: <input type='text' name='liveagent.prechat:ContactPhone' id='phone' /><br />
    Issue: <input type='text' name='liveagent.prechat:CaseSubject' id='subject' /><br />

<!-- Hidden fields used to set additional custom details -->
    <input type="hidden" name="liveagent.prechat:CaseStatus" value="New" /><br />
    
    <!-- This example assumes that "Chat" was added as picklist value to the Case Origin field -->
    <input type="hidden" name="liveagent.prechat:CaseOrigin" value="Chat" /><br />
    
    <!-- This example will set the Case Record Type to a specific value for the record type configured on the org. Lookup the case record type's id on your org and set it here -->
    <input type="hidden" name="liveagent.prechat:CaseRecordType" value="012D0000000Eyni" />
    
    <!-- Used to set the visitor's name for the agent in the Console -->
    <input type="hidden" name="liveagent.prechat.name" id="prechat_field_name" />

<!-- map: Use the data from prechat form to map it to the Salesforce record's fields -->
<input type="hidden" name="liveagent.prechat.findorcreate.map:Contact" value="FirstName,ContactFirstName;LastName,ContactLastName;Email,ContactEmail;Phone,ContactPhone" />

<input type="hidden" name="liveagent.prechat.findorcreate.map:Case" value="Subject,CaseSubject;Status,CaseStatus;Origin,CaseOrigin" />

<!-- doFind, doCreate and isExactMatch example for a Contact: 
    Find a contact whose Email exactly matches the value provided by the customer in the form 
    If there's no match, then create a Contact record and set it's First Name, Last Name, Email, and Phone to the values provided by the customer -->
<input type="hidden" name="liveagent.prechat.findorcreate.map.doFind:Contact" value="Email,true" />
<input type="hidden" name="liveagent.prechat.findorcreate.map.isExactMatch:Contact" value="Email,true" />
<input type="hidden" name="liveagent.prechat.findorcreate.map.doCreate:Contact" value="FirstName,true;LastName,true;Email,true;Phone,true" />

<!-- doCreate example for a Case: create a case to attach to the chat, set the Case Subject to the value provided by the customer and set the case's Status and Origin fields -->
<input type="hidden" name="liveagent.prechat.findorcreate.map.doCreate:Case" value="Subject,true;Status,true;Origin,true" />

<!-- linkToEntity: Set the record Contact record, found/created above, as the Contact on the Case that's created --> 
<input type="hidden" name="liveagent.prechat.findorcreate.linkToEntity:Contact" value="Case,ContactId" />

<!-- showOnCreate: Open the Contact and Case records as sub-tabs to the chat for the agent in the Console -->
<input type="hidden" name="liveagent.prechat.findorcreate.showOnCreate:Contact" value="true" />
<input type="hidden" name="liveagent.prechat.findorcreate.showOnCreate:Case" value="true" />

<!-- saveToTranscript: Associates the records found / created, i.e. Contact and Case, to the Live Chat Transcript record. --> 
<input type="hidden" name="liveagent.prechat.findorcreate.saveToTranscript:Contact" value="ContactId" />
<input type="hidden" name="liveagent.prechat.findorcreate.saveToTranscript:Case" value="CaseId" />

<!-- displayToAgent: Hides the case record type from the agent -->
<input type="hidden" name="liveagent.prechat.findorcreate.displayToAgent:CaseRecordType" value="false" />

<!-- searchKnowledge: Searches knowledge article based on the text, this assumes that Knowledge is setup -->
<input type="hidden" name="liveagent.prechat.knowledgeSearch:CaseSubject" value="true" />

<input type='submit' value='Chat Now' id='prechat_submit' onclick="setName()"/>

<!-- Set the visitor's name for the agent in the Console to first and last name provided by the customer -->
<script type="text/javascript">
   function setName() {
    document.getElementById("prechat_field_name").value =  
        document.getElementById("firstName").value + " " + document.getElementById("lastName").value;
    }
</script>

<style type="text/css">
p {font-weight: bolder }
</style>

</form>
</apex:page>


I can view my pre-chat page and I can also chat between the agent and the visitor, but Sf does not create contact record neither the Case one. Debug logs don't tell me anything. Javascript console does not give me any error message too. Can anyone please help me? Or tell me how I can debug live agent code in order to understand where I'm making mistake?
If you need from me some other detail that can explain better problem, don't hesitate to tell me.
Thank you in advance.
Maria
Hi all, I've got a big problem with a eclipse force.com ide. I managed to install it in a Juno eclipse version (with java 8 installed).  But when I tried to create a new project connected to a Sf environment, I can create it but I can't refresh it from server. It gives me error message: invalid credentials or security token expired. although I am confident they are correct and I also tried to generate my security token different times. I use a user with system administrator profile and my ip address is correctly set in Sf organization trusted ip. When I go to project Properties, and Force.com, to modify credenzials,  password appairs blank. I tried to insert it but it gives me the same error message. I can connect to Salesforce via web or data loader without any problem. When I try to connect with Eclipse, I got a problem: going to user page, in the login history, the last login history is invalid password. Can anyone please help me? I am disperate :( thank you very much.. Maria

Hi all,

I've got a Too many query rows problem in a class.

I've got a custom object, call Prodotti_promozioni_commerciali__c, from I want to create a custom dashboard. This object is a sort of "Bridge object" and has a relationship with Prodotto__c object, who has a Famiglia_dsh__c and a Set_Prod_dsh__c fields, and a relationship with account, who has a ACCOUNTCATEGORY_MI__c fields. It is correlated to another custom object (Promozione_commerciale__c) too. The idea is this: I select from a wizard a family, and I can see for every family the percentage of account related with a promotion (represented with Prodotti_promozioni_commerciali__c object) between total accounts . Every family has 3 or 4 sets correlated to it, and every account can have one between four category: A, B, C,D. The dashboard is composed by lines and columns, the lines represent the set of products for each family, the columns represent possible account category. I have also a datetime picker in order to specify the dates range in which the Prodotti_promozioni_commerciali__c must be shown, depending of Data_adesione__c field of Promozione_commerciale__c object. I have a Bonus picklist Yes/no/all too, if I select Yes, I should see only records with Bonus__c checkbox field = true, same thing if select No, while if I select all I should see both records.

In my code I make a for loop for each product set, like this: in this case I consider only A category, for B,C,D category code is similar.

 

for (string selectedSetProd: listasetProdotti) {

listapromcommA = new set<string>();
List<Prodotti_promozioni_commerciali__c> listaaccounttotA = new List<Prodotti_promozioni_commerciali__c>();
if(selectedBonus == 'all'){
listaaccounttotA = [select Promozione_commerciale__r.Referente__r.AccountId,
Promozione_commerciale__r.Referente__r.Account.ACCOUNTSTATUS_MI__c,
Promozione_commerciale__r.Referente__r.Account.ACCOUNTCATEGORY_MI__c,
Promozione_commerciale__r.Referente__r.Account.RecordType.DeveloperName,
Promozione_commerciale__r.Referente__r.Account.Form_Acc_Cat__c from
Prodotti_promozioni_commerciali__c where Promozione_commerciale__r.Referente__r.Account.Ownerid in:utentiriferimento
AND Promozione_commerciale__r.Referente__r.Account.ACCOUNTSTATUS_MI__c = 'Attivo'
AND Promozione_commerciale__r.Referente__r.Account.RecordType.DeveloperName = 'Ambulatorio'
AND Promozione_commerciale__r.Referente__r.Account.ACCOUNTCATEGORY_MI__c = 'A'
AND Promozione_commerciale__r.Referente__r.Account.ABILITATED_MI__c = true
AND Promozione_commerciale__r.Referente__r.Account.CHECKACCOUNT_MI__c = true
AND Prodotto__r.Famiglia_dsh__c =:selectedFamilyNew
AND Prodotto__r.Set_Prod_dsh__c =:selectedSetProd
AND Promozione_commerciale__r.Data_adesione__c >=: datainizio
AND Promozione_commerciale__r.Data_adesione__c <=: datafine
AND Promozione_commerciale__r.Stato__c = 'Approvata'
];

}
else
{
listaaccounttotA = [select Promozione_commerciale__r.Referente__r.AccountId,
Promozione_commerciale__r.Referente__r.Account.ACCOUNTSTATUS_MI__c,
Promozione_commerciale__r.Referente__r.Account.ACCOUNTCATEGORY_MI__c,
Promozione_commerciale__r.Referente__r.Account.RecordType.DeveloperName,
Promozione_commerciale__r.Referente__r.Account.Form_Acc_Cat__c from
Prodotti_promozioni_commerciali__c where Promozione_commerciale__r.Referente__r.Account.Ownerid in:utentiriferimento
AND Promozione_commerciale__r.Referente__r.Account.ACCOUNTSTATUS_MI__c = 'Attivo'
AND Promozione_commerciale__r.Referente__r.Account.RecordType.DeveloperName = 'Ambulatorio'
AND Promozione_commerciale__r.Referente__r.Account.ACCOUNTCATEGORY_MI__c = 'A'
AND Promozione_commerciale__r.Referente__r.Account.ABILITATED_MI__c = true
AND Promozione_commerciale__r.Referente__r.Account.CHECKACCOUNT_MI__c = true
AND Prodotto__r.Famiglia_dsh__c =:selectedFamilyNew
AND Prodotto__r.Set_Prod_dsh__c =:selectedSetProd
AND Promozione_commerciale__r.Data_adesione__c >=: datainizio
AND Promozione_commerciale__r.Data_adesione__c <=: datafine
AND Promozione_commerciale__r.Stato__c = 'Approvata'
AND Bonus__c =: selectedBonusBool];
}

for(Prodotti_promozioni_commerciali__c ppc:listaaccounttotA){
listapromcommA.add(ppc.Promozione_commerciale__r.Referente__r.AccountId);
}

List<AggregateResult> countaccountpromoAntintA = [SELECT count(Id) conteggiopromo
FROM Account
WHERE Id in: listapromcommA
];

 if(listaaccounttotA.size()> 0) {
DHW_Merial_2__c dmerial = new DHW_Merial_2__c();
dmerial.name = 'prova';
if(listaaccounttotA.size()> 0){
dmerial.Form_Account_accountStatus__c = listaaccounttotA.get(0).Promozione_commerciale__r.Referente__r.Account.ACCOUNTSTATUS_MI__c;
dmerial.Form_ProdottiPromozioniComm_Prodotto_Fam__c = selectedFamilyNew;
dmerial.Form_ProdottiPromozioniComm_Prodotto_Set__c = selectedSetProd;
dmerial.Form_Account_Category__c = listaaccounttotA.get(0).Promozione_commerciale__r.Referente__r.Account.ACCOUNTCATEGORY_MI__c;
dmerial.Form_Account_RecordTypeId__c = listaaccounttotA.get(0).Promozione_commerciale__r.Referente__r.Account.RecordType.DeveloperName;
}

if( String.valueOf(countaccountpromoAntintA.get(0).get('conteggiopromo')) == null || String.valueOf(countaccountpromoAntintA.get(0).get('conteggiopromo')) == 'NaN'
|| countaccountpromoAntintA.get(0).get('conteggiopromo') == '0'
|| countaccountpromoAntintA.get(0).get('conteggiopromo') == 0){

 

dmerial.Count_account_promo__c = 0;
}
else
{
dmerial.Count_account_promo__c = Integer.valueOf(countaccountpromoAntintA.get(0).get('conteggiopromo'));
}

if( String.valueOf(countaccountotaleA.get(0).get('conteggiotot')) == null || String.valueOf(countaccountotaleA.get(0).get('conteggiotot')) == 'NaN' ){
dmerial.Count_account__c = 0;
}
else
{
dmerial.Count_account__c = Integer.valueOf(countaccountotaleA.get(0).get('conteggiotot'));
}
dmerial.name = 'testdmerial'+ Date.today();
insert dmerial;

List<DHW_Merial_2__c> numero = [select Count_account_percent__c from DHW_Merial_2__c where id =: dmerial.id limit 1];

if(numero.size()> 0){
numeropercent = numero.get(0).Count_account_percent__c;

}
acccounta = dmerial.Count_account__c;


if(conteggioA == 0){
percentuale1a = numeropercent;
accpromo1a = dmerial.Count_account_promo__c;
}
else if(conteggioA == 1){
percentuale2a = numeropercent;
accpromo2a = dmerial.Count_account_promo__c;
}
else if(conteggioA == 2){
percentuale3a = numeropercent;
accpromo3a = dmerial.Count_account_promo__c;
}
else if(conteggioA == 3){

percentuale4a =numeropercent;
system.debug('percentuale 4a debug' + numeropercent);
accpromo4a = dmerial.Count_account_promo__c;

}
conteggioA++;


}
if(acccounta== 0){
formulaaccpromoa = 0;
}
else {



formulaaccpromoa = (100 * (Integer.valueOf(countaccountpromoAntintAtuttiprod.get(0).get('conteggiopromo'))))/acccounta; 

}

 

For a particular family, cause there are several records, more than other families records, I obtain a "Too many query rows", if I check All bonus , and I check a large range of dates. How can I solve this, without riducing number of records obtained with where filters?

Do I have to re-write code?

Thank you in advance for your support

 

Maria

Hi all, I have live agent configured in my Sf org. Is it possibile to limit chat availability only on specific times? And is it possible, for a visitor, to leave a message if the agent is not available? I want to know if it's possibile WITHOUT using external liveagent app but ONLY salesforce org.
If yes, how could I do this?
Thank you in advance, Maria
Hi all, I have live agent configured in my Sf org. Is it possibile to limit chat availability only on specific times? And is it possible, for a visitor, to leave a message if the agent is not available? I want to know if it's possibile WITHOUT using external liveagent app but ONLY salesforce org.
If yes, how could I do this?
Thank you in advance, Maria
Hi all, I have a big problem, hope someone can help me. I successfully configured Live agent in a salesforce production org. After it, I edited my main chat button by setting a pre-chat page. Here is the screenshot of configuration of my button.User-added image
And this is the code of the visualforce pre-chat page (LiveAgentPreChatPage): this is accessible through force.com Site I created for this scope.

<apex:page showHeader="false">

<!-- This script takes the endpoint URL parameter passed from the deployment page and makes it the action for the form -->
<script type='text/javascript'>
(function() {
function handlePageLoad() {
var endpointMatcher = new RegExp("[\\?\\&]endpoint=([^&#]*)");
document.getElementById('prechatForm').setAttribute('action',
decodeURIComponent(endpointMatcher.exec(document.location.search)[1]));
} if (window.addEventListener) {
window.addEventListener('load', handlePageLoad, false);
} else { window.attachEvent('onload', handlePageLoad, false);
}})();
</script>

<h1>Live Agent Pre-Chat Form</h1>

<!-- Form that gathers information from the chat visitor and sets the values to Live Agent Custom Details used later in the example -->
<form method='post' id='prechatForm'>
    First name: <input type='text' name='liveagent.prechat:ContactFirstName' id='firstName' /><br />
    Last name: <input type='text' name='liveagent.prechat:ContactLastName' id='lastName' /><br />
    Email: <input type='text' name='liveagent.prechat:ContactEmail' id='email' /><br />
    Phone: <input type='text' name='liveagent.prechat:ContactPhone' id='phone' /><br />
    Issue: <input type='text' name='liveagent.prechat:CaseSubject' id='subject' /><br />

<!-- Hidden fields used to set additional custom details -->
    <input type="hidden" name="liveagent.prechat:CaseStatus" value="New" /><br />
    
    <!-- This example assumes that "Chat" was added as picklist value to the Case Origin field -->
    <input type="hidden" name="liveagent.prechat:CaseOrigin" value="Chat" /><br />
    
    <!-- This example will set the Case Record Type to a specific value for the record type configured on the org. Lookup the case record type's id on your org and set it here -->
    <input type="hidden" name="liveagent.prechat:CaseRecordType" value="012D0000000Eyni" />
    
    <!-- Used to set the visitor's name for the agent in the Console -->
    <input type="hidden" name="liveagent.prechat.name" id="prechat_field_name" />

<!-- map: Use the data from prechat form to map it to the Salesforce record's fields -->
<input type="hidden" name="liveagent.prechat.findorcreate.map:Contact" value="FirstName,ContactFirstName;LastName,ContactLastName;Email,ContactEmail;Phone,ContactPhone" />

<input type="hidden" name="liveagent.prechat.findorcreate.map:Case" value="Subject,CaseSubject;Status,CaseStatus;Origin,CaseOrigin" />

<!-- doFind, doCreate and isExactMatch example for a Contact: 
    Find a contact whose Email exactly matches the value provided by the customer in the form 
    If there's no match, then create a Contact record and set it's First Name, Last Name, Email, and Phone to the values provided by the customer -->
<input type="hidden" name="liveagent.prechat.findorcreate.map.doFind:Contact" value="Email,true" />
<input type="hidden" name="liveagent.prechat.findorcreate.map.isExactMatch:Contact" value="Email,true" />
<input type="hidden" name="liveagent.prechat.findorcreate.map.doCreate:Contact" value="FirstName,true;LastName,true;Email,true;Phone,true" />

<!-- doCreate example for a Case: create a case to attach to the chat, set the Case Subject to the value provided by the customer and set the case's Status and Origin fields -->
<input type="hidden" name="liveagent.prechat.findorcreate.map.doCreate:Case" value="Subject,true;Status,true;Origin,true" />

<!-- linkToEntity: Set the record Contact record, found/created above, as the Contact on the Case that's created --> 
<input type="hidden" name="liveagent.prechat.findorcreate.linkToEntity:Contact" value="Case,ContactId" />

<!-- showOnCreate: Open the Contact and Case records as sub-tabs to the chat for the agent in the Console -->
<input type="hidden" name="liveagent.prechat.findorcreate.showOnCreate:Contact" value="true" />
<input type="hidden" name="liveagent.prechat.findorcreate.showOnCreate:Case" value="true" />

<!-- saveToTranscript: Associates the records found / created, i.e. Contact and Case, to the Live Chat Transcript record. --> 
<input type="hidden" name="liveagent.prechat.findorcreate.saveToTranscript:Contact" value="ContactId" />
<input type="hidden" name="liveagent.prechat.findorcreate.saveToTranscript:Case" value="CaseId" />

<!-- displayToAgent: Hides the case record type from the agent -->
<input type="hidden" name="liveagent.prechat.findorcreate.displayToAgent:CaseRecordType" value="false" />

<!-- searchKnowledge: Searches knowledge article based on the text, this assumes that Knowledge is setup -->
<input type="hidden" name="liveagent.prechat.knowledgeSearch:CaseSubject" value="true" />

<input type='submit' value='Chat Now' id='prechat_submit' onclick="setName()"/>

<!-- Set the visitor's name for the agent in the Console to first and last name provided by the customer -->
<script type="text/javascript">
   function setName() {
    document.getElementById("prechat_field_name").value =  
        document.getElementById("firstName").value + " " + document.getElementById("lastName").value;
    }
</script>

<style type="text/css">
p {font-weight: bolder }
</style>

</form>
</apex:page>


I can view my pre-chat page and I can also chat between the agent and the visitor, but Sf does not create contact record neither the Case one. Debug logs don't tell me anything. Javascript console does not give me any error message too. Can anyone please help me? Or tell me how I can debug live agent code in order to understand where I'm making mistake?
If you need from me some other detail that can explain better problem, don't hesitate to tell me.
Thank you in advance.
Maria
Hi all, I've got a big problem with a eclipse force.com ide. I managed to install it in a Juno eclipse version (with java 8 installed).  But when I tried to create a new project connected to a Sf environment, I can create it but I can't refresh it from server. It gives me error message: invalid credentials or security token expired. although I am confident they are correct and I also tried to generate my security token different times. I use a user with system administrator profile and my ip address is correctly set in Sf organization trusted ip. When I go to project Properties, and Force.com, to modify credenzials,  password appairs blank. I tried to insert it but it gives me the same error message. I can connect to Salesforce via web or data loader without any problem. When I try to connect with Eclipse, I got a problem: going to user page, in the login history, the last login history is invalid password. Can anyone please help me? I am disperate :( thank you very much.. Maria
Hello all,

I need help to get mavensmate to work with my windows 10. How can I get mm to use chrone instead of IE to open the UI when I create a new project. I am getting; "The local MavensMate server did not respond properly. This likely means it is not running or it is malfunctioning. Try restarting your text editor and MavensMate.app", etc

I have reset setting in chrome under advanced settings and I have restarted many times.

Hi all,

I've got a Too many query rows problem in a class.

I've got a custom object, call Prodotti_promozioni_commerciali__c, from I want to create a custom dashboard. This object is a sort of "Bridge object" and has a relationship with Prodotto__c object, who has a Famiglia_dsh__c and a Set_Prod_dsh__c fields, and a relationship with account, who has a ACCOUNTCATEGORY_MI__c fields. It is correlated to another custom object (Promozione_commerciale__c) too. The idea is this: I select from a wizard a family, and I can see for every family the percentage of account related with a promotion (represented with Prodotti_promozioni_commerciali__c object) between total accounts . Every family has 3 or 4 sets correlated to it, and every account can have one between four category: A, B, C,D. The dashboard is composed by lines and columns, the lines represent the set of products for each family, the columns represent possible account category. I have also a datetime picker in order to specify the dates range in which the Prodotti_promozioni_commerciali__c must be shown, depending of Data_adesione__c field of Promozione_commerciale__c object. I have a Bonus picklist Yes/no/all too, if I select Yes, I should see only records with Bonus__c checkbox field = true, same thing if select No, while if I select all I should see both records.

In my code I make a for loop for each product set, like this: in this case I consider only A category, for B,C,D category code is similar.

 

for (string selectedSetProd: listasetProdotti) {

listapromcommA = new set<string>();
List<Prodotti_promozioni_commerciali__c> listaaccounttotA = new List<Prodotti_promozioni_commerciali__c>();
if(selectedBonus == 'all'){
listaaccounttotA = [select Promozione_commerciale__r.Referente__r.AccountId,
Promozione_commerciale__r.Referente__r.Account.ACCOUNTSTATUS_MI__c,
Promozione_commerciale__r.Referente__r.Account.ACCOUNTCATEGORY_MI__c,
Promozione_commerciale__r.Referente__r.Account.RecordType.DeveloperName,
Promozione_commerciale__r.Referente__r.Account.Form_Acc_Cat__c from
Prodotti_promozioni_commerciali__c where Promozione_commerciale__r.Referente__r.Account.Ownerid in:utentiriferimento
AND Promozione_commerciale__r.Referente__r.Account.ACCOUNTSTATUS_MI__c = 'Attivo'
AND Promozione_commerciale__r.Referente__r.Account.RecordType.DeveloperName = 'Ambulatorio'
AND Promozione_commerciale__r.Referente__r.Account.ACCOUNTCATEGORY_MI__c = 'A'
AND Promozione_commerciale__r.Referente__r.Account.ABILITATED_MI__c = true
AND Promozione_commerciale__r.Referente__r.Account.CHECKACCOUNT_MI__c = true
AND Prodotto__r.Famiglia_dsh__c =:selectedFamilyNew
AND Prodotto__r.Set_Prod_dsh__c =:selectedSetProd
AND Promozione_commerciale__r.Data_adesione__c >=: datainizio
AND Promozione_commerciale__r.Data_adesione__c <=: datafine
AND Promozione_commerciale__r.Stato__c = 'Approvata'
];

}
else
{
listaaccounttotA = [select Promozione_commerciale__r.Referente__r.AccountId,
Promozione_commerciale__r.Referente__r.Account.ACCOUNTSTATUS_MI__c,
Promozione_commerciale__r.Referente__r.Account.ACCOUNTCATEGORY_MI__c,
Promozione_commerciale__r.Referente__r.Account.RecordType.DeveloperName,
Promozione_commerciale__r.Referente__r.Account.Form_Acc_Cat__c from
Prodotti_promozioni_commerciali__c where Promozione_commerciale__r.Referente__r.Account.Ownerid in:utentiriferimento
AND Promozione_commerciale__r.Referente__r.Account.ACCOUNTSTATUS_MI__c = 'Attivo'
AND Promozione_commerciale__r.Referente__r.Account.RecordType.DeveloperName = 'Ambulatorio'
AND Promozione_commerciale__r.Referente__r.Account.ACCOUNTCATEGORY_MI__c = 'A'
AND Promozione_commerciale__r.Referente__r.Account.ABILITATED_MI__c = true
AND Promozione_commerciale__r.Referente__r.Account.CHECKACCOUNT_MI__c = true
AND Prodotto__r.Famiglia_dsh__c =:selectedFamilyNew
AND Prodotto__r.Set_Prod_dsh__c =:selectedSetProd
AND Promozione_commerciale__r.Data_adesione__c >=: datainizio
AND Promozione_commerciale__r.Data_adesione__c <=: datafine
AND Promozione_commerciale__r.Stato__c = 'Approvata'
AND Bonus__c =: selectedBonusBool];
}

for(Prodotti_promozioni_commerciali__c ppc:listaaccounttotA){
listapromcommA.add(ppc.Promozione_commerciale__r.Referente__r.AccountId);
}

List<AggregateResult> countaccountpromoAntintA = [SELECT count(Id) conteggiopromo
FROM Account
WHERE Id in: listapromcommA
];

 if(listaaccounttotA.size()> 0) {
DHW_Merial_2__c dmerial = new DHW_Merial_2__c();
dmerial.name = 'prova';
if(listaaccounttotA.size()> 0){
dmerial.Form_Account_accountStatus__c = listaaccounttotA.get(0).Promozione_commerciale__r.Referente__r.Account.ACCOUNTSTATUS_MI__c;
dmerial.Form_ProdottiPromozioniComm_Prodotto_Fam__c = selectedFamilyNew;
dmerial.Form_ProdottiPromozioniComm_Prodotto_Set__c = selectedSetProd;
dmerial.Form_Account_Category__c = listaaccounttotA.get(0).Promozione_commerciale__r.Referente__r.Account.ACCOUNTCATEGORY_MI__c;
dmerial.Form_Account_RecordTypeId__c = listaaccounttotA.get(0).Promozione_commerciale__r.Referente__r.Account.RecordType.DeveloperName;
}

if( String.valueOf(countaccountpromoAntintA.get(0).get('conteggiopromo')) == null || String.valueOf(countaccountpromoAntintA.get(0).get('conteggiopromo')) == 'NaN'
|| countaccountpromoAntintA.get(0).get('conteggiopromo') == '0'
|| countaccountpromoAntintA.get(0).get('conteggiopromo') == 0){

 

dmerial.Count_account_promo__c = 0;
}
else
{
dmerial.Count_account_promo__c = Integer.valueOf(countaccountpromoAntintA.get(0).get('conteggiopromo'));
}

if( String.valueOf(countaccountotaleA.get(0).get('conteggiotot')) == null || String.valueOf(countaccountotaleA.get(0).get('conteggiotot')) == 'NaN' ){
dmerial.Count_account__c = 0;
}
else
{
dmerial.Count_account__c = Integer.valueOf(countaccountotaleA.get(0).get('conteggiotot'));
}
dmerial.name = 'testdmerial'+ Date.today();
insert dmerial;

List<DHW_Merial_2__c> numero = [select Count_account_percent__c from DHW_Merial_2__c where id =: dmerial.id limit 1];

if(numero.size()> 0){
numeropercent = numero.get(0).Count_account_percent__c;

}
acccounta = dmerial.Count_account__c;


if(conteggioA == 0){
percentuale1a = numeropercent;
accpromo1a = dmerial.Count_account_promo__c;
}
else if(conteggioA == 1){
percentuale2a = numeropercent;
accpromo2a = dmerial.Count_account_promo__c;
}
else if(conteggioA == 2){
percentuale3a = numeropercent;
accpromo3a = dmerial.Count_account_promo__c;
}
else if(conteggioA == 3){

percentuale4a =numeropercent;
system.debug('percentuale 4a debug' + numeropercent);
accpromo4a = dmerial.Count_account_promo__c;

}
conteggioA++;


}
if(acccounta== 0){
formulaaccpromoa = 0;
}
else {



formulaaccpromoa = (100 * (Integer.valueOf(countaccountpromoAntintAtuttiprod.get(0).get('conteggiopromo'))))/acccounta; 

}

 

For a particular family, cause there are several records, more than other families records, I obtain a "Too many query rows", if I check All bonus , and I check a large range of dates. How can I solve this, without riducing number of records obtained with where filters?

Do I have to re-write code?

Thank you in advance for your support

 

Maria