• Zahir Koonari
  • NEWBIE
  • 15 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
 I am calling the below class in user after insert trigger, my requirement is to findout if this is the first portal user created for an account. if so I should stamp today's date to a field say first_portal_user_created_date. I know this code is not that efficient as there is SOQL query inside the for loop. Is there a better way of doing this ?  


public class FristPortalUserCreatedDate{

      public static void updateFirstPortalUser(List<User> newUsers) {

    List<Account> AccountsToUpdate = new List<Account>();
        
            for (Integer i = 0; i < newUsers.size(); i++) {
            
                Account selectedAccount = [select id,Portal_user_created_date__c From Account where Id=:newUsers[i].AccountId];
                if(selectedAccount!= NULL &&  selectedAccount.Portal_user_created_date__c=NULL ){

            selectedAccount.Portal_user_created_date__c = system.Today();
            AccountsToUpdate.add(selectedAccount);
                    
                }
            
            }
        Update AccountsToUpdate;
      }
  
  }
 I am calling the below class in user after insert trigger, my requirement is to findout if this is the first portal user created for an account. if so I should stamp today's date to a field say first_portal_user_created_date. I know this code is not that efficient as there is SOQL query inside the for loop. Is there a better way of doing this ?  


public class FristPortalUserCreatedDate{

      public static void updateFirstPortalUser(List<User> newUsers) {

    List<Account> AccountsToUpdate = new List<Account>();
        
            for (Integer i = 0; i < newUsers.size(); i++) {
            
                Account selectedAccount = [select id,Portal_user_created_date__c From Account where Id=:newUsers[i].AccountId];
                if(selectedAccount!= NULL &&  selectedAccount.Portal_user_created_date__c=NULL ){

            selectedAccount.Portal_user_created_date__c = system.Today();
            AccountsToUpdate.add(selectedAccount);
                    
                }
            
            }
        Update AccountsToUpdate;
      }
  
  }