Thursday, January 30, 2020

After Update Trigger In Salesforce Apex

Trigger Scenario:

Lets take a sample use case when an Opportunity stage has been changed to Closed-Won ,I want to create a new Invoice(custom object) record with below data mapping.
  • Invoice Opportunity - Same Opportunity where Stage has been modified.
  • Invoice Account - Account Related To This Opportunity.
  • Invoice Total Quantity - The sum the quantity of all opportunity line items(opportunity products) related to the same Opportunity.


The relationship between all these objects as below




Source Code:


trigger OpportunityTrigger on Opportunity (after update)
{
    if(Trigger.isAfter && Trigger.isUpdate)
 {
  OpportunityTriggerHandler.opportunityAfterUpdate(Trigger.new,Trigger.oldMap)
 }
}


Public Class OpportunityTriggerHandler
{
  public static void opportunityAfterUpdate(List<Opportunity> TriggerNew,Map<Id,Opportunity> TriggerOldMap)
  {
   Set<id> setOppIds = new Set<id>();
   Map<Id,Decimal> mapOppIdtoSumOfLineitems = new Map<Id,Decimal>();
   for(Opportunity opty:TriggerNew)
   {
    if('Closed Won'.equalsIgnoreCase(opty.StageName) && opty.StageName!=TriggerOldMap.get(opty.Id).StageName)
    {
     setOppIds.add(opty.id);
    }
   }
   
   if(!setOppIds.isEmpty())
   {
    List<Invoice__c> listInvocesToInsert = new List<Invoice__c>();
    for(Aggregateresult agr:[SELECT sum(quantity) qunty,Opportunityid FROM Opportunitylineitem WHERE Opportunityid IN:setOppIds GROUP BY Opportunityid])
    {
                mapOppIdtoSumOfLineitems.put((id)agr.get('Opportunityid'),(decimal)agr.get('qunty'));
    }
    
    for(Opportunity op:TriggerNew)
    {
     if('Closed Won'.equalsIgnoreCase(op.StageName) && op.StageName!=TriggerOldMap.get(op.Id).StageName && mapOppIdtoSumOfLineitems.containsKey(op.id)
     {
      Invoice__c inv = new Invoice__c();
      inv.name = 'Trigger-'+op.Name;
      inv.ParentAccount__c=op.Accountid;
      inv.ParentOpportunity__c=op.id;
      inv.Discount__c=mapOppIdtoSumOfLineitems.get(op.id);
      listInvocesToInsert.add(inv);
     }
   }
   if(!listInvocesToInsert.isEmpty())
    Database.insert(listInvocesToInsert,false);
   }
  }
}

Please comment or write us if you have any queries/requirements.

Please like,follow,bookmark,subscribe this site to receive daily updates.



Hope this helps you..Enjoy..!




What is Full Form Of LWC?

LWC stands for Lightning Web Components .This will be used for building the components in lightning experience (like aura ).
  • These will uses html,modern java script and css.
  • It uses core web components standard.
  • Because it’s built on code that runs natively in browsers, Lightning Web Components is lightweight and delivers exceptional performance.
  • The existing aura and lwc components can co exist in lightning experience.
For more information please refer salesforce documentation.

Please comment or write us if you have any queries/requirements.

Please like,follow,bookmark,subscribe this site to receive daily updates.





Hope this helps you..Enjoy..!


Monday, January 27, 2020

How To Deploy Lightning Web Components(LWC)

If you want deploy Lightning Web Components( LWC) using the change set please use the below steps.

  • Create an outbound change set.
  • Click on Add to add the components.
  • Select Component Type as "Lightning Web Component Bundle".

  • Select the one you want to deploy and add for deployment.



Hope this helps you..Enjoy..!



Sunday, January 26, 2020

How To Access Custom Permission In Validation Rule and VF Pages

To refer the custom permissions assigned to users through permission sets/profles in validation rules/pages please use below code

In Validation Rules/VF Pages

$Permission.CustomPermissionApiName

Example:

$Permission.Srinivas_4SFDC

Please comment or write us if you have any queries/requirements.

Please like,follow,bookmark,subscribe this site to receive daily updates.




Hope this helps you..Enjoy..!

Custom Permissions Are Not Working In Apex

Lets assume you have created a custom permission and it's assigned to user through permission sets.

Now when your referring the same value in apex for the assigned user it's still giving a false instead of the true value .

This could be happening because of the  Session Activation Required setting is enabled on the permission set level.

Please disable this and try it out probably it should work.





Please comment or write us if you have any queries/requirements.

Please like,follow,bookmark,subscribe this site to receive daily updates.




Hope this helps you..Enjoy..!

How To Access Custom Permission in Apex

  • Let's assume you have created a custom permission and it's been assigned to permission set or profiles.
  • Now we can refer the same custom permission in apex to check whether it's enabled for logged in user or not.
  • We use will the checkPermission() method from the FeatureManagement class for the same.

Boolean enabSet = System.FeatureManagement.checkPermission('Custom Permission APi Name');
System.debug('enabSet..'+enabSet);

Example:
Boolean enabSet = System.FeatureManagement.checkPermission('SreeTest');
System.debug('enabSet..'+enabSet);

Please comment or write us if you have any queries/requirements.

Please like,follow,bookmark,subscribe this site to receive daily updates.



Hope this helps you..Enjoy..!

Friday, January 24, 2020

How To Configure Console Components In Lightning Console Applications

Unfortunately Salesforce doesn't support console components in lightning experience.

If your existing component is linked to a visual force page and then you want to use this component as a console component in lightning experience the workaround is below.

1. Goto Service Setup

2. Open the App Manager Under User Interface Section

3. Select the application where you want to add console component and Click on Edit

4. Select Utility Items Under App Settings



5. Click on Add Utility Items and Select Visual Force Page option

6. Please give console component name and it's related visual force page(your vf page is not shown)

7. Your component is ready and it's available in the bottom left.


Please comment or write us if you have any queries/requirements.

Please follow,bookmark,subscribe this site to receive daily updates.

Facebook - https://www.facebook.com/ILoveCodingYou/?ref=bookmarks

Hope this helps you..Enjoy..!

How To Disable The Switch To Lightning Experience For An User

If you want to disable to switch to lightning experience link a user ,please follow below steps.

1. Identify the list of users to whom you want to disable this feature

2.Find out all the profiles related to these list of users

3. Goto each profile and Click on Edit

4. Uncheck the Lightning Experience User setting under the Administrative Permissions.


5. Now users will not get this switch permission .

Note:

This entire flow what we discussed above will disable this feature for all the users under the same profile.

If you want to do it for specific user disable Lightning Exp User at profile completely and create a permission set with Lightning Experience User enabled and assign permission set to set of people who required this permission.


Please follow,bookmark,subscribe this site to receive daily updates.

Facebook - https://www.facebook.com/ILoveCodingYou/?ref=bookmarks


Hope this helps you..Enjoy..!

How To Restrict The Record Edit/Modification Access Only To Record Owner and System Admin

If you have a requirement like the record can only be modified either by the owner of the record or the system administration .Other than this if any user tried to modify the record we shouldn't allow them to modify it.

Please use the validation rule to achieve this with below conditions check.

AND( 
     NOT(ISNEW()),
     NOT(OR($Profile.Name == 'System Administrator', $User.Id == OwnerId))
   )


Please follow,bookmark,subscribe this site to receive daily updates.

Facebook - https://www.facebook.com/ILoveCodingYou/?ref=bookmarks


Hope this helps you..Enjoy..!

Wednesday, January 8, 2020

How To Populate A Map With List or Set (Where Collections As a Value) in Salesforce

As a developer most of the times we will be working on collections.One of the most common used collection in salesforce is maps.

Creating a simple maps(value as a string,integer,object etc..) is very easy
  • Map<String,String>
  • Map<String,Integer>
  • Map<key,objects>
The real picture comes into scenario when we want to build some complex maps(where value of a map it self again a collection like list,set etc) 
  • Map<String,list<Account>>
  • Map<integer,set<integer>>
  • Map<AccountId,list<Conatct>>
Most common mistakes that we do while building this complex maps are
  • Old value gets overrides by new values.
  • When your in loop you map always ends up with just holding the last record in list/set.
  • Didn't checking whether value is already is exist or not.
Scenario:

Let take a take I want to create a map which will takes case status as key and it's value is all the cases comes under the same status.

Map<String,List<Case>> mapStatusToCases = new Map<String,List<Case>>()

for( Case cs: [SELECT id,Status,CaseNumber FROM Case limit 20])
{
 //Check whether with same status any record is already is added to map or not
 
 if(mapStatusToCases.containsKey(cs.Status)) 
 {
    // If you directly put this case into the map it will overrides all the existing cases,
   // which is not correct.So get all the existing records and store it list/set.
  List<Case> existingCase = mapStatusToCases.get(cs.Status);
  //add this new record to existing list  
  existingCase.add(cs);
  // now put the same status into the map with all latest case records(existing+new),
  // because key is same so it will overrides existing values with this latest list
  mapStatusToCases.put(cs.Status,existingCase); 
 }
 
 else // this executes if the status is not there in map(it means coming first time)
 {
     //prepare variable of map value type
  List<case> lstNewCases = new List<Case>();
  // adding a new record
  lstNewCases.add(cs); 
  // add the new status as a key and case as a value into map
  mapStatusToCases.put(cs.Status,lstNewCases); 
 }
}

System.debug('All Key Set...'+mapStatusToCases.keySet());
System.debug('All Values in Map...'+mapStatusToCases.values());
System.debug('Complete Map Details...'+mapStatusToCases);

For better understanding in above I have written each statement in a separate line. The same code can also be written in an optimized way as shown below.Both are valid and same.


Map<String,List<Case>> mapStatusToCases = new Map<String,List<Case>>()

for( Case cs: [SELECT id,Status,CaseNumber FROM Case limit 20])
{
 //Check whether with same status any record is already is added to map or not
 
 if(mapStatusToCases.containsKey(cs.Status)) 
 {
  //this statement mapStatusToCases.get(cs.Status) will returns the list
  //So,I'm adding new record to existing list directly
  mapStatusToCases.get(cs.Status).add(cs); 
 }
 
 else // this executes if the status is not there in map(it means coming first time)
 {
     //Delcared a new list and directly adding a new record to that
  mapStatusToCases.put(cs.Status,new List<Case>{cs}); 
 }
}

System.debug('All Key Set...'+mapStatusToCases.keySet());
System.debug('All Values in Map...'+mapStatusToCases.values());
System.debug('Complete Map Details...'+mapStatusToCases);

OutPut:




Please watch this space more useful stuff.

Please follow,bookmark,subscribe this site to receive daily updates.

Facebook - https://www.facebook.com/ILoveCodingYou/?ref=bookmarks


Hope this helps you..Enjoy..!







Monday, January 6, 2020

Origins may not contain paths or queries, since they are not part of the origin in Salesforce

You might encounter with this while adding your community URL or domain address in CORS or While setting up the Snap Ins(Known as (Embedded Service Chat).

The error indicates the url we are should not have any paths and we should take only the url till .com/.in/.domain etc..

If you look in this  scenario the url needs to be added is

https://srinivas4sfdc-developer-edition.ap1.force.com/HelpCust

The one highlighted in yellow can be considered as path or queries.So,please copy your url till the domain(till ends with .com/.in/.edu etc) name not the full url.

The Final url we need to add in CORS or SnapIns settings is 

https://srinivas4sfdc-developer-edition.ap1.force.com/


Hope this helps you..Enjoy..!



Thursday, January 2, 2020

Unable To Find My Visual Force Page In Utility Items Of App Settings In Lightning Experience

When your trying to customize any one of your lightning application and some times your trying to add your visual force page as a component but your visual force page is not showing there?

Then please check out the below settings.

Please goto your visual force page and check whether it's enabled to use in lightning application/lightning experience or not?

If not please enable the option "Available for Lightning Experience, Lightning Communities, and the mobile app"


Note: Not only in Utility Items,anywhere in lightning experience if your visual force page is not shown please check these settings.This could be the major issue in most of the cases.

Please comment or write us if you have any queries/requirements.

Please follow,bookmark,subscribe this site to receive daily updates.

Facebook - https://www.facebook.com/ILoveCodingYou/?ref=bookmarks

Hope this helps you..Enjoy..!


Wednesday, January 1, 2020

Few Scheduled Jobs Got Stopped Working Suddenly in Salesforce

If you have figured out that some of your scheduled jobs got stopped working suddenly.. There could be same issue which we have run into and figure out eventually after some time..

As the year got changed, while scheduling the jobs initially if you have mentioned a year as 2019 or till 2019 in your cron string expression then all your scheduled jobs comes under this category would have been stopped since Jan 1st 2020 at 12:00Am.

There could be some other reasons which might cause the issue but this is the one of route cause we have to cross as year got changed. 

Wish you a happy new year to everyone...



Happy New Year - 2020