Custom Fiscal Week (Monday to Sunday) in Wave Analytics (Salesforce Einstein Analytics)

q = load “GR_Opportunities”;
q = foreach q generate ‘CloseDate_day_epoch’ as ‘CloseDate_day_epoch’, ‘CloseDate_sec_epoch’ as ‘CloseDate_sec_epoch’, ‘Id’ as ‘Id’, ‘StageName’ as ‘StageName’,  daysBetween(toDate(“24-06-1985”, “dd-MM-yyyy”), toDate(CloseDate_sec_epoch)) % 7 as ‘datediff’;
q= foreach q generate (case
when datediff == 6 then (case when daysBetween(now(), toDate(CloseDate_sec_epoch))>=-6 and daysBetween(now(), toDate(CloseDate_sec_epoch))<=0 then “CurrentWeek” else “OtherWeek” end)
when datediff == 5 then (case when daysBetween(now(), toDate(CloseDate_sec_epoch))>=-5 and daysBetween(now(), toDate(CloseDate_sec_epoch))<=1 then “CurrentWeek” else “OtherWeek” end)
when datediff == 4 then (case when daysBetween(now(), toDate(CloseDate_sec_epoch))>=-4 and daysBetween(now(), toDate(CloseDate_sec_epoch))<=2 then “CurrentWeek” else “OtherWeek” end)
when datediff == 3 then (case when daysBetween(now(), toDate(CloseDate_sec_epoch))>=-3 and daysBetween(now(), toDate(CloseDate_sec_epoch))<=3 then “CurrentWeek” else “OtherWeek” end)
when datediff == 2 then (case when daysBetween(now(), toDate(CloseDate_sec_epoch))>=-2 and daysBetween(now(), toDate(CloseDate_sec_epoch))<=4 then “CurrentWeek” else “OtherWeek” end)
when datediff == 1 then (case when daysBetween(now(), toDate(CloseDate_sec_epoch))>=-1 and daysBetween(now(), toDate(CloseDate_sec_epoch))<=5 then “CurrentWeek” else “OtherWeek” end)
when datediff == 0 then (case when daysBetween(now(), toDate(CloseDate_sec_epoch))>=0 and daysBetween(now(), toDate(CloseDate_sec_epoch))<=6 then “CurrentWeek” else “OtherWeek” end)
 
else “Blank Close Date”
end) as currentweek, Id as ‘Id’;
q = filter q by currentweek == “CurrentWeek”;
q = limit q 100;
Advertisements

Default sort order in list in apex

Default Sort Order of sObjects

The List.sort method sorts sObjects in ascending order and compares sObjects using an ordered sequence of steps that specify the labels or fields used. The comparison starts with the first step in the sequence and ends when two sObjects are sorted using specified labels or fields. The following is the comparison sequence used:

  1. The label of the sObject type.

    For example, an Account sObject will appear before a Contact.

  2. The Name field, if applicable.

    For example, if the list contains two accounts named A and B respectively, account A comes before account B.

  3. Standard fields, starting with the fields that come first in alphabetical order, except for the Id and Name fields.

    For example, if two accounts have the same name, the first standard field used for sorting is AccountNumber.

  4. Custom fields, starting with the fields that come first in alphabetical order.

    For example, suppose two accounts have the same name and identical standard fields, and there are two custom fields, FieldA and FieldB, the value of FieldA is used first for sorting.

Not all steps in this sequence are necessarily carried out. For example, if a list contains two sObjects of the same type and with unique Name values, they’re sorted based on the Name field and sorting stops at step 2. Otherwise, if the names are identical or the sObject doesn’t have a Name field, sorting proceeds to step 3 to sort by standard fields.

For text fields, the sort algorithm uses the Unicode sort order. Also, empty fields precede non-empty fields in the sort order.

This is an example of sorting a list of Account sObjects. This example shows how the Name field is used to place the Acme account ahead of the two sForce accounts in the list. Since there are two accounts named sForce, the Industry field is used to sort these remaining accounts because the Industry field comes before the Site field in alphabetical order.

How to insert CaseMilestone in Salesforce

Sometimes we get such a scenario, we may need to create CaseMilestones- especially when you are doing data migration from Salesforce-to-Salesforce; but Salesforce does not allow to create CaseMilestone through custom means.

But we can follow the below steps to insert it in new org same as we have in current org (assuming that you have setup Entitlement and Milestone in new org same as existing org)

  1. Create case with Entitlement with these key fields:
    • SlaStartDate: Copy it from Current Case
    • EntitlementId = Can be given.
    • IsStopped = can be set to pause the milestone- especially for active milestones. Later we can resume this by setting value to false.
  2. If we provide Entitlement Field while creating the case as mentioned above, system automatically creates related Milestone with StartDate as SlaStartDate of Case record.
    • Now you can update the completion date of CaseMilestone record same as Completion Date of existing CaseMilestone in old org.

Filter Reports via URL Parameters in Lightning Experience

No need to futz with filters! Pass URL parameters to set filter values in Lightning Experience reports. When linking to reports or when bookmarking a report, add filter value parameters to the URL to customize how the report filters when opened. For example, bookmark your opportunities report and add a filter value parameter to specify whether you see New Business or Existing Business.This feature is new in Lightning Experience.

Periodically throughout your day you check your opportunities report, and after opening it, you always edit a filter to show New Business or Existing Business. (The Type filter includes both New Business and Existing Business by default.)

Report without filter value URL parameter

Instead of loading the report, editing the filter, and reloading the report, create two bookmarks. One with a filter value parameter for New Business, and one with a filter value parameter for Existing Business. Take note of the opportunities report URL.

https://na1.salesforce.com/one/one.app#/sObject/00OR0000000PCHYMA4/view?t=1479844235107

