• kumara100
  • NEWBIE
  • 0 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies

I want to get number of records in OpportunityLineItem table for each opportunity Id.
First I use count() function for each OpportunityId within FOR () loop.{passing OpportunityId as parameter}
Integer countRec  = [SELECT count() FROM OpportunityLineItem WHERE OpportunityId = '00690000002LdSrAAK'];

But it fails with "Too many SOQL: 101" when process more records in FOR Loop setup.
So I try to rearrange code as
method
1) Integer  countInteger = [SELECT count() FROM OpportunityLineItem WHERE OpportunityId IN : oppIdsList];
        It gives total number of records for all OpportunityIds. Not number of records for each OpportunityIds.
       
       

2) String oppIdsdbCount = '';
    for (OpportunityWrapper oppObjCount : selectedOpportunityList) {
       oppIdsdbCount = oppIdsdbCount + '\'' + oppObjCount.getOpportunity().Id + '\',';
    }
  oppIdsdbCount = '(' + oppIdsdbCount.substring(0, oppIdsdbCount.length() - 1) + ')';
  String queryRecCount = 'SELECT count() FROM OpportunityLineItem WHERE OpportunityId IN ' + oppIdsdbCount ;
  Integer recCount = Database.countQuery(queryRecCount);
  System.debug('======== recCount =====' + recCount);


  ex : String queryRecCount = 'SELECT count() FROM OpportunityLineItem WHERE OpportunityId IN ('00690000002LdSrAAK', '00690000002LdSsAAK', '00690000002LdStAAK')

   In 2nd method also gives total number of records for all OpportunityIds. Not number of records for each OpportunityIds.
 
  So Is there a way to get List<Integer> for count() function. ? Or any other solution ?
 
  Tx
  Chaminda Kumara

Message Edited by kumara100 on 10-26-2009 04:35 AM


My initial code get error when sending more queries as "System.Exception: Too many SOQL queries: 101"
So I rearrange code avoid query within loop.But afterthat it doesn't get results.
Here I use Database.query(sql) for getting describe objects.

Basically I want to get result of Database.query() to List object


Initial code

 

        String oppSOQL = 'SELECT ';
        for (String fieldName : oppFieldsMap.keySet()) {
          oppSOQL += fieldName + ',';
        }
        for (Integer i = 0; i < arrIds.size(); i++) {
          String id = arrIds[i];
          String fullSOQL = '';
          String SOQLEnd = ' FROM Opportunity WHERE Id = \'' + id + '\'';
          if (customObjectDescribedQuery != null && customObjectDescribedQuery != '') {
            fullSOQL = oppSOQL + '(' + customObjectDescribedQuery + ')' +  SOQLEnd;
          } else {
            fullSOQL = oppSOQL.substring(0, oppSOQL.length() - 1)  +  SOQLEnd;
          }
          sObject sObj = Database.query(fullSOQL);
          o = (Opportunity)sObj;
          opportunityList.add(o);
          subTotal += o.EM_TotalBeforeSalesDiscount__c;
        }
        for (Opportunity opp : opportunityList) {
          oppObj = new OpportunityWrapper(opp);
          selectedOpportunityList.add(oppObj);
        }

 

Rearrange Code       

 

        String oppSOQL = 'SELECT ';
        for (String fieldName : oppFieldsMap.keySet()) {
          oppSOQL += fieldName + ',';
        }
        List<Id> oppIds = new List<Id>{};
        for (Integer k = 0; k < arrIds.size(); k++) {
          oppIds.add(arrIds[k]);
        }
        String fullSOQL = '';
        String SOQLEnd = ' FROM Opportunity WHERE Id IN: ' + oppIds;
        if (customObjectDescribedQuery != null && customObjectDescribedQuery != '') {
          fullSOQL = oppSOQL + '(' + customObjectDescribedQuery + ')' +  SOQLEnd;
        } else {
          fullSOQL = oppSOQL.substring(0, oppSOQL.length() - 1)  +  SOQLEnd;
        }
         List<Opportunity> oppList = Database.query(fullSOQL);      //new List<Opportunity>(Database.query(fullSOQL));         
        for(Opportunity o :oppList ){
          opportunityList.add(o);
          subTotal += o.EM_TotalBeforeSalesDiscount__c;
       }
       
        for (Opportunity opp : opportunityList) {
          oppObj = new OpportunityWrapper(opp);
          selectedOpportunityList.add(oppObj);
        }

Message Edited by kumara100 on 10-20-2009 04:33 AM

I want to get error message when setToAddresses() is invalid, but this code gives , isSuccess() method return true even mail address invalid...Is there any special setting for get this error message (Setup--EmailServices,   Administration setup--Email Administration--Deliverability ) 

