• Stanley Jacobs
  • NEWBIE
  • 5 Points
  • Member since 2015
  • Deloitte Consulting

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 8
    Replies
Hello Guys,

When I validate the trigger with test class on LIVE, getting error for Too many SQL Quries 101. Can you please help me to update the trigger.

Trigger: UpdateContactOwnerForTrial that wants to validate on LIVE.
trigger UpdateContactOwnerForTrial on Opportunity (after insert, after Update) {
    
     List<Contact> conList = new List<Contact>([select Id,ownerId from Contact where Id In (select ContactId from OpportunityContactRole where OpportunityId IN :Trigger.newMap.keySet())]); 
  	 List<Contact> contactsToUpdate = new List<Contact>{};
    
    for(Opportunity opp : trigger.new)
    {   
          for(Contact c: conList){
              c.ownerId = opp.ownerId;
              contactsToUpdate.add(c);
        }    
    }
   update contactsToUpdate; 

}

Test class
@isTest
public class TestUpdateContactOwnerForTrial {
	static testMethod void TestUpdateContactOwnerForTrialTest()
	{    
		Account a = new Account(Name = 'TestUpdateContactOwnerForTrial');
		a.Type = 'Customer';
		insert a;

		Contact newContact = new Contact(Lastname='testLastName', AccountId = a.Id );
		insert newContact;
	
        String opportunityName = 'TestUpdateContactOwnerForTrial';
        Opportunity o1 = new Opportunity(AccountId=a.Id, name=opportunityName, StageName='Closed Won', Accounting_Platform__c='QuickBooks Online (US)', CloseDate = Date.today() );
        insert o1;
        
		OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId = o1.Id, ContactId = newContact.Id, Role='Decision Maker',Isprimary=true );
		insert ocr;
        
    }	
		
}

Error: 

User-added image
trigger billingSubscriptionEmail on Opportunity (before update) {


List<Addon_Opportunity__c> addOppList = new List<Addon_Opportunity__c>([SELECT Name,Amount__c,Quantity__c FROM Addon_Opportunity__c where Id__c IN :Trigger.newMap.keySet()]); 

List<Contact> conList = new List<Contact>([select Email from Contact where Id In (select ContactId from OpportunityContactRole where OpportunityId IN :Trigger.newMap.keySet())]); 

 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     Decimal planPrice=0 ;
    String[] toAddresses;
    if(addOppList!=null){
        for(Addon_Opportunity__c addon :addOppList){
                planPrice= planPrice+ addon.Amount__c;
            }    
  }
    for(Opportunity opp : trigger.new){
    
            if(opp.StageName=='Closed Won' && Trigger.oldMap.get(opp.Id).StageName!='Closed Won'){
                
                for(Contact c:conList){
                        if(c.Email.contains('webgility.com')){
                            toAddresses = new String[] {'test3@test.com','test1@test.com'};
                        }else{
                                 toAddresses = new String[] {'test4@test.com','test2@test.com'};
                        }     
                     String billingCycle;
                     String promo='';
                       
                        mail.setToAddresses(toAddresses );   
                        mail.setSubject('New Subscription - '+opp.Accounting_Platform__c+' - '+c.Email);   
                       if(opp.Billing_Cycle__c=='Monthly'){
                         billingCycle = 'Monthly';
                       }else{
                          billingCycle = 'Annual';  
                       }
                       
                      if(opp.promo_code__c!=null){
                          promo = opp.promo_code__c;
                      }else{
                          promo = 'None';
                      } 
                         Decimal P;
                       
                         if(opp.Recurring_Revenue__c==null) opp.Recurring_Revenue__c=0;
                         if(opp.One_Time_Fee__c==null) opp.One_Time_Fee__c=0;
                         P = (integer)opp.Recurring_Revenue__c +(integer)opp.One_Time_Fee__c-(integer)planPrice;
                         String template = 'Billing & Subscription Info:\n';
                         template+= '----------------------------------\n';
                       template+= 'Account Email: '+c.Email+'\n';
                         template+= 'Total Upfront Payment: $'+ opp.One_Time_Fee__c+'\n';  
                         template+= billingCycle+' Recurring Amount: $'+opp.Recurring_Revenue__c+'\n';
                         template+= 'Plan: '+opp.Plan__c+' ($'+P+')\n';
                         template+= 'Plan Tier: '+opp.Plan_Tier__c+'\n';
                         template+= '----------------------------------\n';
                         for(Addon_Opportunity__c addon :addOppList){
                             String qty='';
                             if(addon.Name=='Installation'){
                                addon.Name = 'Installation ,setup and traning';
                             }
                             if(addon.Name=='Add-on Users' || addon.Name=='Add-on Profile')
                             {
                                qty =  String.valueOf(addon.Quantity__c.format())+' ';
                             } 
                             template+= qty+addon.Name+': $'+addon.Amount__c;
                             template+= '\n';
                        }
                        template+= '\n';
                        template+= '----------------------------------\n';
                        template+= 'Recurring billing start date :'+opp.Purchase_Date__c+'\n';
                        template+= 'Promo Code :'+promo+'\n';
                        template+= 'SF Opportunity: '+'https://webgility.my.salesforce.com/'+Trigger.oldMap.get(opp.Id).Id;
                        List<String> args = new List<String>();
                   String formattedHtml = String.format(template, args);
                   
                    mail.setPlainTextBody(formattedHtml);
                         
                    } 
                   if(BillingEmailTriggerHandler.isFirstTime)
                        {
                          BillingEmailTriggerHandler.isFirstTime = false;  
                          Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});  
                       }  
            }

    }

}
Issue is getting on the line no. 6
List<Contact> conList = new List<Contact>([select Email from Contact where Id In (select ContactId from OpportunityContactRole where OpportunityId IN :Trigger.newMap.keySet())]); 

