Skip to main content
 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 populate a field say first_portal_user_created_date and stamp todays date. I know this code is not that efficient as there is SOQL query and DML operation inside the for loop. Is there a better way of doing this ?  

 

public class FristPortalUserCreatedDate{

 

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

 

        

 

            for (Integer i = 0; i < newUsers.size(); i++) {

 

            

 

                List<User> ExistinUsers = [select id,AccountId,Name From User where AccountId=:newUsers[i].AccountId];

 

                if(ExistinUsers.isEmpty()){

 

                    Update new Account(Id=newUsers[i].AccountId,Portal_user_created_date__c = system.Today());

 

                }

 

            

 

            }

 

      }

 

  

 

  }
1 answer
  1. Mar 2, 2017, 2:13 PM
    This Answers Community is focused on configuration and design questions. Programmatic questions are best submitted to the developer forums at https://developer.salesforce.com where the forums and participants are geared toward programming troubleshooting and support. 
0/9000