Map<Id, Account> accountsById = new Map<Id, Account>([SELECT Id, Name FROM Account LIMIT 10]);
Set<Id> accountIds = accountsById.keySet();
Map<Id, Integer> numberOfContacts = new Map<Id, Integer>();
for (Account acc : [SELECT Id, Name, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :accountIds]) {
numberOfContacts.put(acc.Id, acc.Contacts.size());
}
System.debug(numberOfContacts);
Sunday, December 3, 2017
Apex: Get all required fields of an sObject
Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe();
Schema.SObjectType s = m.get('Contact');
Schema.DescribeSObjectResult r = s.getDescribe();
Map<String,Schema.SObjectField> fields = r.fields.getMap();
for(String field : fields.keyset()) {
Schema.DescribeFieldResult describeResult = fields.get(field).getDescribe();
if (describeResult.isCreateable() && !describeResult.isNillable() && !describeResult.isDefaultedOnCreate()) {
System.debug(field);
}
}
Saturday, December 2, 2017
Sunday, November 19, 2017
Apex: Monitor / manage heap during execution
Use the code below in your Apex code to monitor the heap during execution.
System.debug(LoggingLevel.Debug, 'Heap Size: ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());
Reference: https://help.salesforce.com/articleView?id=000170956&type=1
Saturday, November 18, 2017
Javascript: Pattern to create instance-specific private data
To make a property private, just make it local to the constructor. Any outside code trying to access
age variable will result in an undefined. But the outside code can still call the property that accesses this private variable using the public function getAge.
function Person(name) {
// Define a variable only accessible inside of the Person constructor
var age = 25;
this.name = name;
this.getAge = function() {
return age;
};
this.growOlder = function() {
age++;
};
}
var person = new Person("Nicholas");
console.log(person.name); // "Nicholas"
console.log(person.getAge()); // 25
person.age = 100;
console.log(person.getAge()); // 25
person.growOlder();
console.log(person.getAge()); // 26
Subscribe to:
Comments (Atom)


