Monday, June 8, 2015

Resolving Selective/Non-Selective Issue with Query Plan


OverView :
Query Plan tool is analytical tool which  helps in increasing query performance in the system.As a developer i ran into issue where i have faced "Selective /Non-Selective Exception"."Selective /Non-Selective Exception" which states how much data we are going to hit and  result(#No of records going to reterive).Salesforce Architecture is something which enforce optimum use of resources does'nt matter whether character in class/custom field or rollup field limitation.

Enable : Query Plan
              Open Developer Console --->Help--->Preferences--->Enable query Plan

@Bottom of developer console --> Option for query Plan.

Understanding Query Plan :
Cardinality
The estimated number of records that the leading operation type would return. For example, the number of records returned by an index.
Fields
 The indexed field(s) used by the query. 
 If query has Index field : 
    Fields : Indexed Field else null
LeadingOperationType
The primary operation type that Salesforce will use to optimize the query.
  • Index - The query will use an index on the query object.
  • Other - The query will use optimizations internal to Salesforce.
  • Sharing - The query will use an index based on the sharing rules associated with the user who is executing the query. If there are sharing rules that limit which records that user can access, Salesforce can use those rules to optimize the query.
  • TableScan - The query will scan all records for the query object.
Notes
An array of one or more feedback notes. Each note contains:
  • description - A detailed description of an aspect of the optimization. This could include information on optimizations that could not be used, with details on why the optimization was not used.
  • fields - An array of one or more fields used for the optimization.
  • tableEnumOrId - The table name for the fields used for the optimization.
This response field is available in API version 33.0 and later.
RelativeCost : It is cost (Selectivity Threshold) of query as compared to or relative to force.com native query Optimizer
   If Cost  Value  >1 mean that the query won’t be selective.(Red Signal): May Hit exception 
  It must be <1 and preffered value is : 0.0n....(0.024 etc) n (Green Signal ) No Exception
SobjectCardinality:
The approximate record count for the query object.


Note: Using Index field in query may not always resolve "Selectivity /Non-Selectivity Issue".In some cases have to use 2nd index field check the cost .

Key Point :

Try And Clause with Custom Index avoid ORING In Query
Avoid !=(-ve Operator)

In Case :If You are using multiple OR Clause  in Query:Try to make  OR Condition  selective individually.
Ex:

OR (A) OR (B) OR (C)
A: Selective with use of index or by other mean
B: Selective with use of index or by other mean
C: Selective with use of index or by other mean



Note : Technical Resource

https://developer.salesforce.com/page/Developing_Selective_Force.com_Queries_through_the_Query_Resource_Feedback_Parameter_Pilot

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_query_explain.htm











Wednesday, May 20, 2015

Sneak into Writing Test Classes

On a sunny day , working from my apartment Balcony :
working on test classes project i have to struggle for code coverage,Though it is said to be simple task but some times found people juggling with obtaining coverage .i would like to introduce some challenges that every developer faces:

a) if a object have many lookup and some complex trigger/classes structure may hit too many soql
b)After trying so many combination of conditional block  can lead  no +ve in code coverage.
c)May hit developer time span to write test class leading migration activity delay
d)In efficient code coverage without writing unit  test for  +ve and -ve scenario.
e) Improper Exceptions Handling


Here are ways to handle:

a/b) If object may have multiple lookups and complex structure with triggers/classe :
-do not provide all lookup at a time since lookup population may be distributed as business

for example : If custom object "aa__c" have lookup let suppose account/contact/case/....it is possible aa__c record may have account/contact for some business and for other business account with other lookup.

So Break single method in multiple as in 1 : populate two lookup or as per your convience in 2: other two....this should be staritgey .

Cover method code in start test/stop test

b) if calling Class method from trigger: in a complex situation while writing test if method not calling -->call that method within testclass while passing argument.

trigger
{
class_abc.pqr(trigger.new);
}

public class class_abc
{
pqr(list<sobject> lst)
}

@test
{
method1()
{
insert record;
list.add(record); // add to list
classinstance.pqr(list);
}
}

c/d)Include if /else condition : set up data for + and - coverage
e)There may be scenarion you are getting last line exception when executing unit test  and after juggling many hours not fixing it up and got no solution in that case use try-catch block.
f) You can call trigger-class methods in test class by passing parameters/argument if any.all method in class will be cover.

Thanks





Monday, April 20, 2015

Issue with RestUrl Salesforce

Point Regarding Rest Api Salesforce :


When sending rest request to the resturl , Make sure added the name space in Url.
if Name space not provided will throw the error :
[
    {
        "errorCode": "NOT_FOUND",
        "message": "Could not find a match for URL /Account/0019000001InXHd"
    }
]

