// * Variables & Data Types in Apex /* ? What is a variable? * A variable is simply a named storage/memory location that contains a value. In apex, all variables have a "data type". ! Each variable is by default initialized to null ? What is a Data Type? * Data type is a keyword associated with a variable. It basically represents the type of data which that variable will store. * List of primitive data types: * 1. Boolean * 2. Integer * 3. Decimal * 4. Double * 5. Long * 6. Date * 7. Datetime * 8. Time * 9. Id * 10. String * 11. Object * 12. Blob * These are the OOTB or fundamental data types available in apex programming language. Let's talk about each one of them in detail: ? What is a Boolean data type? * A variable of Boolean data type can store only one of these two values: * 1. True * 2. False * Example of Boolean data type: */ /* one two three */ // comment // * In order to declare a variable, we follow the format: <Data Type><space><Variable name><;> Boolean isQualified; System.debug('Value of isQualified boolean variable is: ' + isQualified); isQualified = false; // * Equals (=) means Assignment System.debug('Value of isQualified boolean variable is: ' + isQualified); isQualified = true; System.debug('Value of isQualified boolean variable is: ' + isQualified); // isQualified = 123; /* ? What is an Integer data type? * An Integer data type can store a 32-bit number with no decimal point. It can store value between -2,147,483,648 to 2,147,483,647 * Example of Integer data type: */ Integer sum; System.debug('Value of sum Integer variable is: ' + sum); sum = 1; System.debug('Value of sum Integer variable is: ' + sum); sum = 10; System.debug('Value of sum Integer variable is: ' + sum); sum = 2147483647; System.debug('Value of sum Integer variable is: ' + sum); sum = -2147483647; System.debug('Value of sum Integer variable is: ' + sum); // sum = false; /* ? What is a Decimal data type? * A Decimal data type variable can store a number with a decimal point. * Example of Decimal data type: */ Decimal sum; System.debug('Value of sum Decimal variable is: ' + sum); sum = 1.0; System.debug('Value of sum Decimal variable is: ' + sum); sum = 1.01; System.debug('Value of sum Decimal variable is: ' + sum); sum = 1.234567890123456789012345678901234567890123456789; System.debug('Value of sum Decimal variable is: ' + sum); sum = -123456789012345678901234567890123456789012345678.9; System.debug('Value of sum Decimal variable is: ' + sum); //* Help Article about the field: https://help.salesforce.com/s/articleView?id=000387302&type=1, however we can have 49 digits in total for a variable of type decimal // * Assiging integer to a decimal sum = 123; System.debug('Value of sum Decimal variable is: ' + sum); // sum = true; /* ? What is a Double data type? * A Double data type variable can store a 64-bit number with a decimal point. It can store value between -2^63 to 2^63-1 * Example of Double data type: */ Double sum = 3.14; System.debug('Value of sum Double variable is: ' + sum); // * Use Double for less precision. For example: Scientific calculations where rounding off can be done. Use Decimal for more precision. For example: Financial calulations where there are no chances of errors. By default, currency fields in Salesforce are stored as decimals. /* ? What is a Long data type? * A Long data type variable can store a 64-bit number without a decimal point. It can store value between -2^63 to 2^63-1. * Example of Long data type: */ Long long1 = 1000000000; // * Valid long value without "L" Long long2 = 9223372036854775807L; // * Valid long value with "L" (for clarity) System.debug('Value of long1 Long variable is: ' + long1); System.debug('Value of long2 Long variable is: ' + long2); // * Long can be used when we need a higher range as compared to an integer /* ? What is a Date data type? * A Date data type variable can store a date value without any information about time. * Example of Date data type: */ Date date1 = Date.newInstance(1994, 12, 26); System.debug('Value of date1 Date variable is: ' + date1); System.debug('Subtracting 1 from date1 variable. Updated value = ' + (date1 - 1)); System.debug('Adding 1 to date1 variable. Updated value = ' + (date1 + 1)); Date dateFromString = Date.valueOf('1994-12-26'); System.debug('Value of dateFromString Date variable is: ' + dateFromString); System.debug('Date in string without timestamp is: ' + String.valueOf(dateFromString)); System.debug('Today\'s Date is: ' + System.today()); // * Adding two date values Date date1 = Date.newInstance(1994, 12, 26); Date date2 = Date.newInstance(1994, 12, 20); System.debug(date1 + date2); /* ? What is a Datetime data type? * A Datetime data type variable can store a datetime value like a timestamp. * Example of Datetime data type: */ Datetime date1 = Datetime.newInstance(1994, 12, 26); System.debug('Value of date1 Datetime variable is: ' + date1); Datetime dateFromString = Datetime.valueOf('1994-12-26 23:11:10'); // GMT + 5:30 System.debug('Value of dateFromString Datetime variable is: ' + dateFromString); System.debug('Date in string without timestamp is: ' + String.valueOf(dateFromString)); /* ? What is a Time data type? * A Time data type variable can store a time value which indicates a particular time. * Example of Time data type: */ Time time1 = Time.newInstance(14, 25, 60, 100); System.debug('Value of time1 Time variable is: ' + time1); System.debug('Value of hour: ' + time1.hour()); System.debug('Value of minute: ' + time1.minute()); System.debug('Value of second: ' + time1.second()); System.debug('Value of millisecond: ' + time1.millisecond()); /* ? What is a ID data type? * An ID data type variable can store any valid 18-character salesforce record id. * Example of ID data type: */ Id accountId = '001Hy00001D07fwIAB'; System.debug('Value of Id accountId = ' + accountId); Id accountIdFifteenDigits = '001Hy00001D07fw'; System.debug('Value of Id accountIdFifteenDigits = ' + accountIdFifteenDigits); accountId = '123456789123456789'; /* ? What is a String data type? * A String data type variable can store any set of characters surrounded by single quotes. It has no limit on the number of characters, heap size limit is used instead * Example of String data type: */ String name = 'SFDC Stop'; System.debug('Value of String name = ' + name); String nameWithTrailingWhitespace = 'SFDC Stop '; System.debug('Value of String nameWithTrailingWhitespace = ' + nameWithTrailingWhitespace); String emptyStringWithWhitespace = ' '; System.debug('Value of String emptyStringWithWhitespace = ' + emptyStringWithWhitespace); String escapeSequence = '\nSFDC\tStop \\ A blog about salesforce'; System.debug('Value of String escapeSequence = ' + escapeSequence); String a = 'apple', b = 'mango'; System.debug(a<b); String c = 'sfdc', d = 'Sfdc'; System.debug(c==d); /* ? What is a Object data type? * An Object data type variable can store value of any data type that is supported in apex. All data types inherit from object * Example of Object data type: */ Object num = 10; System.debug('Value of Object num = ' + num); Object name = 'SFDC Stop'; System.debug('Value of Object name = ' + name); Object num1 = 10, num2 = 20; System.debug('Sum of Object num1 and num2 = ' + (num1 + num2)); Object num1 = 10, num2 = 20; Integer num1Int = (Integer) num1, num2Int = (Integer) num2; System.debug('Sum of Integer num1Int and num2Int = ' + (num1Int + num2Int)); /* ? What is a Blob data type? * An Blob data type variable can store a collection of binary data as a single object. Blob is the short form for Binary Large Object. * Example of Blob data type: Contents of an image. Blob can be converted to a String and String can be converted to a Blob */ Blob b1 = Blob.valueOf('test'); System.debug('Value of Blob b1 = ' + b1); System.debug('Value of Blob b1 as string = ' + b1.toString());
Thursday, 25 April 2024
Sunday, 24 March 2024
Use Case
Automation
Platform Encryption
Building a Flow
Problem in the Flow
Let's save the flow once by clicking the Save button. As you try to save the flow, you'll get an error as shown below:Resolution: Invocable Apex
public class LeadQueryAction {
/**
* This method will query leads by phone numbers and return the list of lead ids
*/
@InvocableMethod(label='Get Leads by Phone Numbers' description='Return lead records filtered by phone numbers' category='Lead')
public static List<Id> queryLeadsByPhoneNumbers(List<String> phoneNumbers) {
Map<Id, Lead> leadsMap = new Map<Id, Lead>([SELECT Id FROM Lead WHERE Phone IN :phoneNumbers]);
System.debug(phoneNumbers);
System.debug(leadsMap.keySet());
return new List<Id>(leadsMap.keySet());
}
}As you can see above, we have a class named LeadQueryAction. This class has a method named queryLeadsByPhoneNumbers(List<String> phoneNumbers). This method will receive the list of phone numbers from flow, will query the leads using those phone nunbers and will return the list of lead ids. We're querying lead records using SOQL query: [SELECT Id FROM Lead WHERE Phone IN :phoneNumbers];. We're converting the queried lead records list directly into a Map<Id, Lead> named leadsMap so that we can get the lead ids easily using this map. Finally, we're returning the list of lead ids by converting lead ids set into a list using new List<Id>(leadsMap.keySet());Our apex class is ready now, let's get back to our flow and call this method.
Flow: Bulk Testing
List<Application__c> applications = new List<Application__c>(); applications.add(new Application__c(CandidateName__c = 'Richard Hendricks', CandidatePhone__c = '9999999999')); applications.add(new Application__c(CandidateName__c = 'Richard Hendricks', CandidatePhone__c = '9999999999')); insert applications;
List<String> phoneNumbers = new List<String>{'9999999999','9999999999'};
System.debug(LeadQueryAction.queryLeadsByPhoneNumbers(phoneNumbers));Issue Resolution for "FlowError: The number of results does not match the number of flow interviews"
public class LeadQueryAction {
/**
* This method will query leads by phone numbers and return the list of lead ids
*/
@InvocableMethod(label='Get Leads by Phone Numbers' description='Return lead records filtered by phone numbers' category='Lead')
public static List<Id> queryLeadsByPhoneNumbers(List<String> phoneNumbers) {
List<Id> leadIds = new List<Id>();
List<Lead> leads = [SELECT Id, Phone FROM Lead WHERE Phone IN :phoneNumbers];
Map<String, Id> leadPhoneNumberToIdMap = new Map<String, Id>();
for(Lead lead : leads) {
leadPhoneNumberToIdMap.put(lead.Phone, (Id) lead.Id);
}
for(String phoneNumber : phoneNumbers) {
if(leadPhoneNumberToIdMap.containsKey(phoneNumber)) {
leadIds.add(leadPhoneNumberToIdMap.get(phoneNumber));
} else {
leadIds.add(null);
}
}
return leadIds;
}
}List<Application__c> applications = new List<Application__c>(); applications.add(new Application__c(CandidateName__c = 'Richard Hendricks', CandidatePhone__c = '9999999999')); applications.add(new Application__c(CandidateName__c = 'Erlich Bachman', CandidatePhone__c = '9999999991')); applications.add(new Application__c(CandidateName__c = 'Richard Hendricks', CandidatePhone__c = '9999999999')); applications.add(new Application__c(CandidateName__c = 'Gavin Belson', CandidatePhone__c = '9999999992')); insert applications;
List<String> phoneNumbers = new List<String>{'9999999999', '9999999993', '9999999999', '9999999994'};
System.debug(LeadQueryAction.queryLeadsByPhoneNumbers(phoneNumbers));Sunday, 24 October 2021
Hello Trailblazers,
In this post we're going to see the solution of the error "System.DmlException: Insert failed. First exception on row 1; first error: INVALID_FIELD, Cannot specify both an external ID reference Parent and a salesforce id, ParentId: []" OR "System.DmlException: Upsert failed. First exception on row 1; first error: INVALID_FIELD, Cannot specify both an external ID reference Parent and a salesforce id, ParentId: []"
The most common scenario for this error is when we're trying to insert both the parent and child records at the same time and we're providing both - the Salesforce ID of the parent object and the parent object record reference with External ID, mentioned in the child record. Ideally we should only provide one out of Salesforce ID of parent object or parent object reference with External ID but not both. Let's have a look at an example.
Inserting Account and Contact record together
"Whenever we're performing a DML on child records, we cannot specify both the parent reference record, as well as the parent record id together, even if the parent record id is null"
Saturday, 2 October 2021
Hello Trailblazers, in this post we're going to talk about the resolution of a very common error as given below:
First error: Exceeded max size limit of 12000000 OR First error: Exceeded max size limit of 6000000
This error appears when you've exceeded the heap size limit in a single transaction. The heap size limit for a synchronous transaction is 6MB and for an asynchronous transaction is 12MB. Asynchronous transactions basically consists of batch classes, future methods and queueable apex.
How to resolve this error?
List<Account> accounts = [SELECT Id FROM Account]; // Stores 50,000 records at max - greater heap size
for(List<Account> accounts : [SELECT Id FROM Account]) {
}
// Stores 200 records at max - lesser heap size
void callMe()
{
List<Account> accounts = [SELECT Id FROM Account];
System.debug(Limits.getHeapSize()); // Coming as: 321085
}
callme();
System.debug(Limits.getHeapSize()); // Coming as: 1044Friday, 14 May 2021
Hello Trailblazers,
Recently, I faced this issue in one of the projects I was working on. The issue was UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record or N records, here N is just a placeholder in "N records" it can be 10, 20, 30 or even 100...200 for you. Let's have a look at the use case first and then we'll discuss the solution.
Use Case
Object B (Child) -> Object A (Parent)
Product Part (Child) -> Product (Parent)
Let's consider that we have a field named as: Product__c on the Product Part object which is a master detail relationship between product and product parts.






































