0

Im trying to use the instanceof keyword in a visualforce template. I would like to pull the MobilePhone field from the Related Owner record. This works when accessed in apex but is not working in the visualforce expression.

VF Page:

<messaging:emailTemplate subject="request {!relatedTo.Contact__r.Name}" recipientType="Contact" relatedToType="Reference_Request__c">
    <messaging:htmlEmailBody >
    <c:RGComponent refRefId="{!relatedTo.Id}" />
    </messaging:htmlEmailBody>
</messaging:emailTemplate>

RGComponent:
<apex:component access="global" controller="RGController">
    <apex:attribute name="refRefId" type="String" description="This is the Id of the account" assignTo="{!ref}" access="global" />
    <table class="table" width="50%" align="left">
        <tr valign="top"><td>Account Name:</td> <td>{!ref.Account__r.Name}</td></tr>
<tr><td>Request owner phone number:</td><td>{!ref.Owner.Phone}</td></tr>
        <tr valign="top"><td>Owner Mobile Phone</td> <td>{!IF((referenceReq.Owner instanceof User), referenceReq.Owner.MobilePhone, '')}"</td></tr>
    </table>
</apex:component>


RGController:
public class RGController {
    public String ref{get;set;}
    
    public Reference_Request__c referenceReq{
    get {
            referenceReq = [Select Id, Contact__r.email,Contact__r.Name,Account__r.Name
            TYPEOF Owner WHEN User THEN Id, Name, Email,Phone,MobilePhone WHEN Group THEN Id,Name ELSE Id,Name END
            from Reference_Request__c WHERE Owner.Type = 'User' limit 1];
            return referenceReq; 
        }
    set;
    }
}
  1. {!ref.Owner.Phone} - works fine
  2. {!IF((referenceReq.Owner instanceof User), referenceReq.Owner.MobilePhone, '')} - Does NOT Work.

anyone on here tried something similar and got it working?

the below works fine in apex but when im trying to use a similar function in VF it does not work.

List<Reference_Request__c> reqlist = [SELECT Id, Contact__r.Name, 
TYPEOF Owner
WHEN User THEN Id, Name, MobilePhone
WHEN Group THEN Id,Name
ELSE Id,Name
END
FROM Reference_Request__c WHERE Owner.Type = 'User'];

for (Reference_Request__c r: reqlist) {
    if (r.Owner instanceof User) {
        User u = r.Owner;
        System.debug('User:=>'+ r.Id + ':'+ u.Name +' : '+ u.MobilePhone);
    }
}
2

instanceOf is strictly an Apex language feature. You can't use it in Visualforce. Owner is a Name object, so you can use the Type field:

{!IF(referenceReq.Owner.Type='User',referenceReq.Owner['MobilePhone'],'')}
1
  • Thanks for your reply. that did it! – roy Jun 18 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.