Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Wednesday, July 10, 2019

How To Check Whether User is In Console or Not in Lightning Experience

Basically if you want to check whether user in Lightning experience console(sales/service consoles in lightning experience not the just lightning experience ) you can use the same sforce console methods which you have used in classic.This method will support in lightning experience with out any changes.

Sample code:

<apex:page> 
<apex:includeScript value="/support/console/42.0/integration.js" /> 
<apex:includeScript value="/support/api/41.0/interaction.js"/> 

<button onclick="openTab();" > Console openPrimaryTab</button> 

<script> 
function inConsole() { 
if (sforce.console.isInConsole()) { 
alert('In console'); 
}else { 
alert('Not in console'); 
} 
}
</apex:page>

Note:
Even after using this if it's not working please check you version of /support/api/42.0/integration.js. If your version is below 42 then it will not work.So,please change the version it will work automatically.

Hope this helped you..Enjoy!..

Saturday, July 6, 2019

Base Lightning Components in Salesforce LWC

In Lightning web components(lwc) we will Base Lightning Components to display the particular record information/to create a record quickly with provided information with out using any custom apex.

List of the available Base Lightning Components are
  • lightning-record-edit-form
  • lightning-record-form
  • lightning-record-view-form

lightning-record-edit-form:

  • This component wrapper accepts the record id to display one or more fields and labels related to that field corresponding to given record id 
  • This will not require any apex because it uses Lightning Data Services to fetch the data
  • This will not support Standard objects like Task and Events.
  • Use lightning-output-field to display data in read only format
  • Use lightning-input-field to edit these fields in edit on record edit page

lightning-record-form:

  • Use this component to build the forms quickly to create a record,view the record or to update the record. 
  • Building the record creation forms is easier than building the form using lightning-record-edit-form and lightning-record-view-form. 
  • This component accepts 4 properties like object name which is mandatory,record id which is optional in case of create record ,mode to specify view/edit/read only and layout type like Full or compact
  • It will also takes care of FLS and sharing rules
 <lightning-record-form  
   record-id="739hchjsh773"  
   object-api-name="Position__c"  
   layout-type="Full"  
   mode="view">  
 </lightning-record-form>  

lightning-record-view-form:

  • Use this component when you want to dispaly the record on view mode
  • This will not require any apex because it uses Lightning Data Services to fetch the data
  • This will not support Standard objects like Task and Events.
  • It will also takes care of FLS and sharing rules.

For more information refer:

Hope this helps you...Enjoy!

Wednesday, August 23, 2017

CSS Styles/Scripts Are Not Working in Salesforce Force.com Sites

If your styles/scripts are working perfectly in Sandbox and Production preview but if it's not working on Force.com sites which has hosted publicly on Sales force server below points might be the issue.

Your page is referring to some external CSS/JS files:

<apex:page>
 <head>
 <apex:stylesheet value="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css" />
<apex:includescript value="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js" /> 
</head>
<apex:form>
  ----
  ----
</apex:form>
</apex:page>

If your referring any styles/scripts from external sources with out storing them in Static Resource you will end up with problem Please store that files in Statistic resource and then use it then it will work in Force.com sites as well.


<apex:page>
 <head>
  <apex:includeScript value="{!$Resource.dataTableJS}"/>
  <apex:stylesheet value="{!$Resource.dataTableCSS}" />
</head>
<apex:form>
  ----
  ----
</apex:form>
</apex:page>


Your Static Resource is File not accessible publicly



If your using any static resource in your page and if that static resource is not accessible publicly (Cache Control set to Private) then styles will not reflect in Force.com sites.So mark your Static resources cache control as Public



Hope this helps you...Enjoy!


How to Invoke the REST API from the Visual Force Page in Salesforce

Problem:
We usually making a lot of API calls from Salesforce to External systems to get the some data or to pass the some updates to External Systems.All these calls will be made from the Apex class to External systems. What if you have situation like where you need to make a call to external systems from the visual force page.

Solution:
No need to worry still we have option to make an REST API call from the page directly.How?As we already know that Salesforce has introduced a concept called AJAX ToolKit.This is an frame which will be used to make all the operations from JavaScript similar to APEX. With help of this tool kit you can make an REST API call from the Script in Visual Force Page

