Tuesday, July 4, 2017

Trigger Scenario in Salesforce: 

Auto populate a custom Text field "Account_Name__C " on opportunity product lineitem with an "Account Name" from the opportunity object or possibliy from Account Object itself, needs to happen when a user selects the opportunity product and saves the record.


Using the After trigger:
trigger updateOLI on OpportunityLineItem (after insert) {
      List<OpportunityLineItem> OpportunitiesToUpdate = new List<OpportunityLineItem>();
      for(OpportunityLineItem OLI : [Select Id, Opportunity.Account.Id From OpportunityLineItem Where Id IN : trigger.newMap.keyset()]) {
            OLI.Account_Name__c = OLI.Opportunity.Account.Name;
             OpportunitiesToUpdate.add(OLI);            
       }   
       if(!OpportunitiesToUpdate.isEmpty())
                update OpportunitiesToUpdate;
}
Using the before trigger:
trigger updateOLI on OpportunityLineItem (Before insert ,before update) {

for(OpportunityLineItem oli : trigger.new){
                oppids.add(oli.OpportunityId);
            }
            map<id,opportunity> mapopp = new map<id,opportunity>([select id,name,accountid,account.name                                                                                    from opportunity where id IN:oppids]);
            for(OpportunityLineItem oli1 : trigger.new){
              
                    if(mapopp.containsKey(oli1.OpportunityId)){
                        oli1.Account_Name__c = mapopp.get(oli1.opportunityid).Account.name;
                    }
}

           }

Salesforce Flow Interview Questions for admins – Part 23

121. How to create lookup field in Salesforce flow?
Ans : There is no direct way to create a lookup field in flow but we can use workaround mentioned in this post.

122 : How to handle fault in Salesforce flow?
Ans :
 We can send emails or fault screen can be created. If any element is connected to second screen, it automatically becomes fault screen and we can use “$Flow.FaultMessage” on screen to show error message. output text can be added on screen with message something like “Sorry, an error occurred in the page. For help, provide your administrator with the following information: {!$Flow.FaultMessage}”. Read more here.

123 : How to redirect Salesforce flow after completion, if end URL is known in advance?
Ans :

There are two approach :
First by using “retURL” parameter in URL of flow
and second,  if flow is used in Visualforce page then
1<apex:page>
2    <flow:interview name="MyUniqueFlow" finishLocation="{!$Page.MyUniquePage}"/>
3</apex:page>
4or
5<apex:page>
6    <flow:interview name="MyUniqueFlow" finishLocation="{!URLFOR('/home/home.jsp')}"/>
7</apex:page>

124 : What are difference between lookup and fast lookup data elements in flow?
Ans :
 Lookup record will return only first matching record however to get all matching record, we should use fast lookup. Any data element with prefix “fast” means to work in bulk. As shown in below image we have two flavors of Data element in flow for CRUD operation.
Salesforce Flow Data Elements
Salesforce Flow Data Elements

125: What is use of loop element in Salesforce flow ?
Ans :
 Loop element in Salesforce flow is used to iterate elements in collection. You can compare it with “for or while” loops in programming language. Below image shows sample of flow, which uses Loop element to mass update lead records by changing some value iteratively using loop element. You can check this YouTube video as well to see it in action.
Sample Salesforce flow using loop element
Sample Salesforce flow using loop element

126: Which interface needs to be implemented in Apex to be used in Flow ?
Ans :
 We can execute apex as well using flow by annotating it with “@InvocableMethod” and marking method as static. However this method only takes one parameter of type list. If we want to send multiple parameters, then simplest way is to create comma separated list of argument and pass it. In this method, we can break it and use according. Below is sample code
1Global class Flow_UpdateAccountField {   
2    @InvocableMethod
3    public static void performUpdate(List<String> lstCSV){
4        List<String> recIds = lstCSV[0].split(',');
5        //0 - AccId, 1-field1__c
6        Account acc = new Account(Id=recIds[0], field1__c=recIds[1]);
7        update acc;       
8    }
9 
10}

127 : How to create non mandatory dropdown field in Salesforce flow ?
Ans : We cannot create non mandatory dropdown field in flow (at time of writing this). However there is simple workaround as explained in this post.

128 : How to create two columns page layout in Salesforce Flow ?
Ans :
 We cannot create two column page layout in Salesforce flow (at time of writing this post). However we can use workaround explained in this post.

129 : How to set finish location of Salesforce Flow for newly created record inside flow ?
Ans :
 Currently there is no way to set finish location of Salesforce flow by point and click or Visualforce. We have to write Apex controller or controller extension to achieve this. Below is sample code.
Visualforce code:
1<apex:page standardController="Account" extensions="Flow_redirect_finish"
2    <flow:interview name="Create_Contact" interview="{!contactFlow}" finishlocation="{!NewRecordId}">
3        <apex:param name="varAccountId" value="{!$CurrentPage.parameters.parameter1}"/>
4        <apex:param name="varCallLogId" value="{!$CurrentPage.parameters.parameter2}"/>
5    </flow:interview>
6</apex:page>
Apex code :
1public class Flow_redirect_finish {
2    private Account objAcc ;
3    public Flow.Interview.Create_Contact contactFlow { get; set; }
4     
5    //Constructor for Controller extension
6    public Flow_redirect_finish(ApexPages.StandardController stdController){
7        objAcc = (Account)stdController.getRecord();
8    }
9     
10    private String readFlowVariable() {
11        if (contactFlow == nullreturn '';
12            else return contactFlow.varContactId;
13    }
14     
15    public PageReference getNewRecordId(){
16        return new PageReference('/' + readFlowVariable() );
17    }
18}

130 : How to create Dependent Picklist ?
Ans : Dependent picklist cannot be created in Salesforce flow. However there is work around. Lets say, if we need dependent picklist of level 3, like first select “country” and then “state” and then “city”, then we will need three screen. We can take help of Custom object, Custom setting or Custom Metadata types as discussed in this blog post.