You need to sign in to do that
Don't have an account?

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);
}
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);
}
User uid = [select id,name,Emp_Id__c from user where Emp_Id__c like : s];
All Answers
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;
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?
Can you check what value is there ??
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 :)
User uid = [select id,name,Emp_Id__c from user where Emp_Id__c like : s];