function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arjun SrivastavaArjun Srivastava 

Creating Merge field using Apex class

Hi Friends,

 

I have been stuck in one issue where I'm creating a mergefield in a apex controller's property and displaying it on the page.

 

Below is the code for reference:

 

public with sharing class emailtempcls {
public string temps {get;set;}
public emailtempcls()
{
    temps='{!$user.username}';
}
}

 

<apex:page Controller="emailtempcls">
{!temps}
{!$User.Username}
</apex:page>

Actual  Output:  {!$user.username}   xyz@gmail.com

Desired Output :  xyz@gmail.com  xyz@gmail.com

How can i convert temps value as the mergefield value on page?

 

Can anyone please help me in converting merge field value dynamically. Is it possible? Any Suggestions would be really appreciated.

 

 

Thanks!

 

Best Answer chosen by Arjun Srivastava
Arjun SrivastavaArjun Srivastava

Thanks for giving your time.
In class i am using a string property i.e.
string temps='{!$user.username}';

Now when using this property on vf page,i am expecting it to be rendered as Current login user name but instead it is giving me a output as : {!$user.username}.
I want to know is there any way to make this string property to be rendered as other merge fields which we normally use directly. I hope this helps.

All Answers

nitinkhunal.ax1786nitinkhunal.ax1786

Apex Class:

public with sharing class emailtempcls {
public string temps {get;set;}
public emailtempcls(ApexPages.StandardController controller)
{
User us=[Select Id, Username from User Limit 1];
temps=us.Username;
}
}

 

Visualforce page:

<apex:page standardController="contact" extensions="emailtempcls">
{!temps}
{!$User.Username}
</apex:page>

 

Try this one..

and if it works please click over right answer

Arjun SrivastavaArjun Srivastava

Thanks for the reply.
But you misunderstand my question. Above code is a dummy example.

i have scenario where i would be having mergefields in the form of string i.e. controller property which  I want to display them as the value like Normally merge field use to do.

 

 

nitinkhunal.ax1786nitinkhunal.ax1786

Would you paste your code here so i can understand your problem better?

Arjun SrivastavaArjun Srivastava

Thanks for giving your time.
In class i am using a string property i.e.
string temps='{!$user.username}';

Now when using this property on vf page,i am expecting it to be rendered as Current login user name but instead it is giving me a output as : {!$user.username}.
I want to know is there any way to make this string property to be rendered as other merge fields which we normally use directly. I hope this helps.

This was selected as the best answer
nitinkhunal.ax1786nitinkhunal.ax1786

it would help you.. This code will give you information about current user..

 

public with sharing class emailtempcls {
public string temps {get;set;}
public emailtempcls(ApexPages.StandardController controller)
{
Id Ids=UserInfo.getUserId();
User us=[Select Id, Username from User where Id=:Ids];
temps=us.Username;
}
}

Arjun SrivastavaArjun Srivastava

I don't want to fetch information neither by query nor by other method. I am creating a merge field in a string property. just wanted to know how parse it onto the vf page as a value instead of the string value itself.

testrest97testrest97

temps=userinfo.getUserName();

 

if you are doing this temps='{!$user.username}', you are converting it to a string and displaying the string.

~Onkar~Onkar

public with sharing class comm {
public string temps {get;set;}
public comm()
{
temps= userinfo.getUserName();
}
}

 

Note : Components such as s-controls, custom buttons, custom links, formulas, and Visualforce pages allow you to use special merge fields to reference the data in your organization. Use the following global variables when choosing a merge field type to add to your custom component:(Merge field can not be use in Apex class)

 

To Access these same property salesforce in apex class salesforce  has "the system static methods for UserInfo."

 

pooja@magichorse.compooja@magichorse.com

Hi...

 

Try this...

 

public with sharing class emailtempcls {
public string temps {get;set;}
public emailtempcls()
{
    temps = UserInfo.getUserName();
}
}