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

Test class doesn't get ref_r.id even if I've assigned ref__c='someId'
Hi all ,
I'm writing a test class for a controller, And test class is like this..
Please note that hard coding values isn't good at all, but i'll change it later, this is just for the time being..
myType__c Obj= new myType__c();
Obj.Name='Test Object';
Obj.ST_Ref__c='a2Ne0000000JAUo';
Obj.A_Ref__c='001e000000HPgd1';
insert Obj;
system.debug('Test SO area '+Obj.Service_Offering_Ref__r.id); ----------------> But still this is null :(
In my controller class i've a code like
String SOID=Obj.ST_Ref__r.id; and i do pass this SOID to a query
Currently query doesn't give any result , because SOID is null
But i've assigned
Obj.ST_Ref__c='a2Ne0000000JAUo';
Whats wrong with this??
Please help me, Thanks a lot
I'm writing a test class for a controller, And test class is like this..
Please note that hard coding values isn't good at all, but i'll change it later, this is just for the time being..
myType__c Obj= new myType__c();
Obj.Name='Test Object';
Obj.ST_Ref__c='a2Ne0000000JAUo';
Obj.A_Ref__c='001e000000HPgd1';
insert Obj;
system.debug('Test SO area '+Obj.Service_Offering_Ref__r.id); ----------------> But still this is null :(
In my controller class i've a code like
String SOID=Obj.ST_Ref__r.id; and i do pass this SOID to a query
Currently query doesn't give any result , because SOID is null
But i've assigned
Obj.ST_Ref__c='a2Ne0000000JAUo';
Whats wrong with this??
Please help me, Thanks a lot
Obj.ST_Ref__c='a2Ne0000000JAUo';
you are trying to accessing a data from the organisation, you can't access data without using the seealldata= true on the test class
but please take into consideration that this is not a best practice salesforce.
Regards
All Answers
Also you're trying to debug a field on your record that you've never set before.
Please provide more code from both your test class and controller, it's not very clear from what you've provided at this stage...
you can't access the parent object of the object myType__c directly you need to do a select.
Example of the code will be
myType__c Obj= new myType__c();
Obj.Name='Test Object';
Obj.ST_Ref__c='a2Ne0000000JAUo';
Obj.A_Ref__c='001e000000HPgd1';
insert Obj;
myType__c selectedtObj=[select id,name,Service_Offering_Ref__r.id
from myType__c
where id = :Obj.Id];
system.debug('Test SO area '+selectedtObj.Service_Offering_Ref__r.id);
also in your controller if you need to access the parent object you need to do a select statement.
but my question for you is why you don't access ST_Ref__c value directly ?? and no need to do a select.
Regards
Thanks for your reply.
In my controller class that is how we have taken value
String SOID=VendorRegObject.Service_Offering_Ref__r.id; --> controller class
We have used a qury before we take the value of SOID. which works fine in controller class..
But in test class it doesnt run after that code...
Obj.ST_Ref__c='a2Ne0000000JAUo';
you are trying to accessing a data from the organisation, you can't access data without using the seealldata= true on the test class
but please take into consideration that this is not a best practice salesforce.
Regards
Thanks a lot for your reply.. Yes i used seealldata=true and those codes got covered. Now best pratice would be
i) make seealldata=false and
ii) insert a ST_Ref__c type object record before Obj is insert and
iii) get that ID and assigned to the Obj.ST_REF__C=STObj.id;
Am i correct??
Pleaes help me, your replies are really helpful
seealldata is by default false, yes you are right you should insert ST_Ref__c before and then use the id.
This is the best practice, while working with test class
Regards,
Anyway i already got what i needed
Thanks