Edit the URL to set the Type filter value to New Business by appending the parameter &fv0=New%20Business. The full, bookmarked URL reads:

https://na1.salesforce.com/one/one.app#/sObject/00OR0000000PCHYMA4/view?t=1479844235107&fv0=New%20Business

Let’s take a closer look at what the parameter means.

  • & — Denotes a new parameter in the URL. If no other parameters are present in the URL, then substitute ? in place of &.
  • fv0 — The fv stands for “filter value,” and is the name of the parameter. The 0 is the numerical order in which the filter appears in the report. (The first filter is 0, the second filter is 1, the third is 2, and so forth.) Standard filters don’t count in this order, and can’t be filtered using URL parameters, although they appear as the first three filters on any report. To set the value of the fifth filter in the report, specify fv4. In our example, we’re filtering the first field filter in the report, the Type filter.

    The order in which filters appear in Lightning Experience on the report run page isn’t necessarily the order that filters appear in the report. Locked filters are listed beneath unlocked filters on the run page, but aren’t necessarily ordered after all the unlocked filters. To see the order of filters in your report, refer to their order in the Report Builder. Alternatively, make a GET call to /services/data/v39.0/analytics/reports/<report ID>/describe and note their order in the JSON response.

  • =New%20Business — Specifies that the Type filter value is New Business.

    The parameter value must be URI encoded, which means certain characters (such as spaces) must be written in a format that URLs can understand. In our example, the space (‘ ‘) between “New” and “Business” becomes %20 when URI encoded.

When you navigate to your opportunity report through the URL with a filter value parameter, the report opens filtered and ready to read.

Report with a filter value URL parameter
Take note of these limitations to setting filter values via the URL.

  • Only field filters support edits from URL parameters. Standard filters (role hierarchy filters, scope filters, date filters), cross filters, and row limit filters aren’t supported. Standard, cross, and row limit filters don’t count when listing filters — fv0 is the first field filter.
  • Chart filters aren’t supported.
  • Filter operators (like equals and greater than) can’t be modified via URL parameters.
  • You can’t change the field being filtered via URL parameters.
  • You can’t add new filters to reports using filter value URL parameters. You can only modify existing filters.
  • You can’t delete filters from reports using filter value URL parameters. Setting a blank value filters by no text or numerals, but doesn’t remove the filter.

CREDIT: https://releasenotes.docs.salesforce.com/en-us/spring17/release-notes/rn_rd_reports_filter_url.htm

Calculate Week Days between two Dates in Wave Einstein analytics (SAQL)

Requirement: a SAQL query to find the number of WEEKDAYS between two dates. Specifically, can use this SAQL query in a Compute Expression

SAQL Expression: Here dataset name is “cases”. Start date is Created Date of a Case record and End Date is ClosedDate of Case.

Algorithm is copied from here: https://help.salesforce.com/articleView?id=000004526&type=1.

q = load "cases";
q = filter q by ClosedDate_sec_epoch != 0;
q = foreach q generate daysBetween(toDate("24-06-1985", "dd-MM-yyyy"), toDate(CreatedDate_sec_epoch)) % 7  as standardDateDiff, daysBetween(toDate(CreatedDate_sec_epoch), toDate(ClosedDate_sec_epoch)) % 7 as targetDatesDiff, Id as CaseId;
q = foreach q generate (case 
when standardDateDiff == 0 then (case targetDatesDiff when 1 then 2 when 2 then 3 when 3 then 4 when 4 then 5 when 5 then 5 when 6 then 5 else 1 end)
when standardDateDiff == 1 then (case targetDatesDiff when 1 then 2 when 2 then 3 when 3 then 4 when 4 then 4 when 5 then 4 when 6 then 5 else 1 end) 
when standardDateDiff == 2 then (case targetDatesDiff when 1 then 2 when 2 then 3 when 3 then 3 when 4 then 3 when 5 then 4 when 6 then 5 else 1 end) 
when standardDateDiff == 3 then (case targetDatesDiff when 1 then 2 when 2 then 2 when 3 then 2 when 4 then 3 when 5 then 4 when 6 then 5 else 1 end) 
when standardDateDiff == 4 then (case targetDatesDiff when 1 then 1 when 2 then 1 when 3 then 2 when 4 then 3 when 5 then 4 when 6 then 5 else 1 end) 
when standardDateDiff == 5 then (case targetDatesDiff when 1 then 0 when 2 then 1 when 3 then 2 when 4 then 3 when 5 then 4 when 6 then 5 else 0 end) 
when standardDateDiff == 6 then (case targetDatesDiff when 1 then 1 when 2 then 2 when 3 then 3 when 4 then 4 when 5 then 5 when 6 then 5 else 0 end) 
else 999 end) as result, targetDatesDiff as targetDatesDiff, CaseId as CaseId;
q = foreach q generate (floor(targetDatesDiff / 7) * 5) + result as WeekDaysOpened, CaseId as CaseId;
q = limit q 2000;

Why Email publisher action is not visible on Case though it is added to the page layout?

1. Ensure “Enable Case Feed Actions and Feed Items” is enabled under support settings. To modify Support settings : Go | Setup | Customize | Cases | Support Settings.

 

2. If you do not find the setting “Enable Case Feed Actions and Feed Items” under support settings, make sure “Feed Tracking” is enabled for case object. To enable Feed Tracking: Go | Setup | Customize | Chatter | Feed Tracking | Select Case object | Enable Feed Tracking.

 

3. The email Deliverability settings | Access level must be set to ‘All Email’ or this component will not render.

4. Please note that if you not at all find the Email Publisher action to add on Case page layout, it is because “Email to Case” is not enabled for the organization. To Enable: Go | Setup | Customize | Cases | Email to Case.