Thanks 








 
Hi Team,

I have 2 objects quote and invoice. Whenever quote is created and when it is updated to Approved , all data goes to invoice object and a invoice record get created.
My problem is when anytime other fields in quote is updated again the trigger runs and a new invoice gets created. i just want my trigger to run only once. This is my code.

trigger createInvoiceQuote on Quote (after update)
{
   
   
    if(trigger.isAfter){
    set<id> oppIds = new set<id>();
    list<Invoicing__c> newInvoiceList = new list<Invoicing__c>();
    list<Quote> QuoteList = new list<Quote>();
    map<id,id> conRoleMap = new map<id, id>();
    set<id> accSet = new set<id>();
    map<id, contact> accConMap = new map<id , contact>();

    QuoteList = [select id, status, name, isSyncing, opportunityId, contact.Billing_Contact__c , contactId, opportunity.Fieldwork_Stage__c, AccountId, Account.GST_Setting__c from quote where id IN: trigger.newMap.keyset()];
    
    system.debug('QuoteList' + QuoteList);
    
    for(Quote quo : QuoteList){
        if(quo.isSyncing && quo.Status.equalsIgnoreCase('Approved')){
                oppIds.add(quo.opportunityId);
                accSet.add(quo.AccountId);
        }
    }
    
    for(OpportunityContactRole conRole : [select id, contactId, IsPrimary, opportunityId from OpportunityContactRole where opportunityId in: oppIds AND isPrimary = true])
    {
        conRoleMap.put(conRole.opportunityId , conRole.contactId);
    }
    
    for(contact con : [select id, accountId, email, billing_Contact__c from contact where id in: conRoleMap.values() OR (accountId in: accSet AND Billing_Contact__c = true)])
    {
        accConMap.put(con.AccountId , con);
        accConMap.put(con.id , con);
    }
    
    system.debug('conRoleMap ' + conRoleMap);

    for(Quote quo : QuoteList){
        if(quo.isSyncing && quo.Status.equalsIgnoreCase('Approved')){               
            Invoicing__c inv = new Invoicing__c();
            inv.Quote__c = quo.Id;
            inv.Invoice_Name__c = quo.Name;
            inv.Status__c = quo.opportunity.Fieldwork_Stage__c;
            inv.Opportunity_Name__c = quo.OpportunityId;
            inv.GST_Setting__c = quo.Account.GST_Setting__c;
        if(accConMap.containsKey(quo.AccountId)){
            Contact secCon = accConMap.get(quo.AccountId);
            if(secCon.Billing_Contact__c){
                inv.Secondary_Billing_Contact__c = secCon.Id;
                inv.Secondary_Billing_Contact_Email_id__c = secCon.email;
            }
        }
        if(conRoleMap.containsKey(quo.OpportunityId)){
            inv.Primary_Billing_Contact__c = conRoleMap.get(quo.OpportunityId);
            if(accConMap.containsKey(conRoleMap.get(quo.OpportunityId)))
                inv.Primary_Billing_Contact_Email_id__c = accConMap.get(conRoleMap.get(quo.OpportunityId)).email;
        }
            newInvoiceList.add(inv);
        }
    }
        
        for(Invoicing__c inv : [select id, Opportunity_Name__c from Invoicing__c where Opportunity_Name__c in: oppIds]){
            inv.Opportunity_Name__c = null;
            newInvoiceList.add(inv);
        }
        
        upsert newInvoiceList;
    }

}

Can anyone please help. Need it urgently

Thanks.

Regards,
Samuel
 
Hi Guys,

I am trying to hit below URL to use Attachments API and I am getting BAD Request. Can you help me with the right URL and point out if anything is missed.

builder = new PostMethod("https://gus,salesforce.com/" + "/services/data/v20.0/sobjects/Attachment/");
builder.setRequestHeader("Authorization","OAuth " + lr.getSessionId()); 
httpclient.executeMethod(builder);
System.out.println(builder.getStatusText());   << throws Bad Request
Our web uses the Salesforce API for retrieving data with this query:

SELECT id, name, CRMforNonProfit__Transactions_Total__c, Transaction_Count__c, NumberOfContacts from Campaign

The whole code looks like:
$sforce = new \SforceEnterpriseClient();
$sforceConnection = $sforce->createConnection($this->context->parameters['appDir'] . '/../exlibs/toolkit/soapclient/enterprise2.wsdl');
$login = $sforce->login($this->context->parameters['toolkit']['user'], $this->context->parameters['toolkit']['login'] . $this->context->parameters['toolkit']['securitytoken']);
$query = 'SELECT Id, Name, CRMforNonProfit__Transactions_Total__c,Transaction_Count__c, NumberOfContacts from Campaign';
$response = $sforce->query($query);

As long as we read in information about first campaign only, we observed no problems with the speed of loading. When we now also read in information about the second campaign, loading time increased to 60 seconds or more.

Where is the problem? How to speed up loading?