• Sunil Kumar Reddy Y 9
  • NEWBIE
  • 55 Points
  • Member since 2021

  • Chatter
    Feed
  • 2
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 6
    Replies
Hi Team,

I have a Apex class which is called in trigger. It is throwing Apex CPU time limit exceeded Error. I have searched and found that my code is not bulkified but I am facing issue in bulkifying it.

APEX CODE:

public class QuoteLineClass {
    
    public static void UpdatePricingCategory(List<SBQQ__QuoteLine__c> qLine){
        
        System.Debug('UpdatePricingCategory begin');     
        for(SBQQ__QuoteLine__c quoteLine : qLine)  {
            Id VantageRecordTypeId = Schema.SObjectType.SBQQ__ContractedPrice__c.getRecordTypeInfosByName().get('Vantage').getRecordTypeId();
            Id NonVanProdRecordTypeId = Schema.SObjectType.SBQQ__ContractedPrice__c.getRecordTypeInfosByName().get('Non Vantage').getRecordTypeId(); 
            if(quoteLine.SBQQ__Product__c!=null && (quoteLine.Offer_Name__c == null || quoteLine.Offer_Name__c == '')){
                
                List<SBQQ__ContractedPrice__c> cps=[Select id,RecordTypeId,Pricing_Master_Version__c,SBQQ__Price__c,Pricing_Category__c,SBQQ__EffectiveDate__c,SBQQ__ExpirationDate__c, SBQQ__Product__c from SBQQ__ContractedPrice__c where SBQQ__Product__c =:quoteLine.SBQQ__Product__c
                                                    AND Status__c='Approved' AND SBQQ__EffectiveDate__c <=:quoteLine.Activity_Date__c AND SBQQ__ExpirationDate__c >=:quoteLine.Activity_Date__c];
                System.Debug('1... SBQQ__ContractedPrice__c size' + cps.size());
             
                for(SBQQ__ContractedPrice__c cp :cps){
                    if(cp.RecordTypeId == VantageRecordTypeId){
                        quoteLine.CP_Pricing_Category__c = cp.Pricing_Category__c;
                        quoteLine.CP_Vantage_Pricing_Version__c = cp.Pricing_Master_Version__c;
                        quoteLine.SBQQ__ContractedPrice__c = null;
                        quoteLine.SBQQ__SpecialPriceType__c = null;
                        quoteLine.CP_Lightbox_Pricing__c = true; 
                        System.Debug('inside if');
                    } 
                    else if(cp.RecordTypeId == NonVanProdRecordTypeId){    
                        quoteLine.SBQQ__ContractedPrice__c = cp.Id;
                        quoteLine.CP_Lightbox_Pricing__c = false;
                        System.Debug('inside else if');
                    }                 
                }                
            }
        }
    }    
    
    public static void UpdateQuoteLine(List<SBQQ__QuoteLine__c> qLine){
        List<SBQQ__QuoteLine__c> q = new List<SBQQ__QuoteLine__c>();
        for(SBQQ__QuoteLine__c quoteLine : qLine){
            if(quoteLine.Score_Band__c == 'No Score Band'){
                quoteLine.SBQQ__ContractedPrice__c = null;
                quoteLine.SBQQ__SpecialPriceType__c = null;
            }
        }
    }     
}

TRIGGER:

trigger QuoteLineTrigger on SBQQ__QuoteLine__c (before insert, after insert, before update, after update, before delete, after delete) {
    QuoteLineTriggerDispatcher dispatcher = new QuoteLineTriggerDispatcher();
    dispatcher.dispatchEvent();
    System.Debug('inside trigger before if');
   if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
        if(checkRecursive.runOnce()){
            System.Debug('inside trigger after if');
        QuoteLineClass.UpdatePricingCategory(Trigger.new);
       QuoteLineClass.UpdateQuoteLine(Trigger.new);  
        System.Debug('inside trigger after if.... completed');
        }
    }  
}

Please help me in bulkifying it.

Thank You,
Mahesh

Hi everyone. 

Thanks in advance for your help.

I need some help to figure out how to fix the following error:

getSuspendedAsset: execution of AfterInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Case.Type Trigger.getSuspendedAsset: line 12, column 1

This is happening when I am inserting a new record in the Case object.

This is my code:

