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
Swamy P R NSwamy P R N 

How to get records based on Autonumber field

HI Guys,

i implemented a webservice class,it is going to retrieve records based on autonumber field. for the main class its executed successfully but it was not able to retrieve any records in test classes.

Auto number field is in User object,for this i inserted a user and than am querying on that particular user record but it was showing " System.QueryException: List has no rows for assignment to SObject "

test class :
Profile p = [select id from profile where name='Standard User'];

            User testUser= new User(alias = 'u1', email='u1@swa.com',
              emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
              localesidkey='en_US', profileid = p.Id, country='United States',
              timezonesidkey='America/Los_Angeles', username='u1@swa.com');
        
            insert testUser;
rewCls.empid==testUser.Emp_Id__c;--->Am i able to get that autonuber field after inserting the user ?
cls_RecieveAndSend_DatatoSrisys.doPost();


class:
global static MyUserDefinedClass doPost(){

if(rewCls.month==null && rewCls.year!=null && rewCls.empid!=null){
           System.Debug('Users Emp Id is '+rewCls.empid);
           
            User u = [select id,name from user where Emp_Id__c=:rewCls.empid];
            System.Debug('Users Name :'+u.Name);
}
Best Answer chosen by Swamy P R N
Swamy P R NSwamy P R N
I tried few different options and when I used like in the SOQL, I got back the value..
User uid = [select id,name,Emp_Id__c from user where Emp_Id__c like : s];

All Answers

Vinit_KumarVinit_Kumar
The key here is to query the auto number field after inserting the User record and then use that value to assign it to your other varibable.

so,before this line in your code :-

rewCls.empid==testUser.Emp_Id__c;

just put this line:-

String c = [select Emp_Id__c from User where id=:testUser.id]; // declare the variable based on your data type of Auto number field.

and then,
rewCls.empid = c;
Swamy P R NSwamy P R N
Hi ,

In test class i was inserting one sample user.so when i queried based on that inserted id,am getting the record.
But when am querying with the AutoNumber field i was not able to see any record.

Profile p = [select id from profile where name='Standard User'];

        1)    User testUser= new User(alias = 'u1', email='u1@swa.com',
              emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
              localesidkey='en_US', profileid = p.Id, country='United States',
              timezonesidkey='America/Los_Angeles', username='u1@swa.com');
        
            insert testUser;
           
        2)    User usrempid = [select Id,Emp_Id__c from User where id=:testUser.Id];
            System.Debug('Emp Id is***'+usrempid.Emp_Id__c);
           
            string s=usrempid.Emp_Id__c;
            System.Debug('Variable Id is ***'+s);
           
         3)   User uid = [select id,name,Emp_Id__c from user where Emp_Id__c=:s];
            System.Debug('Passed Query ***'+uid.id);
\
Emp_Id__c---->Auto number field

In 1st point am inserting a user,am retrieving in 2nd , 3rd points.In 2nd point i was able to see the record but in 3rd point am not able to see record i.e, am getting null.Is any Autonumber effect?
Vinit_KumarVinit_Kumar
Are you querying the 3rd one after assigning value in String s. I mean is there value in String s already which you are using in your where filter .

Can you check what value is there ??
Swamy P R NSwamy P R N
Previously there is no value,only whatever is there in " usrempid.Emp_Id__c; " thats only.
Vinit_KumarVinit_Kumar
Autonumber fields are populated at Run time.

So,if the Emp_Id__c does not have any value to it how do you expect it to return the record.Use the way I suggested you,that should work :)
Swamy P R NSwamy P R N
In debug log " s" having  value..but it was not getting that entire record i.e, 3rd point 
Swamy P R NSwamy P R N
I tried few different options and when I used like in the SOQL, I got back the value..
User uid = [select id,name,Emp_Id__c from user where Emp_Id__c like : s];
This was selected as the best answer