Flow Diagram:
 

Sample Source Code:

<apex:page standardController="Account">
<apex:includeScript value="//code.jquery.com/jquery-1.11.1.min.js" />
<script src="../../soap/ajax/40.0/connection.js" type="text/javascript"></script>

<script>
function makeAPICall() 
{
    var weblink = //Your API endpoint 'https://www.yourednpoint/';
 
    $j.ajax({
  url: weblink,
  type: 'GET', // Type POST or GET
  dataType: 'json',
  beforeSend: function(request) {
      // Add All your Headers Here if Any
   request.setRequestHeader('HeaderString','HeaderValue');
   //Sample Headers 
   request.setRequestHeader('Country', 'IND');
   request.setRequestHeader('Currency', 'INR');
  },

  crossDomain: true,
  
  //If Successfully executed 
  success: function(result) {
   
   //Response will be stored in result variable in the form of Object.Please use result variable for processing
    console.log('Response Result'+result); 
    
      //If you want to convert JSResponse Object to JSON response 
    var jsonResp = JSON.stringify(result));

  },
  
  //If any Error
  
  error: function(jqXHR, textStatus, errorThrown) {
   
   // alert('ErrorThrown: ' + errorThrown);
  }
    });
}
</script>
<apex:form>
  call javaScript method from here to make an api call invocation
</apex:form>
</apex:page>

Hope this helps you....Enjoy!
 

Saturday, July 1, 2017

How to Check Whether All Check Boxes Selected Or Not in Visual Force Page Using Jquery

In my previous post we have discussed about how to check whether at least one check box is checked or not in visual force it's very easy using Jquery .In this post we are going to know about how to check whether all check boxes has selected or not? using Jquery .Please use the below code snippet to check this functionality.

Sample Source Code:


function selectionCheck(selClass)
   {
          
     var chbxClass = '.'+selClass; // selClass is styleclass name for all your checkbox
  
 if ($(chbxClass+':checked').length == $(chbxClass).length) 
        {
        
          alert('You have selected all records for Action'));           
            
        }
  
 else
 {
  alert('You haven't selected all records'); 
 }
     
  }



Thanks for visiting..hope this helps you!

Monday, April 3, 2017

How To Allow Only Numbers in Phone Number Field in Visual Force Page

If your trying to restrict your phone number field to accept only Numbers in visual force page you can use the below simple JavaScript code to achieve this easily.

Java Script Code:
<script>

function isNumber(evt) {
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            return false;
        }
}

</script>

<apex:inputfield value="{!c.Phone__c}" id="boPhone" onkeypress="return isNumber(event)"/>



Thanks for visiting..hope this helps you!

Tuesday, March 28, 2017

How To Check Whether at Least One Check Box Checked or Not? Using JQuery

If you want to know whether at least one check box is checked or not in visual force it's very easy using Jquery .Please use the below code snippet to check this functionality.

Sample Source Code:


function selectionCheck(selClass)
   {
          
        var chbxClass = '.'+selClass; // selClass is styleclass name for all your checkbox
          
        if($(chbxClass).is(':checked'))
  {
   alert('You have selected one or more records');
  }
                
        else
        {
            alert('Please select at least one record');
            return false;
        }
  }


In the above code if you see we are one jQuery method called is() ,this method will loop through the all the list and returns true if at least one record has checked among the list otherwise it will return false.


Thanks for visiting..hope this helps you!



Wednesday, January 4, 2017

How to pass parameters from JavaScript to Controller in Salesforce

If you want to send more than one parameter from javaScript to Apex controller in your class,no worries it can be done easily .Please follow the below snippet to achieve the same.

Sample VisualForce Page

<apex:page>
  <apex:form>
    <apex:pageblock>
   <select id="dropDownId">
   <apex:commandButton value="Update" onclick="validate();return false;" />
 </apex:pageblock>
 
  <apex:actionFunction name="updateRecords" action="{!performUpdate}" 
                              reRender="resultPanel,errMsg" status="statusSaveTrip">
        <apex:param id="status" name="selStatus" value="" />
        <apex:param id="renerTabId" name="renderTabId" value="" />
        <apex:param id="phone" name="phone" value="" /> 
    </apex:actionFunction>
  </apex:form>
