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
JAMBERT JESUDASANJAMBERT JESUDASAN 

Getting one record(by lastmodifieddate) from existing Accounts

Hi,
My scenario, While uploading a bulk accounts records, If the email or phone matched with Existing account means, I need to update(most recent lastmodified) account record. 

i having 6 existing accounts with an email 'test@abc.com'. now i am inserting bulk accounts and in the sheet one record have an email 'test@abc.com. So how to retrive the most recent modified record to update?
Amit Singh 1Amit Singh 1
Hi Jambert,

Use ORDER BY clause in your SOQL to get the latest account record.
 
Order By LastModifiedDate DESC LIMIT 1

SOQL be like

SELECT id, Name, LastModifiedDate FROM Account Order By LastModifiedDate DESC LIMIT 1
Let us know if this helps :)

Thanks,
Amit
 
JAMBERT JESUDASANJAMBERT JESUDASAN
Hi Amit,

If i use LIMIT as 1, Then the query result will be 1 record. But I am inserting bulk records, So what happens in the sheet having more than 1 records? 
example,
for test@abc.com --> Existing 5 Accounts, test1@abc.com --> existing 3 accounts, test2@abc.com --->  existing 4 accounts, etc..  For this, If i use LIMIT as 1,  I guess the logic work for test@abc.com (or any one), and Remaining will be skipped right? 

Thanks!
Amit Singh 1Amit Singh 1
If you want to work with all the account record then remove the limit from SOQL query

Thanks,
JAMBERT JESUDASANJAMBERT JESUDASAN

I tried. If i removed the Limit, It will updates all the exisiting accounts

test@abc.com -- It updates All existing 5 accounts.(need to update 1st record by desc modifieddate) 
test1@abc.com -- It updates All exisitng 3 Accounts.(need to update 1st record by desc modifieddate) 
test2@abc.com -- It updates All exisitng 4 Accounts. (need to update 1st record by desc modifieddate) 

Thanks