trigger getSuspendedAsset on Case (after insert) {

    static final String INVOLUNTARY = 'Reconn I bla bla bla';    
    static final String VOLUNTARY = 'Reconn V bla bla bla';
    
    Case caseToUpdate= [SELECT Id, AccountId, AssetId From Case where Id in :Trigger.new];    
    system.debug('Case Id: '+caseToUpdate.Id);
    system.debug('AccountId: '+caseToUpdate.AccountId);
    system.debug('AssetId: '+caseToUpdate.AssetId);    

    try{
    If(caseToUpdate.Type == 'Something' 
       && caseToUpdate.High_Level_Transaction_Classification__c == 'Something' 
       && (caseToUpdate.Transaction_Type__c == INVOLUNTARY || caseToUpdate.Transaction_Type__c == VOLUNTARY)){
               
           if(caseToUpdate.AccountId != null){
               List<Asset> assets = 
                   [select Id from Asset where Status__c = 'Suspended' and AccountId =: caseToUpdate.AccountId LIMIT 1]; // setting LIMIT 1 for testing purpose
                   if (assets.size() > 0){
                    for(Asset a : assets){caseToUpdate.AssetId = a.Id;}
                    update caseToUpdate;
                   }                   
           }
       }
    }
Catch(DMLException de){
    System.debug('An unexpected error has occurred: ' + de.getMessage());}
}
Not sure if this is possible, but I want a trigger to fire when the Clone button on opportunity is clicked so that an alert will popup.  The alert is basic and the user can just click OK and move on to the standard clone create/edit page...I'd rather not override the standard edit page so that is why I'm hoping I can somehow achieve this in a trigger?

My trigger is below, but I'm thinking of all the actions available (before insert, after insert etc....) and none of them make logical sense so I'm not sure if what I want to do is possible. 

Worst case, I will just do it before insert, but the message will be telling them to NOT complete fields in the edit screen until after they save (loooong story) so it would be better for the user to get that message before they get to the edit screen.

Any help or suggestions would be appreciated!
 
trigger Clone_Popup on Opportunity (before insert) {
    for (Opportunity o : Trigger.new) {
  	if ( o.isclone() ) {
    o.addError('Warning...blah blah blah');
  }
}
}

 
Hi Team,

I have a Apex class which is called in trigger. It is throwing Apex CPU time limit exceeded Error. I have searched and found that my code is not bulkified but I am facing issue in bulkifying it.

APEX CODE:

public class QuoteLineClass {
    
    public static void UpdatePricingCategory(List<SBQQ__QuoteLine__c> qLine){
        
        System.Debug('UpdatePricingCategory begin');     
        for(SBQQ__QuoteLine__c quoteLine : qLine)  {
            Id VantageRecordTypeId = Schema.SObjectType.SBQQ__ContractedPrice__c.getRecordTypeInfosByName().get('Vantage').getRecordTypeId();
            Id NonVanProdRecordTypeId = Schema.SObjectType.SBQQ__ContractedPrice__c.getRecordTypeInfosByName().get('Non Vantage').getRecordTypeId(); 
            if(quoteLine.SBQQ__Product__c!=null && (quoteLine.Offer_Name__c == null || quoteLine.Offer_Name__c == '')){
                
                List<SBQQ__ContractedPrice__c> cps=[Select id,RecordTypeId,Pricing_Master_Version__c,SBQQ__Price__c,Pricing_Category__c,SBQQ__EffectiveDate__c,SBQQ__ExpirationDate__c, SBQQ__Product__c from SBQQ__ContractedPrice__c where SBQQ__Product__c =:quoteLine.SBQQ__Product__c
                                                    AND Status__c='Approved' AND SBQQ__EffectiveDate__c <=:quoteLine.Activity_Date__c AND SBQQ__ExpirationDate__c >=:quoteLine.Activity_Date__c];
                System.Debug('1... SBQQ__ContractedPrice__c size' + cps.size());
             
                for(SBQQ__ContractedPrice__c cp :cps){
                    if(cp.RecordTypeId == VantageRecordTypeId){
                        quoteLine.CP_Pricing_Category__c = cp.Pricing_Category__c;
                        quoteLine.CP_Vantage_Pricing_Version__c = cp.Pricing_Master_Version__c;
                        quoteLine.SBQQ__ContractedPrice__c = null;
                        quoteLine.SBQQ__SpecialPriceType__c = null;
                        quoteLine.CP_Lightbox_Pricing__c = true; 
                        System.Debug('inside if');
                    } 
                    else if(cp.RecordTypeId == NonVanProdRecordTypeId){    
                        quoteLine.SBQQ__ContractedPrice__c = cp.Id;
                        quoteLine.CP_Lightbox_Pricing__c = false;
                        System.Debug('inside else if');
                    }                 
                }                
            }
        }
    }    
    
    public static void UpdateQuoteLine(List<SBQQ__QuoteLine__c> qLine){
        List<SBQQ__QuoteLine__c> q = new List<SBQQ__QuoteLine__c>();
        for(SBQQ__QuoteLine__c quoteLine : qLine){
            if(quoteLine.Score_Band__c == 'No Score Band'){
                quoteLine.SBQQ__ContractedPrice__c = null;
                quoteLine.SBQQ__SpecialPriceType__c = null;
            }
        }
    }     
}

