I have been reading "Understanding Testing in Apex". In the section entitled Understanding Test Data, there is a sentence that reads
If a test makes a Visualforce request, the executing test stays in the
test context but runs in a different thread, so test data isolation is
no longer enforced.
This is interesting and I would like to write a test class that illustrates this concept, but I am perplexed by the very first clause of the sentence: "If a test makes a Visualforce request...". How does one do this?
PageReference pageRef = new PageReference('/apex/testVFPage');
This is how request a vf request. Response of the resulting page will be store in 'pageRef'. PageReference is used for this. The best practise to have a vf page rquest is as below.
PageReference pageRef = Page.testVFPage;
It allows the Force.com platform to detect the page dependancy.
If you need to do vf request for code coverage of your controller. Then do like this.
Test.setCurrentPageReference(Page.testPage); //vf page request
ApexPages.currentPage().getParameters().put('id',ord.Id); //pass parameters if needed
Related
I have an assignment.
I need to send to different contacts the participants associated with it using visualforce template in pdf format.
I know how send email to different recipients, but i dont know how to send with different text. When I do SOQL in my controller I have List of List (Contacts and Participants) but how send to different contacts with different text body i dont know
Visualforce email template can't have an apex controller like say a vf page. BUT it can include a vf component - and that component can have controller and accept parameters!
It's bit trickier than usual because you'd need to move some logic to getter but it's doable. Make a component, pass to it "related to.Id" or something and it'll work
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm
https://salesforce.stackexchange.com/q/245930/799
There's my ancient answer https://salesforce.stackexchange.com/q/4303/799 if you're still stuck, overcomplicated for what you need but hey, gives some ideas.
Remember that each such email + component + controller will count to limit of soqls. If it uses 1 query (gimme participants for this contact id) - that's max 100 emails in single transaction.
The functionality I have written works, this is for a test method.
In apex test code, I create a StandardController then a controller extension and invoke a method on that controller extension which returns a PageReference to a custom object's standard edit page. I want to change field values on that edit page and invoke the Save method/button, is this possible and how?
In advance, thanks.
No, you cannot change values in a standard edit screen from within a test method, nor do you need to. The correct way to test logic that is executed after saving on a standard edit screen is to construct an instance of a new record, populate the fields, and insert it into the database.
I am new to Playframework and AngularJS and developing an application in AngularJS on the front end and Playframework on the back end.
I am facing one scenario: when the user clicks a menu link (Show students), it should show all the students. In the play action, the data is sent as json. Now the problem is if the data is sent as Json, then I don't see the option for which page it should forward to.
Alternatively, I have to use the normal playframework style like forwarding to page with list of values.
//This is the current playframework code I use. Here I set the values to students form
// This is not the way I want
public static Result getAllEmployees(){
List<Student> all = Student.find.all();
JsonNode json = Json.toJson(all);
return ok(views.html.students.render(all));
}
//This is the way I want
public static Result getAllEmployees(){
List<Student> all = Student.find.all();
JsonNode json = Json.toJson(all);
return ok(json); // no option for specifying the page.
}
Is there anyway to do it?
They are both valid approaches to returning data, but the first returns a whole html page, for example in response to a #routes.Employees.getAllEmployees reference in your view. The second one returns just a Json content type response, for example in response to a Javascript request or AngularJS $http.get. The methods need to have different names (they can't both be called getAllEmployees). Both of them will have entries in the routes file. They will be different entries the way you have written them, so perhaps something like:
GET /employee controllers.Employees.getAllEmployees
GET /api/employee controllers.Employees.apiGetAllEmployees
(Disclaimer: I haven't done this in AngularJS yet, but the principle should be similar.)
I am trying to extend the simple blog tutorial found in the documentation.
I have an index.ctp on /app/View/Users/index.ctp with all users being shown with a link on each username.... When i click on the username, I expect it to go to the view.ctp and show each user's blog posts and the number of posts each user has done.
I get an error here. Do I need to write a component for this? If so, how do I go about it?
class UsersController extends AppController {
public function view($id = null) {
$this->set('allposts',$this->Post->findByUserId($id));
}
}
So I get an error at this point because it cannot see the Post model in the user model. How do I go around this?
Thanks....
No, you don't need a component for that. If you have linked your models correctly (that's it, set belongsTos and hasMany(es) etc etc), then you have to access the post by concatenating.
$this->set('allposts',$this->User->Post->findByUserId($id));
Please see other questions regarding this.
Now, extending this a little bit since you seem a bit confused on what Components actually do. Components are a way to extending or adding functionalities to Controllers. If you want to, say, create a new Paginator, or call Facebook from your controller to get info, and there's no definite place to put that (users can have facebook info, but if you have another controller named "Friends", you might have to put some facebook functions there too).
Normally, if you think you need a component to have calls from one model to another, that's a sign you are organizing things wrongly (not a general rule, I'm open to examples on when this isn't true).
i have a form for adding people object ,i want to add as many people without saving people in database,after clicking add people form submit button,the same form should appear without saving the form data to database,instead it should save to the session.please explain in detail if possible with the help of a example to do this, i have not found any tutorial which explains cakephp session in detail.
Vinay,
check out the official examples.
But you should definitly start with the easy tutorials here and dissect these. You will learn form handling and later can put it together with the sessions.
Sessions are simple. You include the component, write something to the session and read it from the session later. That's it. You can store in it almost anything you want, including arrays.
class FooController extends AppController {
public $components = array('Session');
public function foo() {
$this->Session->write('some.key', 'some value');
}
public function bar() {
$baz = $this->Session->read('some.key');
}
}
See http://book.cakephp.org/view/1311/Methods