I have written a APEX Class and when I copy the code and paste in Developer Console > Debug > Open Execute anonymous window, I get expected results. However, when I call the apex class from VisualForce component, I am not getting desired result. The class is supposed to return the Name of a borrower of a book based on given book name. But the borrower's name is returned as blank when called from VF component. Here is my code -
Background: I have two master data tables - Books and Members. Two transaction tables - Book_Issue and Issue_Details. Relationship between Book Issue and Issue Details is Master-Detail, i.e. One to Many. Book Issue has a lookup field on Members. Issue Details has lookup field on Books.
Requirement: When a Book is overdue, system will generate an email to the Member. On the email it will address the Member as "Dear ". The Member Name will be determined like this - based on Book Name get the Book Issue name. Based on Book Issue name get the Member name.
Here is my code - APEX class
public with sharing class BookReturnReminder8 {
public string IssueHdr;
public string MemberName;
public Book_Issue__c BookIssue;
public void setIssueHdr(String s) {
IssueHdr = s;
}
public String getMemberName() {
List < Book_Issue__c > rsBookIssue = new List < Book_Issue__c > ();
rsBookIssue = [select id, name, member__r.name__c from Book_Issue__c
where name =: IssueHdr
];
System.Debug('Rowset count ' + rsBookIssue.size());
if (!rsBookIssue.isEmpty())
BookIssue = rsBookIssue[0];
else
BookIssue = new Book_Issue__c();
MemberName = BookIssue.member__r.name__c;
return MemberName;
}
}
Here is code for VisualForce component
<apex:component controller="BookReturnReminder8" access="global">
<apex:attribute name="BookIssueHdr" type="string" description="Book Issue Header" assignTo="{!IssueHdr}" />
<!-- Begin Default Content REMOVE THIS -->
Dear {!MemberName} Please refer to Book Issue number {!BookIssueHdr}
<!-- End Default Content REMOVE THIS -->
</apex:component>
Here is my VisualForce email template -
<c:Book_Return_Reminder01_vfc BookIssueHdr="{elatedTo.book_issue_num__r.name}" />
The book {!relatedTo.Book__r.Name} issued to you was due for return on {!relatedTo.Due_Date__c}.
Please return the same immediately.
Thanks
{!relatedTo.CreatedBy}
Sample email message -
Dear
<expect Member Name here>
Please refer to Book Issue number BK2016-0002The book Madame Bovary issued to you was due for return on Wed Sep 28 00:00:00 GMT 2016. Please return the same immediately.
Thanks
00528000001rhGCAAY
When I type the following code on in Developer Console > Debug > Open Execute anonymous window, I get expected results.
BookReturnReminder8 testclassA = new BookReturnReminder8();
testclassA.SetIssueHdr('BK2016-0002');
string MemberName = testclassA.getMemberName();
System.debug ('Member Name: ' + MemberName);
From the Logs window, I have opened the log, checked on Debug only. I get
[DEBUG]Rowset count 1
[DEBUG] Member Name: Barker, L
Any help is appreciated. Sorry for the long post. I am new to the forum, if I have violated any rules / policy, please advise. Thanks
member
object. Next would be sharing rules related to it. I suggest you test this page logged in as a "member". You can also try the page using the controller set for "without sharing" to help track down the source of the sharing/permissions issue if removing with sharing solves the problem as you don't really want to leave it in that state.BookIssueHdr="{relatedTo.book_issue_num__r.name}
a lookup fromrelatedTo
or a reference to a child relationship? If the latter, this won't work