function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zahir KoonariZahir Koonari 

Updating a field in account object when a portal user is created for that account

 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;
      }
  
  }
Best Answer chosen by Zahir Koonari
rajat Maheshwari 6rajat Maheshwari 6

Hi Zahir,

To avoid SOQL query unside for loop., please use below snippet - :

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

    List<Account> AccountsToUpdate = new List<Account>();
set<Id> set_Id = new Set<Id>();
        
            for (User u : newUsers) 
             {
                set_Id.add(u.AccountId);
             }
            
                Account selectedAccount = [select id,Portal_user_created_date__c From Account where Id IN:set_Id];


                if(selectedAccount!= NULL &&  selectedAccount.Portal_user_created_date__c=NULL ){

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

Please let me know the results :)

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

All Answers

sfdc loginsfdc login
Is this code working 
Zahir KoonariZahir Koonari
This code is not working its giving a mixed DML operation exception ... looks like we cannot update an account at the same context when a user record is inserted. I think I need to add an @future anotation before the update .. Actually my concern was how can I avoid the SOQL query inside the for loop. 

Many Thanks
rajat Maheshwari 6rajat Maheshwari 6

Hi Zahir,

To avoid SOQL query unside for loop., please use below snippet - :

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

    List<Account> AccountsToUpdate = new List<Account>();
set<Id> set_Id = new Set<Id>();
        
            for (User u : newUsers) 
             {
                set_Id.add(u.AccountId);
             }
            
                Account selectedAccount = [select id,Portal_user_created_date__c From Account where Id IN:set_Id];


                if(selectedAccount!= NULL &&  selectedAccount.Portal_user_created_date__c=NULL ){

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

Please let me know the results :)

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
This was selected as the best answer
Zahir KoonariZahir Koonari
Hi Rajat,

Your answer did really help me a lot to get rid of the SOQL from the for loop. Thank you so much for that. I tweeked this code a bit for bulkify operation and also I had to pass Ids of the accounts to this function from the trigger itself ... the reason being I had to use @future anotation as you know we cannot update a setup and non setup object in the same transaction. 

Anyway your answer helped me a lot. 

Thanks 
Zahir