</apex:page>

<script>
  funtion validate(dropDownId )
  {
    //Some processing here
  updateRecords(param1 value,param2 value,param3 value);
  }
</script>

Sample Apex Controller

public class SampleController
{
   public SampleController()
   {
   
   }
   
   public pagereference performUpdate()
    {
     System.debug('..entered...performUpdate..');     
     String selDropdownVal=Apexpages.currentPage().getParameters().get('selStatus');
     String tableIdtoRender = Apexpages.currentPage().getParameters().get('renderTabId');
     String phone = Apexpages.currentPage().getParameters().get('phone');
     System.debug('..selDropdownVal...'+selDropdownVal+'..'+tableIdtoRender+'..'+phone);
   }
}


Thanks for visiting..hope this helps you!

How to add pick list field values dynamically in visual force

If we want to add picklist values at run time in visual force it can be done very easily by using <apex:selectOptions> tag .Their might be a situation where you need to go with html 'select ' tag to form the picklist. In this situations if you want add values to it run time their is no in built attributes as we follows in visual force tags.To achieve this we need to use some scripting .In this example I'm going to show with the help of jQuery.

Sample code snippet:


<apex:page>
 <apex:includeScript value="//code.jquery.com/jquery-1.11.1.min.js"/>
  <apex:form id="formId">
    <apex:pageblock>
   <select id="dropDownId">
   <apex:commandButton value="Update" onclick="addPicklistValues('dropDownId');return false;" rerender="formId"/>
 </apex:pageblock>
  </apex:form>

<script>
  function addPicklistValues(dropDownId )
  {
  var dropId = '#'+dropDownId;
  $(dropId).find('option').remove().end().append('<option value="-None-">--None--</option>','<option value="Approved">Approved</option>','<option value="Rejected">Rejected</option>');
              
  }
</script>
</apex:page>

Thanks for visiting..hope this helps you!

Tuesday, January 3, 2017

Jquery to check that checkbox is selected or not in visual force

If you wanna check whether a checkbox is selected or not in visual force please use the below jQuery snippet.

jQuery code snippet:


 function updateSelRecords(checkboxid or checboxstleclas)
    {
        var chbxClassId = '.'+checboxstleclas; // if your receiving checkbox styleclas
                     or 
        var chbxClassId = '#'+checkboxid; //if your receiving checboxId
  
        if($(chbxClassId).prop('checked'))
         {
      
           alert('Checkbox selected');
         }
         
        else
        {
         alert('Please select at least one record');
           return null;
        }
    }



Thanks for visiting...!

Monday, January 2, 2017

How to select all check boxes in visual force page using Jquery class attribute

If you want to select all records in a table we can do it very simply by using tag name but there are some situations where we have 4 tables and clicking on All check box in one table should select only the records related to that table not the all table records.

To achieve that we need to have a unique identifier for each table record but unfortunately in sales force Id of any vf tag can't be a dynamic so we can't use ID here .There is one more attribute called 'styleclass' in visual force tags which can be a dynamic.Now we are using this class attribute to differentiate the table records and we will use jQuery to achieve this functionality.

Sample code snippet:

<apex:repeat value="{!listSer}" var="sr">
  <apex:pageBlockTable value="{!listWrapCase}" var="caseWrap" id="pgbId">
                          
   <apex:column >
     <apex:facet name="header" >
       <input type="checkbox" onclick="selectAllCheckboxes(this,'{!sr}chbxId')"/>
    </apex:facet>
  
   <apex:outputPanel >
     <apex:inputCheckbox value="{!caseWrap.isSelect}" styleClass="{!sr}chbxId"/>
   </apex:outputPanel>
  </apex:column>  
  
  <apex:column >
    <apex:facet name="header" >CaseId</apex:facet> 
    <apex:outputLabel value="{!caseWrap.cs.CaseNumber}" />
 </apex:column>
   
 </apex:pageBlockTable>
</apex:repeat>

Jquery snippet:


<script type="text/javascript">  
     
      function selectAllCheckboxes(obj,receivedInputclass){
      
        $('.'+receivedInputclass).trigger('click');
      }
</script>


Thank you!