• vikram fartyal
  • NEWBIE
  • 30 Points
  • Member since 2014
  • Sr. Software Engineer
  • Metacube Software (P) Ltd.


  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
Hi folks,
       Can anyone tell me the purpose of visualforce remoting and how use that?


Thanks in advance
Karthick
Hi,

I have been trying to send an email by using visual force template, but not got success. I created a component and used it in my VF Template, but when i use it in apex class and try to send email either it gives the error or send mail with no data.

My Controller Class Code:
private void sendEmailNotifications(Map<Id, Store__c> mapRf, EmailTemplate emailTemp) {    	
    	List<Messaging.SingleEmailMessage> mailsToSend = new List<Messaging.SingleEmailMessage>();
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            
    	for(Id rfId : mapRf.keySet()){
        	Store__c r = mapRf.get(rfId);
        	
        	// fetch list of the uses to whom mails gonna sent
        	List<Id> uIds = new List<Id>();
        	if(mapGrpEmail.containsKey(mapGrpNameRfId.get(rfId))){
        		for(User u : mapGrpEmail.get(mapGrpNameRfId.get(rfId))){
                	uIds.add(u.Id);
                }
        	}
           
            email.setTargetObjectId('005w0000003dyDo');
            String[] toAddresses = new String[] {'abc@xyz.com'}; 
            email.setToAddresses(toAddresses); 
            //email.setWhatId(rfId) ;
			email.setSaveAsActivity(false);
			email.setTemplateId(emailTemp.Id)			
			mailsToSend.add(email);
        }

i am setting userId in setTargetId() method, so if i set Store__c.Id in setWhatId() method it gives me following error:

System.EmailException: SendEmail failed. First exception on row 1; first error: INVALID_FIELD_WHEN_USING_TEMPLATE, When a template is specified the plain text body, html body, subject and charset may not be specified : []

And if i don't set WhatId how i gonna get merged fields to be populated in my template, which is quite annoying. I read somewhere that WhatId supports only When a ContactId is set into setTargetId() but i simply need to UserId.

Here is my template:
 
<messaging:emailTemplate subject="Store - Created" recipientType="User" relatedToType="Store__c">
    <messaging:htmlEmailBody >
        <c:RedFlagComponent rfId="{!relatedTo.Id}" flag="true" /><br/><br/>
    </messaging:htmlEmailBody>
</messaging:emailTemplate>
Here is Componenet:
 
<apex:component controller="RedFlagComponentController" access="global">
    <apex:attribute name="sId" type="Id" description="Id of the store" assignTo="{!storeId}"/>{!isNew}
    
    	<table>
    	<apex:repeat value="{!lstRfs}" var="r">
	    	<apex:outputText rendered="{!isNew}" value="A red flag notification has been raised in {!r.Store__r.Name}, {!r.Address_Concatenated__c }/{!r.External_Reference__c} on {0,date,dd/MM/yyyy}." >
	    		<apex:param value="{!TODAY()}" />
	        </apex:outputText>
	      
	        <br/>
	        <hr/>
		        <tr>
		            <td>Priority: {!r.Priority__c}</td>              
		        </tr>		        
		        <tr>
		            <td>Link to the latest visit: <a href="{!orgUrl}{!r.Photo_Attachments__r[0].Visit__c}" target="_blank">{!r.Photo_Attachments__r[0].Visit__c}</a></td>              
		        </tr>
		        <tr>
		            <td>Link to red flag photos: 
		            	<apex:outputPanel >
		            	<apex:repeat value="{!r.Photo_Attachments__r}" var="p">
		            		<a href="{!orgUrl}{!p.Id}" target="_blank">{!p.Name}</a><br/>
		            	</apex:repeat>
		            	</apex:outputPanel>
		            </td>              
		        </tr>
        </apex:repeat>        
    </table>
    <hr/>
</apex:component>

Any help would be appreciated. Thanks in advance.


Vikram
I want to display data in a JQGrid on visualforce page. I am not getting any error but i can't also see any data. By looking into the debug logs i came to know that my java script is not at all calling the controller method. Below is my visualforce page code & Controller code

VF page:

<apex:page controller="GridController" showHeader="true" standardStylesheets="false" id="page1" >

<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.16/themes/redmond/jquery-ui.css" rel="stylesheet" type="text/css" />
<link href="http://www.trirand.net/aspnetmvc/Content/themes/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/JavaScript"></script>
<script src="http://www.trirand.net/aspnetmvc/Scripts/trirand/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="http://www.trirand.net/aspnetmvc/Scripts/trirand/jquery.jqGrid.min.js" type="text/javascript"></script>
    
<script type="text/javascript">
     var gridData;
     var obj;  
     
    function search(jsonString) {        
        $("#pdata").jqGrid("GridUnload");
                
        gridData = JSON.stringify(jsonString);    
        obj = JSON.parse(gridData);      

        jQuery("#pdata").jqGrid({
        data&colon; jsonString,
        datatype: 'local',
        colNames:['Id','Name'],
        colModel:[
            {name:'Id',index:'Id', width:40},
            {name:'Name',index:'Name', width:40}],
        rowNum:10,
        rowList:[5,10,20,30,50,100, 1000],
        pager: '#ppdata',
        sortname: 'name',
        viewrecords: true,
        sortorder: "desc",
        caption:"Contact Data",
        width: 800,
        height: 180,   
    });   
   $("#pdata").trigger("reloadGrid");
 }

function showDataInJqGrid(){
    var accName = document.getElementById("query").value;    
    contactSearch(accName);
}

function contactSearch(name) {
    var jsonString;
    Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.GridController.showContacts}',
            name,
            function (result, event) {
                if(event.type == 'exception'){
                    alert(event.message);
                } else{        
                    jsonString = result;        
                    search(jsonString);
                    gridData = JSON.stringify(jsonString);
                }
            },
            {escape: true}
        );

      // I first tried this below option also to call controller method but same results i got
     /*GridController.showContacts(name, function (result, event) {
                if(event.type == 'exception'){
                    alert(event.message);
                } else{        
                    jsonString = result;        
                    search(jsonString);
                    gridData = JSON.stringify(jsonString);
                }
          }); */
}

