• Indira Reddy 1
  • NEWBIE
  • 10 Points
  • Member since 2020

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

I modified a rollup trigger I found online for our use case. Below trigger rolls up the count of subscriptions on Account. We have around 1.5 million subscriptions and an account can have up to 30,000 subscription records. It works fine when I manually update a child record. However, it's throwing an error,1. When I update "Update_for_Sub_Trigger__c" field using data loader in 10k records batch, it's throwing "system.limitexception: Apex CPU time limit exceeded" 2. When I update fields that are not referenced in the trigger update section of the code (using a dataloader), it's throwing error- "system.limitexception: too many soql queries: 101". I am a novice Apex developer and not able to understand the reason for these errors.  Can someone let me know if I am coding it the wrong way? Thank you!

trigger SubscriptionCount on Subscription__c (after insert, after update, after undelete, after delete) {
   
    List <Subscription__C> newSubscriptionList = new List<Subscription__C>();
    Set<Id> accountIdsSet = new Set<Id>();
   
    if ( Trigger.isDelete ) {
        newSubscriptionList = Trigger.Old;
    } else {
        newSubscriptionList = Trigger.New;
    }      
    for ( Subscription__C Sub : newSubscriptionList ) {
        if ( Trigger.isUpdate ) {
            Subscription__C oldSub = (Subscription__C)Trigger.oldMap.get(Sub.Id); // Map<Id, sObject>
           
            if (( oldSub.Account__c != Sub.Account__c )|| ( oldSub.Update_for_Sub_Trigger__c != Sub.Update_for_Sub_Trigger__c)){
                accountIdsSet.add(oldSub.Account__c);
                accountIdsSet.add(Sub.Account__c);
            }
        }
        else {
           
            if (Sub.Account__c != null ){
                accountIdsSet.add(Sub.Account__c);
            }
        }
       
           
       
               
        List<Account> accountList = [Select Id,  Total_Subscriptions__c, (Select Id From Subscriptions__r)
                                     From Account Where Id IN : accountIdsSet];
       
        for (Account acc : accountList) {
            List<Subscription__C> relatedSub = acc.Subscriptions__r;
            if ( relatedSub != null ){
                acc.Total_Subscriptions__c = relatedSub.size();
            } else {
                acc.Total_Subscriptions__c = 0;
            }
        }
       
        if(accountList.size() > 0){
           
            update accountList;
        }
       
       
    }
}
 
Hi All,

I am trying to roll up the count of a custom object on the parent account. The custom object is related to Account via lookup relation. It's working fine on the update, insert and delete but not working on Account merge. As per Salesforce guide child object triggers are not fired on Merge. Did anyone have a similar issue like this? If yes, please suggest if there is any workaround. Thank you!
Hi All,
I am new to Apex and trying to convert Euro to USD and save it in a custom field 'measure'. Here is my code-
CurrencyType conversionRate = [SELECT conversionrate from currencytype where isocode = 'Eur'];
 newProduct.measure__c.= opplineItem.Price / (conversionRate);  
I get the following error 'Arithmetic expressions must use numeric arguments'. I need to convert conversionRate to decimal. Is there anything like integer.valueof() to convert to decimal.
Thank you
Hello Everyone,

I modified a rollup trigger I found online for our use case. Below trigger rolls up the count of subscriptions on Account. We have around 1.5 million subscriptions and an account can have up to 30,000 subscription records. It works fine when I manually update a child record. However, it's throwing an error,1. When I update "Update_for_Sub_Trigger__c" field using data loader in 10k records batch, it's throwing "system.limitexception: Apex CPU time limit exceeded" 2. When I update fields that are not referenced in the trigger update section of the code (using a dataloader), it's throwing error- "system.limitexception: too many soql queries: 101". I am a novice Apex developer and not able to understand the reason for these errors.  Can someone let me know if I am coding it the wrong way? Thank you!

trigger SubscriptionCount on Subscription__c (after insert, after update, after undelete, after delete) {
   
    List <Subscription__C> newSubscriptionList = new List<Subscription__C>();
    Set<Id> accountIdsSet = new Set<Id>();
   
    if ( Trigger.isDelete ) {
        newSubscriptionList = Trigger.Old;
    } else {
        newSubscriptionList = Trigger.New;
    }      
    for ( Subscription__C Sub : newSubscriptionList ) {
        if ( Trigger.isUpdate ) {
            Subscription__C oldSub = (Subscription__C)Trigger.oldMap.get(Sub.Id); // Map<Id, sObject>
           
            if (( oldSub.Account__c != Sub.Account__c )|| ( oldSub.Update_for_Sub_Trigger__c != Sub.Update_for_Sub_Trigger__c)){
                accountIdsSet.add(oldSub.Account__c);
                accountIdsSet.add(Sub.Account__c);
            }
        }
        else {
           
            if (Sub.Account__c != null ){
                accountIdsSet.add(Sub.Account__c);
            }
        }
       
           
       
               
        List<Account> accountList = [Select Id,  Total_Subscriptions__c, (Select Id From Subscriptions__r)
                                     From Account Where Id IN : accountIdsSet];
       
        for (Account acc : accountList) {
            List<Subscription__C> relatedSub = acc.Subscriptions__r;
            if ( relatedSub != null ){
                acc.Total_Subscriptions__c = relatedSub.size();
            } else {
                acc.Total_Subscriptions__c = 0;
            }
        }
       
        if(accountList.size() > 0){
           
            update accountList;
        }
       
       
    }
}
 
Hi All,
I am new to Apex and trying to convert Euro to USD and save it in a custom field 'measure'. Here is my code-
CurrencyType conversionRate = [SELECT conversionrate from currencytype where isocode = 'Eur'];
 newProduct.measure__c.= opplineItem.Price / (conversionRate);  
I get the following error 'Arithmetic expressions must use numeric arguments'. I need to convert conversionRate to decimal. Is there anything like integer.valueof() to convert to decimal.
Thank you