4

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-0002

The 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

6
  • 1
    Who is the email being sent from? What profile does that user have? Does that user have access to the member in question?
    – Nick C
    Commented Oct 28, 2016 at 12:36
  • There could be a number of reasons for this, all surrounding permissions and sharing. First would be OWD on the 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.
    – crmprogdev
    Commented Oct 28, 2016 at 15:50
  • is BookIssueHdr="{relatedTo.book_issue_num__r.name} a lookup from relatedTo or a reference to a child relationship? If the latter, this won't work
    – cropredy
    Commented Oct 28, 2016 at 17:11
  • @cropredy, book_issue_num__r represents the parent relationship of the child record. The child record here the Issue_Details where every record represents a book under the Issue Header. Goal of this code line is - to get the identifier of the Issue Header record, which is a AutoNumber field in my example, i.e. BK2016-0002
    – Guha
    Commented Oct 29, 2016 at 6:58
  • @NickCook, I was logged in as myself, with System Administrator profile. On the VisualForce Email Template page, there is a button "Send Test and Verify Merge Fields". I click on that button. On the popup window there is a field User. I enter my USER ID. And under Related To Record there is a field to select a record from object Book_Issue_Detail__c. I select a transaction from this object. Then Click OK. The system prepares an email where all the merge fields are brought across except Member Name.
    – Guha
    Commented Oct 29, 2016 at 13:35

1 Answer 1

1

Possibly what is happening here is that getMemberName is getting called when IssueHdr is null/not set. I've come across this type of behaviour before when using components I've never really taken the time to understand what the issue is but always found something like this alleviates the issue:

public with sharing class BookReturnReminder8 {
    public string IssueHdr;
    public string MemberName {get;set;}
    public Book_Issue__c BookIssue;
    public void setIssueHdr(String s) {
        IssueHdr = s;
        MemberName = setMemberName();
    }
    private String setMemberName() {
        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;
    }
}

Also, not sure about this -

        MemberName = BookIssue.member__r.name__c;

Isn't that going to throw an NPE if rsBookIssue IS empty?

3
  • thank you for your code. But I think while testing from the VisualForce Email Template page, by clicking on the button "Send Test and Verify Merge Fields", APEX class is not invoked.
    – Guha
    Commented Oct 31, 2016 at 10:30
  • I have created a new class as below, based on your code public with sharing class BookReturnReminder10 { public string IssueHdr; public string MemberName {get;set;} public Book_Issue__c BookIssue; public void setIssueHdr(String s) { IssueHdr = s; MemberName = 'Barker, L'; // just testing VF component and VF email template for Book Return Reminder } }
    – Guha
    Commented Oct 31, 2016 at 10:35
  • Here the Member Name is hard coded. No SOQL, no question of security. Yet no difference. Though when I paste the following on Debug window, actual result matches with Expected BookReturnReminder10 testclassA = new BookReturnReminder10(); testclassA.SetIssueHdr('BK2016-0002'); string MemberName = testclassA.MemberName; System.debug ('Member Name: ' + MemberName);
    – Guha
    Commented Oct 31, 2016 at 10:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .