Posts

Showing posts with the label Apex

REST apex code fetch the API usage

Image
  Apex code fetch the API usage
Image
  Understanding Salesforce Outbound Messages Outbound messaging allows you to specify that changes to fields within Salesforce can cause messages with field values to be sent to designated external servers. After you set up outbound messaging, when a triggering event occurs, a message is sent to the specified endpoint URL. The message contains the fields specified when you created the outbound message. Once the endpoint URL receives the message, it can take the information from the message and process it. Outbound messaging uses the  notifications()  call to send SOAP messages over HTTP(S) to a designated endpoint when triggered by a workflow rule.  Outbound messages need an endpoint URL which we need to mention while configuring outbound messages.  You can use a postman or any of the following to create your own endpoint. 1. Requestbin   2. Hookbin 3. Requestcatcher   4. Integration Playground for our example, we will use Integration Playground Defini...
Image
Related reference Fields Value in sObject SOQL In Dynamic SOQL we create query string using Apex code at run time and use Database.Query method to fetch the results. example:  String queryString = ' SELECT Id, Name, AccountId FROM Contact LIMIT 1'; sObject result = Database.query(queryString); above query will return me a contact information, if I wanted to access record's Name I can access it using sObject get() method i.e. result.get('Name') will give me Name value. Output: But the real challenge we will face when you try to access fields value from related object field, i.e. if you try to access Account.AnnaualRevenue you will get an exception 'System.SObjectException: Invalid field Account.AnnualRevenue for Contact'. To fix this we we will use getSObject() method to traverse the relationship. below code will traverse the relationship and get you the value: References: sObject Class , String Class , Martin Borthiry
Image
SOQL Injection A SOQL injection consists of insertion or injection of SOQL query via the input data from client to application. A successful SQQL injection exploit can read sensitive data from the database. Below is the example of Apex and Lightning Component code vulnerable to SOQL injection. Now enter name existing contact name and click on search button to see the result, in my case I search for contact whose Name contains 'Winter'. Now in search box enter " winter%' OR Name LIKE ' " and click on search button to all contact records. How to prevent SOQL injection? 1. Static Query: To prevent SOQL injection attach, avoid using dynamic SOQL queries. Instead, use static queries and binding variables. 2. Escaping Single Quotes: use of string.escapeSingleQuotes() method arounf variable function escapes any instance that it finds of a ‘ quote in the string using the backslash (\) escape character. This prevents an attacker’s input from...
Image
lightning:map component lightning:map introduced in winter 19 which can be use display a map of one or more locations using Google Maps. We can pass markers to the component to define the locations to map. A marker can be a coordinate pair of latitude and longitude, or a set of address elements: City, Country, PostalCode, State, and Street. This component requires API version 44.0 and later. lightning:map has required attribute mapMarkers which accepts array of markers that indicate location. A market contains Location information:  This can be a coordinate pair of latitude and longitude, or an address composed of address elements. Descriptive information:  This is information like title, description and an icon which is information relevant to the marker but not specifically related to location. Example Create a new Object called 'Tower' with two new custom fields 1. Field Label: State     Type: Master-Detail (Account) 2. Field Label: Locati...
Image
Inherited Sharing in Apex Inherited Sharing is available with Salesforce Winter'19 release. Use Inherited sharing keyword on apex class which allows the class to run in sharing mode of class that called it. Inherited sharing ensures that our apex code is not used in unexpected or insecure way. An Apex class with inherited sharing runs as with sharing when used as a Visualforce page controller, Apex REST service, or an entry point to an Apex transaction. If the class is used as the entry point to an Apex transaction, an omitted sharing declaration runs as without sharing. However, inherited sharing ensures that the default is to run as with sharing. A class declared as inherited sharing runs only as without sharing when explicitly called from an already established without sharing context. Example: We have declares Apex class with inherited sharing and a visualforce invocation of that Apex code. Because of the 'inherited sharing' declaration, only contacts for which...
Image
CAPTCHA in Lightning Use below code to implement captcha in your lightning component. Lightning_CAPTCH.cmp Lightning_CAPTCHController.js Lightning_CAPTCHHelper.js Add caption
Image
instanceof in Apex If we need to verify at runtime whether an object is actually an instance of a particular class use instanceof keyword.  The  instanceof  keyword can only be used to verify if the target type in the expression on the right of the keyword is a viable alternative for the declared type of the expression on the left. You could have something which is passed as Object type and using instanceof you can determine if it is an instance of your class or other data types below code will help understand the use instanceof keyword. Open developer console and run below code and observe the logs InstanceofController.getType(new Account()); INstanceofController.getType(true); INstanceofController.getType(Id.valueOf('001xa000003DIlo')); InstanceofController.getType(Date.newInstance(2020, 12, 31)); InstanceofController.getType(DateTime.newInstance(2020, 12, 31,0,0,0)); InstanceofController.getType('Clever Fox'); InstanceofController.getType(Blob.valueo...
Image
Virtual Class in Apex A class that extends another class inherits all the methods and properties of the extended class. In addition extending class can override the existing virtual methods by using override keyword in the method definition. Overriding a virtual method allows you to provide a different implementation for an existing method. This means that the behavior of a particular method is different based on the object you're calling it. This is referred as  polymorphism . just like abstract class, it is not mandatory to override virtual methods. virtual and abstract classes can extends virtual class. Check below code Now open developer console and run below code snippet and observe the logs Marker obj1, obj2; obj1 = new Marker(); // This outputs 'MARKER: writing some text' obj1.write(); obj2 = new BlueMarker(); // This outputs 'BLUEMARKER: writing some text using BlueMarker' obj2.write(); // We get the price of marker // and can call it fr...