You need to sign in to do that
Don't have an account?
Zahir 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;
}
}
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;
}
}
Hi Zahir,
To avoid SOQL query unside for loop., please use below snippet - :
Please let me know the results :)
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
All Answers
Many Thanks
Hi Zahir,
To avoid SOQL query unside for loop., please use below snippet - :
Please let me know the results :)
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
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