Hot answers tagged

31

QueryMore is the appropriate means of accomplishing this if you're getting data all at once. For pagination, such as in a website or portal, sort by some value then use filters. I would recommend a field with a high cardinality (e.g. many unique values) because it makes pagination that much easier. I've posted this answer elsewhere, but I'll post it here, ...


27

Take a look at the UserLicense object documentation. There is a field called LicenseDefinitionKey that uniquely identifies a particular user license and the documentation lists all possible values. From the documentation: A string that uniquely identifies a particular user license. Label is License Def. ID. Values are: And one of the values: SFDC: ...


26

You can not use text area fields in SOQL and SOSL filter criterias. Try this approach: List<Project__c> projects = new List<Project__c>(); for(Project__c proj : [SELECT Name, Status__c, Description__c FROM Project__c WHERE Name LIKE '%' + String.escapeSingleQuotes(name) +'%' + OR Status__c = String.escapeSingleQuotes(status)]) { if(proj....


20

It's case-insensitive by default. From the Force.com SOQL and SOSL Reference on WHERE conditionExpression: comparisonOperator Case-insensitive operators that compare values. Operator = Name Equals Description Expression is true if the value in the specified fieldName equals the specified value in the expression. String comparisons using the equals ...


18

DateTime dt = DateTime.now(); String formattedDt = dt.format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\''); Update: Another option is to use SOQL dynamic binding, no formatting required. DateTime dt = DateTime.now(); String query = 'SELECT Id FROM User WHERE SystemModstamp > :dt ' List<SObject> results = Database.query(query);


18

Use LIKE with a wildcard (%). WHERE Name LIKE 'Womp%' This particular wildcard means "zero or more characters"


16

Didn't find how to do it in a single query, but this gives all users with a Salesforce user licence type. Important to know is that you have to query this through the profile. Map<Id,Profile> profileIds = new Map<id,profile>([SELECT Id,UserLicenseId FROM Profile where UserLicenseId in (SELECT Id FROM UserLicense where name ='Salesforce')]); ...


14

You can access them just like you would a custom object, using SOQL. By doing that though, you aren't fully leveraging one the big advantages of using a custom setting over a custom object, no impact on governor limits. If you use a custom setting and access it using some of the methods provided, you are retrieving your data with zero impact on the ...


14

Update Please note that for the running user, you can use the FeatureManagement class: Boolean isEnabled = FeatureManagement.checkPermission('My_Permission_Api_Name'); Surprisingly, I don't think you can do it more simply than the below. Execution public static List<User> getUsersWithCustomPermission(String name) { Set<Id> ...


14

You can write like this: In your case "Test3" is parent,"test1" and "test2" are childs. Try below query, [select id,name,(select id,name from test1s__r),(select id,name from test2s__r) from test3__c] For Example SELECT id,(SELECT Id,Status,CaseNumber FROM Cases),(SELECT Id,Name FROM Contacts) FROM Account


13

Isolation of Test Data from Organization Data in Unit Tests Starting with Apex code saved using Salesforce API version 24.0 and later, test methods don’t have access by default to pre-existing data in the organization, such as standard objects, custom objects, and custom settings data, and can only access data that they create. However, objects that are used ...


13

In the immediate aftermath (and a short time thereafter), you can find deleted records with a MasterRecordId in the Recycle Bin. SELECT MasterRecordId FROM Account WHERE MasterRecordId != null ALL ROWS You might be able to use a left inner join but I am unable to confirm currently. SELECT Id FROM Account WHERE Id IN (/*above query*/) Here's the ...


13

Yes. You are looking for a Left Outer Join, where the type of query you first reference is a Right Outer Join. Your query might look like: SELECT (SELECT Id FROM Awards__r) FROM Person__c You can control the value of this Child Relationship Name in the configuration on the lookup field. Custom lookups append __r to the relationship name. When configuring ...


13

You have to use dynamic SOQL if you want to add fields dynamically. Merge syntax is not supported. List<String> fields = new List<String> { 'Id', 'Name' }; String soql = 'SELECT ' + String.join(fields, ',') + ' FROM MyObject__c WHERE ...'; List<MyObject__c> records = Database.query(soql); I actually often have dynamic filters as well when ...


12

I tried a simplified version of the query using the Account to Contact parent-to-child relationship. With this I was able to access the Contacts for each Account. List<Account> queryResults = [Select Id, (Select Id from Contacts) from Account limit 10]; for (Account a : queryResults) { for (Contact c : a.Contacts) { System.debug(c); } }...


12

Schema.DescribeFieldResult F = <Object_API_Name>.<Field_API_Name>.getDescribe(); Schema.sObjectField T = F.getSObjectField(); List<PicklistEntry> entries = T.getDescribe().getPicklistValues();


12

Yes, it's always legal to assign a List<SObject> that came from a query into a SObject variable, and it will succeed during runtime only if there's exactly one element. Note that child objects are always considered to have come from a query, so the following code compiles (but obviously crashes): Account a = new Account(); Contact c = a.Contacts; ...


11

Did you check "Deleted and archived records:" as "Include" ? And as for the Developer console, you have no way to do it. You can do with Apex using SOQL with the "ALL ROWS" option, but the Query Editor on the Developer console doesn't support it.


11

Conjecture This seems like an issue with the garbage collector internals, as if exiting the block scope created by if/do/for, etc does not cause the garbage collector to run and clear out heap variables that are no longer visible. You'll have to open a case to find anything more definite. Evidence (?) I ran the following and consider it of interest: ...


11

The convention I adopt when selecting many fields is to: Put the SELECT clause on its own line Indent each line in the actual fields by 1 tab/4 spaces Generally put as many fields as possible on one line without overrunning 80/100/120 according to line length preference Put each sub-query on its own line I also find it's best to put each clause on its own ...


11

Explanation The core problem here is that implicit type conversion from List<String> to String will cause truncation. It looks like this happens after the tenth element. You can see it for yourself by running the following script in Execute Anonymous: List<Integer> numbers = new List<Integer>(); for (Integer i = 0; i < 25; i++) ...


11

The first query I got to work was on the ObjectPermissions object: SELECT Parent.Profile.Name FROM ObjectPermissions WHERE Parent.IsOwnedByProfile = true AND SObjectType = 'MyObject__c' This query gets exactly the data I wish, but in the Query Editor, gives the dreaded: [object Object] Left Inner Join to the rescue! More palatable: SELECT Profile.Name ...


11

They're the same. system.assertEquals(Id.valueOf('00ea0000001p22SAAQ'), Id.valueOf('00ea0000001p22S')); Salesforce Ids have a 15 digit Id and an 18 digit Id. The longer version is "case-safe" in order to support integrations with a wider array of external systems. For more detail around the conversion from 15 digits to 18 digits, see What are Salesforce ID&...


10

It is incorrect to assume that SOSL is better for large data sets. Both SOQL and SOSL can be used with large data sets when implemented correctly. I wrote an article (originally posted as a Q&A here) about the differences between SOQL and SOSL you may find useful. SOSL actually limits you to 200 records returned in a single SOSL query while SOQL will ...


10

When you do a SOQL always the relationships are denoted by __r for custom objects while for standard objects its object Name usually. If you are moving from Child to Parent or from parent to child in soql then field relationship Name comes into picture. Now lets say we have query on contact as below, [Select Id,AccountId,Account.Name from Contact] ...


10

You need escaped single quotes around the picklist value instead of double quotes: '(SELECT Name, Risk__Status__c FROM Risk__Actions__r WHERE Risk__Status__c=\'Open\' order by Name), ' +


10

EncodingUtil.urlEncode() will do what you are looking for: String stringToEncode = 'test+test@test.com'; String encodedUrl = EncodingUtil.urlEncode(stringToEncode, 'UTF-8'); System.debug(encodedUrl); The code above outputs: test%2Btest%40test.com You can use EncodingUtil.urlDecode() to get it back to its original form.


10

Yes, of course. The first step is to gather a collection of the Account ids. Set<Id> accountIds = new Set<Id>(); for (Order order : trigger.new) accountIds.add(order.AccountId); The next step is to query the Account records and put them in a Map: Map<Id, Account> accounts = new Map<Id, Account>([ SELECT Fields FROM Account ...


10

In the case of assignment to a single record and filtering on an Id, there is zero impact on your governor usage. It won't affect CPU Time, and if you were already enforcing exactly one result by assigning to a single record, it won't affect heap size or query rows either. Obviously it won't affect number of queries. I can't think of any other relevant ...


Only top voted, non community-wiki answers of a minimum length are eligible