ublic class EmailController {

    public String errorMessage {get; set;}

    public PageReference SendTestEmail() {
      errorMessage = 'empty';
      Messaging.SingleEmailMessage objEmail = new Messaging.SingleEmailMessage();
      objEmail.setToAddresses(new String[] {'kumaraopro100@gmail.com'});
      objEmail.setSubject('Test email subject');
      objEmail.setPlainTextBody('This is a test email Body FROM SALESFORCE');

      Messaging.SendEmailResult[] sendEmailResults = null;
      try{
       sendEmailResults = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { objEmail});
 
        for (Messaging.SendEmailResult sendEmailResult : sendEmailResults) {
          if(sendEmailResult.isSuccess()){
            errorMessage = 'success';
            System.debug(sendEmailResults);
          } else {
             errorMessage = 'error';
             System.debug(sendEmailResults);
          }
        }
      }catch(Exception e){
        errorMessage = 'error';
        System.debug(sendEmailResults);
      }
      return null;
    }
}

 

Chaminda Kumara

 

Message Edited by kumara100 on 10-14-2009 09:17 PM
Message Edited by kumara100 on 10-14-2009 09:18 PM
Message Edited by kumara100 on 10-14-2009 09:40 PM


My initial code get error when sending more queries as "System.Exception: Too many SOQL queries: 101"
So I rearrange code avoid query within loop.But afterthat it doesn't get results.
Here I use Database.query(sql) for getting describe objects.

Basically I want to get result of Database.query() to List object


Initial code

 

        String oppSOQL = 'SELECT ';
        for (String fieldName : oppFieldsMap.keySet()) {
          oppSOQL += fieldName + ',';
        }
        for (Integer i = 0; i < arrIds.size(); i++) {
          String id = arrIds[i];
          String fullSOQL = '';
          String SOQLEnd = ' FROM Opportunity WHERE Id = \'' + id + '\'';
          if (customObjectDescribedQuery != null && customObjectDescribedQuery != '') {
            fullSOQL = oppSOQL + '(' + customObjectDescribedQuery + ')' +  SOQLEnd;
          } else {
            fullSOQL = oppSOQL.substring(0, oppSOQL.length() - 1)  +  SOQLEnd;
          }
          sObject sObj = Database.query(fullSOQL);
          o = (Opportunity)sObj;
          opportunityList.add(o);
          subTotal += o.EM_TotalBeforeSalesDiscount__c;
        }
        for (Opportunity opp : opportunityList) {
          oppObj = new OpportunityWrapper(opp);
          selectedOpportunityList.add(oppObj);
        }

 

Rearrange Code       

 

        String oppSOQL = 'SELECT ';
        for (String fieldName : oppFieldsMap.keySet()) {
          oppSOQL += fieldName + ',';
        }
        List<Id> oppIds = new List<Id>{};
        for (Integer k = 0; k < arrIds.size(); k++) {
          oppIds.add(arrIds[k]);
        }
        String fullSOQL = '';
        String SOQLEnd = ' FROM Opportunity WHERE Id IN: ' + oppIds;
        if (customObjectDescribedQuery != null && customObjectDescribedQuery != '') {
          fullSOQL = oppSOQL + '(' + customObjectDescribedQuery + ')' +  SOQLEnd;
        } else {
          fullSOQL = oppSOQL.substring(0, oppSOQL.length() - 1)  +  SOQLEnd;
        }
         List<Opportunity> oppList = Database.query(fullSOQL);      //new List<Opportunity>(Database.query(fullSOQL));         
        for(Opportunity o :oppList ){
          opportunityList.add(o);
          subTotal += o.EM_TotalBeforeSalesDiscount__c;
       }
       
        for (Opportunity opp : opportunityList) {
          oppObj = new OpportunityWrapper(opp);
          selectedOpportunityList.add(oppObj);
        }

Message Edited by kumara100 on 10-20-2009 04:33 AM

I want to get error message when setToAddresses() is invalid, but this code gives , isSuccess() method return true even mail address invalid...Is there any special setting for get this error message (Setup--EmailServices,   Administration setup--Email Administration--Deliverability ) 

ublic class EmailController {

    public String errorMessage {get; set;}

    public PageReference SendTestEmail() {
      errorMessage = 'empty';
      Messaging.SingleEmailMessage objEmail = new Messaging.SingleEmailMessage();
      objEmail.setToAddresses(new String[] {'kumaraopro100@gmail.com'});
      objEmail.setSubject('Test email subject');
      objEmail.setPlainTextBody('This is a test email Body FROM SALESFORCE');

      Messaging.SendEmailResult[] sendEmailResults = null;
      try{
       sendEmailResults = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { objEmail});
 
        for (Messaging.SendEmailResult sendEmailResult : sendEmailResults) {
          if(sendEmailResult.isSuccess()){
            errorMessage = 'success';
            System.debug(sendEmailResults);
          } else {
             errorMessage = 'error';
             System.debug(sendEmailResults);
          }
        }
      }catch(Exception e){
        errorMessage = 'error';
        System.debug(sendEmailResults);
      }
      return null;
    }
}

 

Chaminda Kumara

 

Message Edited by kumara100 on 10-14-2009 09:17 PM
Message Edited by kumara100 on 10-14-2009 09:18 PM
Message Edited by kumara100 on 10-14-2009 09:40 PM