Category: SOSL

Handy SOQL/SOSL Queries for Knowledge

 Happy Monday!! In my last post, I have explained the Data Model around Salesforce Lightning Knowledge. If you haven’t gone through that post, I highly recommend you going through the post “Understanding Salesforce Lightning Knowledge Data Model” as it is going to help you to understand the queries in this post.Let’s first create the article – “Hello World” and below are the different versions with different publishing status. Below article properties which I will be using the query -Article Number: 000001000Knowledge Article Id: kA03t00000063w2CAAFetching Latest Knowledge Article VersionSELECT Id,PublishStatus,Title FROM Knowledge__kav WHERE KnowledgeArticleId = ‘kA03t00000063w2CAA’ AND IsLatestVersion = TrueFetching Published Knowledge Article VersionSELECT Id,PublishStatus,Title FROM Knowledge__kav WHERE KnowledgeArticleId = ‘kA03t00000063w2CAA’ AND PublishStatus = ‘Online’Fetching Draft Knowledge Article VersionSELECT Id,PublishStatus,Title FROM Knowledge__kav WHERE KnowledgeArticleId = ‘kA03t00000063w2CAA’ AND PublishStatus = ‘Draft’Fetching Archived Knowledge Article VersionsSELECT Id,PublishStatus,Title FROM Knowledge__kav WHERE KnowledgeArticleId = ‘kA03t00000063w2CAA’ AND PublishStatus = ‘archived’ AND IsLatestVersion = FalseThis one is tricky, as to query Archived article versions, KnowledgeArticleId and IsLatestVersion = False and PublishStatus = archived should be mentioned.Note -Using bind variables with Apex SOQL Statements with KnowledgeArticleVersion is not allowed. So you need to use dynamic SOQL like below -final String ONLINE_ARTICLE = ‘Online’;final String myQuery = ‘SELECT Id FROM Knowledge__kav WHERE PublishStatus = :ONLINE_ARTICLE’;List<Knowledge__kav> allArtciles = Database.query(myQuery);Working with DATA CATEGORYWITH DATA CATEGORY is an optional clause in SOQL and it helps to identify articles linked with one or more...

Read More

Dynamic Apex – Build Dynamic SOQL and SOSL

In this post, I will explain another very powerful feature of Dynamic Apex i.e. Dynamic SOQL and SOSL.   Dynamic SOQL: Dynamic SOQL refers to the ability of creating SOQL statement in runtime with Apex code i.e. you can select which fields you want to query based on situation or user input and then prepare the SOQL statement and finally perform the query to fetch the data from the organisation. You can execute the below statement to perform the query: Dynamic SOQL can return concrete sObjects i.e. Account, Lead or MyCustomObject__c or the generic sObject data type. At runtime, system will match the return type with the variable’s declared type. If the types are not matching, then runtime will be thrown. So it is the responsibility of the developer to cast from generic sObject data type to a concrete sObject. In terms of governor limits, Dynamic SOQL queries have the same governor limits as static queries. Below example is going to fetch the fields from Account sObject, prepare the SOQL statement and then finally query Account sObject to fetch the data. Dynamic SOQL Consideration: In Dynamic SOQL, you cannot use bind variable strings in a query string. For example, the below section of code will not work and will throw an error that Variable doesn’t exist. To solve that you need to resolve the variable into a String variable and...

Read More

Salesforce Summer 15 New Feature || Now we can use Location and Distance Variables in SOQL and SOSL Queries

With Salesforce Summer’15 release, now we can use location and distance variable in SOQL and SOSL queries.To know more about location and distance variable, I will request to check Salesforce’s documentation @Location-Based SOQL QueriesNow let me explain this new feature with a small code snippet.In my account object, I have created one custom field of type Geolocation with name Head Office Location (API Name: Head_Office_Location__c).Below is the code snippet -As you can see in the above code what I am trying to find all the accounts whose head office is located within 1 miles from my current location. To achieve this, I have used my current location’s latitude and longitude as apex bind variable in SOQL query.The way DISTANCE and GEOLOCATION function work are given below -DISTANCE(mylocation1, mylocation2, ‘unit’)GEOLOCATION(latitude, longitude)Now after Summer’15, you can replace any of the parameters used in the above two functions by apex bind variable.So to me, it is really a good feature. What do you guys think? Please provide your feedback....

Read More
Loading