</script>

<apex:sectionHeader title="Contact Search using AccountID" subtitle="Search Contact" />
  <apex:pageBlock mode="maindetail">
    <apex:form id="qform" >
            <apex:pageBlockSection title="Search Contact for the Account" collapsible="false" columns="1" >
                <table width="100%">
                    <tr>
                        <td><h3>Enter Account Name </h3>&nbsp;&nbsp;<input type="text" id="query" />&nbsp;&nbsp;&nbsp;
                            <input type="button" value="Show Contacts " class="btn" onclick="showDataInJqGrid();" />&nbsp;&nbsp;&nbsp;
                        </td>
                    </tr>
                </table>
            </apex:pageBlocksection>
    </apex:form>

    <apex:pageBlockSection title="Contacts in Response" collapsible="false" rendered="true">               
        <div id="response" style="font-size: 16px;width: 300px;font-family: monospace; font-stretch: expanded" />               
        <table id="pdata"></table>
        <div id="ppdata"></div>
    </apex:pageBlocksection>
  </apex:pageblock>
</apex:page>

Controller:

global class GridController {

    public GridController() {

    }
   
    @RemoteAction
    global static List<Contact> showContacts(String accName){
        system.debug('inside method: '+accName);
        accName = '%'+ accName+'%';
        List<Contact> lst_contacts = new List<Contact>([select id, name from contact where Account.Name LIKE : accName]);
        return lst_contacts;
    }
}

Any help or quick suggestions?
I want to display data in a JQGrid on visualforce page. I am not getting any error but i can't also see any data. By looking into the debug logs i came to know that my java script is not at all calling the controller method. Below is my visualforce page code & Controller code

VF page:

<apex:page controller="GridController" showHeader="true" standardStylesheets="false" id="page1" >

<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.16/themes/redmond/jquery-ui.css" rel="stylesheet" type="text/css" />
<link href="http://www.trirand.net/aspnetmvc/Content/themes/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/JavaScript"></script>
<script src="http://www.trirand.net/aspnetmvc/Scripts/trirand/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="http://www.trirand.net/aspnetmvc/Scripts/trirand/jquery.jqGrid.min.js" type="text/javascript"></script>
    
<script type="text/javascript">
     var gridData;
     var obj;  
     
    function search(jsonString) {        
        $("#pdata").jqGrid("GridUnload");
                
        gridData = JSON.stringify(jsonString);    
        obj = JSON.parse(gridData);      

        jQuery("#pdata").jqGrid({
        data&colon; jsonString,
        datatype: 'local',
        colNames:['Id','Name'],
        colModel:[
            {name:'Id',index:'Id', width:40},
            {name:'Name',index:'Name', width:40}],
        rowNum:10,
        rowList:[5,10,20,30,50,100, 1000],
        pager: '#ppdata',
        sortname: 'name',
        viewrecords: true,
        sortorder: "desc",
        caption:"Contact Data",
        width: 800,
        height: 180,   
    });   
   $("#pdata").trigger("reloadGrid");
 }

function showDataInJqGrid(){
    var accName = document.getElementById("query").value;    
    contactSearch(accName);
}

function contactSearch(name) {
    var jsonString;
    Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.GridController.showContacts}',
            name,
            function (result, event) {
                if(event.type == 'exception'){
                    alert(event.message);
                } else{        
                    jsonString = result;        
                    search(jsonString);
                    gridData = JSON.stringify(jsonString);
                }
            },
            {escape: true}
        );

      // I first tried this below option also to call controller method but same results i got
     /*GridController.showContacts(name, function (result, event) {
                if(event.type == 'exception'){
                    alert(event.message);
                } else{        
                    jsonString = result;        
                    search(jsonString);
                    gridData = JSON.stringify(jsonString);
                }
          }); */
}

</script>

