Friday, November 10, 2017

How to Convert String to sObject using Apex in Salesforce

Below is the sample code can be used to Convert String to sObject using Apex.

String str = 'Account';
sObject objct = Schema.getGlobalDescribe().get(str).newSObject();
objct.put('Name', 'My Account');
objct.put('Description', 'Description of My Account');
System.debug('sObject - ' + objct);
Below is the output of this code.

SOQL Query to Get the CreatedBy and LastModifiedBy User Name


Below SOQL query can be used to get the CreatedBy and LastModifiedBy User Name


SELECT Id, Name, CreatedBy.FirstName, CreatedBy.LastName, CreatedBy.Name, LastModifiedBy.FirstName, LastModifiedBy.LastName, LastModifiedBy.Name FROM Object__c

Note: Object__c, you can replace for the object you want to get these data.

Tuesday, October 31, 2017

AuraHandledException

From Apex class:

 @AuraEnabled
    public static List<Account> fetchAccount() {
        throw new AuraHandledException('User-defined error');

    }

From client-side controller:

doInit: function(component, event, helper) {
        var action = component.get('c.fetchAccount');
        action.setParams({ firstName : cmp.get("v.firstName") });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set('v.Accounts', response.getReturnValue());
            }
            else if (component.isValid() && state === "ERROR") {
                console.log("Error Message: ", response.getError()[0].message);
            }
        });
        $A.enqueueAction(action);
    }


How to Select All Fields with SOQL in Apex

1. Query All Fields for a Known Record ID:

ID recordId = '5001a00000CgCE2';

DescribeSObjectResult describeResult = recordId.getSObjectType().getDescribe();

List<String> fieldNames = new List<String>( describeResult.fields.getMap().keySet() );

String query =
  ' SELECT ' +
      String.join( fieldNames, ',' ) +
  ' FROM ' +
      describeResult.getName() +
  ' WHERE ' +
      ' id = :recordId ' +
  ' LIMIT 1 '
;

// return generic list of sobjects or typecast to expected type
List<SObject> records = Database.query( query );

System.debug( records );

2. Query All Fields for All Records

The same concept as above but we don’t have a specific record ID, so we need to determine the sobjectype by explicitly specifying the object we’ll query on.

// without an ID, simply specify the object to then derive the sobject type
DescribeSObjectResult describeResult = Account.getSObjectType().getDescribe();

List<String> fieldNames = new List<String>( describeResult.fields.getMap().keySet() );

String query =
  ' SELECT ' +
      String.join( fieldNames, ',' ) +
  ' FROM ' +
      describeResult.getName()
;

// return generic list of sobjects or typecast to expected type
List<SObject> records = Database.query( query );

System.debug( records );

Friday, October 13, 2017

JavaScript in Strict Mode

JavaScript code that runs in a browser can access a user's private information. To protect this information, LockerService requires you to use ECMAScript (ES5) strict mode, and enforces it in your code. This mode makes it easier to write secure JavaScript. A few code tweaks to comply with ES5 can substantially reduce the need for checking data at runtime.

Here’s a simple example of how strict mode prevents JavaScript from traversing the call-stack and modifying other functions. Without strict mode, the following function would have returned the name of the function that called primaryFunction().

JavaScript code:
function primaryFunction() {
  'use strict'
  console.log("The function calling the current method is: “+ primaryFunction.caller.name);
}
function callerFunction() {
  return primaryFunction(); 
}
callerFunction();

Output:
Uncaught TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.
at primaryFunction (<anonymous>:3:72) at callerFunction (<anonymous>:6:10) at <anonymous>:8:1

Strict mode restricts access to several other entities. When it is enforced, you can no longer reference the window object through the JavaScript variable this, for example. These restrictions eliminate a lot of potential security vulnerabilities.