So the request Url should be like :when Class is exposed as restresource:
     https://Instance.salesforce.com/services/apexrest/icoder/Account/AccountId
     https://Instance.salesforce.com/services/apexrest/namespace/Account/AccountId

Saturday, April 18, 2015

Email-to-Case Salesforce Engine

Email-to-Case functionality works like  success factor for company where customer can connect efficiently with quick interaction via email.
Customer can create ticket/cases while sending email to company support process  mail , ticket/inquiries can direct be placed to customer account in CRM.

It is process by which customer query can be resolved via interaction through email.

Process Flow Email-to Case SFDC:

  • When Customer send any  product malfunction/outage/breakdown  inquiries through email , Salesforce automatically logged a case and link the Account(Customer) and Contact(found by Email) to Case.
Feature can be setup:

Customize->Case->Email-to-case

  • Enable Email to Case
  • Enable Notify Case Owner on new Email

When sending Email from Case : Thread Id in subject & In Body

Thread Id : An id which is used for communication to & from for case to identify case related email thread.
if Customer reply to the mail having thread id in subject and in body, Email will automatically be linked to the Case.
All the email activities gone through this email(having ThreadID) communication will be recorded on case.

In Section Routing Address : 
Provide  support@company domain  in email,click on save--> an email notification will be received on "support@company domain"-->verify link-->
there will be email service address.

Set Email FWD setting on "support@company domain" -->Enter Email Service address (Recieived in  Section Routing Address).

Here its all done.

So when a customer send email @support it will forward email to Salesforce Email Service Address and Salesforce Engine eventually create a Case.

Thursday, April 2, 2015

Google Map : Simplifying Map In Visualforce

Salesforce spring15 release which bring some cheers to developer where implementing requirement
related to google map.

As earlier a lot of work needs to be done like div componant to create canvas for map binding dynamically data to marker ,use of javascript url(Map api)  on VF  to show location specific data.

Now we have quick solution to show google map on Vf using simple <apex:> Tag.We have <apex:map> component to show  a map canvas on Visualforce page.Using  <apex:map> and <apex:mapMarker> we can easily implement location on map.

There are certain attribute that need to be set while displaying map.
<apex:map>
Attriute : mapType
The type of map to display. Must be one of the following:

  • hybrid
  • roadmap
  • satellite



height: sets size
width : sets size

<apex:MapMarker>
position Object Specifies the location of the marker.

Code Snippet:

<apex:map width="600px" height="400px" mapType="roadmap"  center="Account Billing address">
   <apex:mapMarker title="{! contact.Name }"
     position="{!contact.MailingStreet},{!contact.MailingCity},{!contact.MailingState}"/>
</apex:map>

Note : Enable Map setting:
Customize-->Map and Location-->enable



http://docs.releasenotes.salesforce.com/en-us/spring15/release-notes/rn_vf_mapping_components.htm

Sunday, July 8, 2012

Rollup Summary In Validation: Avoid Trigger

There are certain simple scenarios where we think we have to write code and then again writing apex , test class and coverage..a lot hectic things.But if we thinks Configuration Power so many of such things can be avoided and can be handeled easily.
Here i am disscusssig some configuration Tips though simple but very efficient .

If You want to Validate Parent Object on behalf of Child Records.
Approach : Avoid Trigger
Create RollupSummary ,Set Filter Criteria On Rollup Field.
Use RollUp Summary In Validation Rule on Parent
Its Done.


Use  apex Trigger only when there is complex Scenarion.



Regards
Clouder
Simplifying enterprise architecture







Saturday, February 11, 2012

Hi ,
This is in regarding script statement,Script statement are nothing but Executable Line in Code .

Example :

1):Conditional Statement
2)Assignment
3)For loop
4)While
5)Transaction Statement

Example:


If(Name =='Mohit')
{
Statement 1;
}
Else
{
Statement 2;
}

In This Code Script Statement is 1 since it is conditional Either Statement 1 or Either 2 Statement will Execute.



Some more Illustration for Optimizing Script Statement..Write Effective code

List<account> lstact=[select id ,name from account where name='abc'];
for(integer i=0;i<lstact.size();i++)
{
string str=lstact[i].name;
}
 Result: Please Note Down Number of script Statement: 3











Code Listing 2: Please Note Down here Number of Script Statement :1

for(account varact:[select id ,name from account where name='abc'])
{
string str=varact.name;
}













Try To reduce : Number of Script Statement.Effective Code writing.

If you feel unappropriate please say,this is my first blog for any mistake i apologize.