<apex:sectionHeader title="Contact Search using AccountID" subtitle="Search Contact" />
  <apex:pageBlock mode="maindetail">
    <apex:form id="qform" >
            <apex:pageBlockSection title="Search Contact for the Account" collapsible="false" columns="1" >
                <table width="100%">
                    <tr>
                        <td><h3>Enter Account Name </h3>&nbsp;&nbsp;<input type="text" id="query" />&nbsp;&nbsp;&nbsp;
                            <input type="button" value="Show Contacts " class="btn" onclick="showDataInJqGrid();" />&nbsp;&nbsp;&nbsp;
                        </td>
                    </tr>
                </table>
            </apex:pageBlocksection>
    </apex:form>

    <apex:pageBlockSection title="Contacts in Response" collapsible="false" rendered="true">               
        <div id="response" style="font-size: 16px;width: 300px;font-family: monospace; font-stretch: expanded" />               
        <table id="pdata"></table>
        <div id="ppdata"></div>
    </apex:pageBlocksection>
  </apex:pageblock>
</apex:page>

Controller:

global class GridController {

    public GridController() {

    }
   
    @RemoteAction
    global static List<Contact> showContacts(String accName){
        system.debug('inside method: '+accName);
        accName = '%'+ accName+'%';
        List<Contact> lst_contacts = new List<Contact>([select id, name from contact where Account.Name LIKE : accName]);
        return lst_contacts;
    }
}

Any help or quick suggestions?
Hi folks,
       Can anyone tell me the purpose of visualforce remoting and how use that?


Thanks in advance
Karthick

I want to display data in a JQGrid on visualforce page. I am not getting any error but i can't also see any data.

Below is my visualforce page code & Controller code

 

Visualforce Page:

 

<apex:page standardController="Contact" showHeader="true" standardStylesheets="false" extensions="GridController" id="page1" >

<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.16/themes/redmond/jquery-ui.css" rel="stylesheet" type="text/css" />
<link href="http://www.trirand.net/aspnetmvc/Content/themes/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/JavaScript"></script>
<script src="http://www.trirand.net/aspnetmvc/Scripts/trirand/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="http://www.trirand.net/aspnetmvc/Scripts/trirand/jquery.jqGrid.min.js" type="text/javascript"></script>
    
<script type="text/JavaScript">
     var gridData;
     var obj;  
     
    function search(jsonString) {        
        $("#pdata").jqGrid("GridUnload");
                
        gridData = JSON.stringify(jsonString);    
        obj = JSON.parse(gridData);      

        jQuery("#pdata").jqGrid({
        data&colon; jsonString,
        datatype: 'local',
        colNames:['Id','Name'],
        colModel:[
            {name:'Id',index:'Id', width:40},
            {name:'Name',index:'Name', width:40}],
        rowNum:10,
        rowList:[5,10,20,30,50,100, 1000],
        pager: '#ppdata',
        sortname: 'name',
        viewrecords: true,
        sortorder: "desc",
        caption:"Contact Data",
        width: 800,
        height: 180,   
    });   
   $("#pdata").trigger("reloadGrid");
 }

function showDataInJqGrid(){
    var accName = document.getElementById("query").value;    
    contactSearch(accName);
}

function contactSearch(name) {
    var jsonString;      
    myp1.GridController.showContacts(name,handleContacts);
}

function handleContacts(result, event) {
    if(event.type == 'exception'){
        alert(event.message);
    } else{        
        jsonString = result;        
        search(jsonString);
        gridData = JSON.stringify(jsonString);
    }
}
</script>

<apex:sectionHeader title="Contact Search using AccountID" subtitle="Search Contact" />
  <apex:pageBlock mode="maindetail">
    <apex:form id="qform" >
            <apex:pageBlockSection title="Search Contact for the Account" collapsible="false" columns="1" >
                <table width="100%">
                    <tr>
                        <td><h3>Enter Account Name </h3>&nbsp;&nbsp;<input type="text" id="query" />&nbsp;&nbsp;&nbsp;
                            <input type="button" value="Show Contacts " class="btn" onclick="showDataInJqGrid();" />&nbsp;&nbsp;&nbsp;
                        </td>
                    </tr>
                </table>
            </apex:pageBlocksection>
    </apex:form>

    <apex:pageBlockSection title="Contacts in Response" collapsible="false" rendered="true">               
        <div id="response" style="font-size: 16px;width: 300px;font-family: monospace; font-stretch: expanded" />               
        <table id="pdata"></table>
        <div id="ppdata"></div>
    </apex:pageBlocksection>
  </apex:pageblock>
</apex:page>

 

 

 

Controller:

 

 

global class GridController {
    public GridController(ApexPages.StandardController controller) {

    }
    
    @RemoteAction
    global static List<Contact> showContacts(String accName){
        accName = '%'+ accName+'%';
        List<Contact> lst_contacts = new List<Contact>([select id, name from contact where Account.Name LIKE : accName]);
        return lst_contacts;
    }
}

 

 

can someone help me?

Thanks,

Akash