TRIGGER:

trigger QuoteLineTrigger on SBQQ__QuoteLine__c (before insert, after insert, before update, after update, before delete, after delete) {
    QuoteLineTriggerDispatcher dispatcher = new QuoteLineTriggerDispatcher();
    dispatcher.dispatchEvent();
    System.Debug('inside trigger before if');
   if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
        if(checkRecursive.runOnce()){
            System.Debug('inside trigger after if');
        QuoteLineClass.UpdatePricingCategory(Trigger.new);
       QuoteLineClass.UpdateQuoteLine(Trigger.new);  
        System.Debug('inside trigger after if.... completed');
        }
    }  
}

Please help me in bulkifying it.

Thank You,
Mahesh
Hello everone, I need help on the following error message:
Reason: Apex trigger LeadConvert caused an unexpected exception, contact your administrator: LeadConvert: execution of AfterInsert

caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Your lead is missing a field mapping for the Pet Primary Caregiver,Household Allergies,Household Pets,Top Dogg K9 Exposure fields. 

LeadConvert Trigger:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
trigger LeadConvert on Lead (after insert) {

    ID acctID, ContID;
    /* Get the Id and MasterLabel from LeadStatus object */
    LeadStatus convertStatus = [ select Id, MasterLabel from LeadStatus where IsConverted = true limit 1 ];
    
  List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

  for (Lead lead: Trigger.new) {
    /* if (!lead.isConverted && lead.WebForm__c == 'Free Trial') { */
        

        try {
        Account acct = new Account(Name=lead.LastName + ' Household', recordtypeid = '0123i000000paRjAAI');
        insert acct;
        // Once the account is inserted, the sObject will be populated with an ID. Get this ID.
        acctID = acct.ID;
      Contact cont = new Contact( Contact_type__c='Applicant', AccountId=acctID);
        insert cont;
            contID = cont.ID;
      
    } catch(DmlException e) {
        System.debug('An unexpected error has occurred: ' + e.getMessage());
    }
        
        

        if (!lead.isConverted) {
      Database.LeadConvert lc = new Database.LeadConvert();
      lc.setLeadId(lead.Id);
            
            //String oppName = lead.Name;
      //lc.setOpportunityName(oppName);
      lc.setAccountId(acctID);
      lc.setConvertedStatus(convertStatus.MasterLabel);
            Database.LeadConvertResult lcr = Database.convertLead(lc);
      System.assert(lcr.isSuccess());
            
      //leadConverts.add(lc);
    }
  }

  //if (!leadConverts.isEmpty()) { 
    //List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);

Not sure if I should make these fields not required on the Salesforce side to prevent error or deactivate the trigger. Also discovered that a blank application can be submitted which should not be the case. Please advise how to remove the error message and if it should be addressed on the application side on the website or the Salesforce side. 

Hi everyone. 

Thanks in advance for your help.

I need some help to figure out how to fix the following error:

getSuspendedAsset: execution of AfterInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Case.Type Trigger.getSuspendedAsset: line 12, column 1

This is happening when I am inserting a new record in the Case object.

This is my code:

trigger getSuspendedAsset on Case (after insert) {

    static final String INVOLUNTARY = 'Reconn I bla bla bla';    
    static final String VOLUNTARY = 'Reconn V bla bla bla';
    
    Case caseToUpdate= [SELECT Id, AccountId, AssetId From Case where Id in :Trigger.new];    
    system.debug('Case Id: '+caseToUpdate.Id);
    system.debug('AccountId: '+caseToUpdate.AccountId);
    system.debug('AssetId: '+caseToUpdate.AssetId);    

    try{
    If(caseToUpdate.Type == 'Something' 
       && caseToUpdate.High_Level_Transaction_Classification__c == 'Something' 
       && (caseToUpdate.Transaction_Type__c == INVOLUNTARY || caseToUpdate.Transaction_Type__c == VOLUNTARY)){
               
           if(caseToUpdate.AccountId != null){
               List<Asset> assets = 
                   [select Id from Asset where Status__c = 'Suspended' and AccountId =: caseToUpdate.AccountId LIMIT 1]; // setting LIMIT 1 for testing purpose
                   if (assets.size() > 0){
                    for(Asset a : assets){caseToUpdate.AssetId = a.Id;}
                    update caseToUpdate;
                   }                   
           }
       }
    }
Catch(DMLException de){
    System.debug('An unexpected error has occurred: ' + de.getMessage());}
}