Showing posts with label Salesforce. Show all posts
Showing posts with label Salesforce. Show all posts

Monday, February 10, 2020

How To Search/Open Record Detail Page in Live Agent Using REST API

In my previous posts we have seen how to send the pre-chat details,how to store pre-chat details in transcript object.

In this post I will explain how to perform the search in specified object with given filed names and details.If record found with given criteria how to open this record in chat details automatically .This in turn will helps the agents to save the time to look for the record by doing the manual search to answers the customer.

The key area that we need to look at is "entityMaps" array in prechatdetails and "prechatEntities" array .
  • entityMaps - Store object api name search field name and it's values
  • prechatEntities - which will stores the search object,search field,perform the search or not,create a new if not exist and whether to do exact match or not. 
{
   
   "organizationId":"00XXXXXXxfQ",
   "buttonId":"573xxxxxl",
   "deploymentId":"572xxxxxx9",
   "userAgent":"",
   "language":"en-US",
   "screenResolution":"1920x1080",
   "visitorName":"John A",
   "prechatDetails":[
       {
         "label":"search field api name",
         "value":"value for the search field",
         "displayToAgent":true,
         "transcriptFields": [],
          "entityMaps":[
            {
               "entityName":"object api name",
               "fieldName":"search field api name"
            }
         ]
      }
     
   ],
   "prechatEntities":[
      {
         "entityName":"object api name",
         "showOnCreate":true,
         "entityFieldsMaps":[
            {
               "fieldName":"field api name",
               "label":"field api name",
               "doFind":true,
               "isExactMatch":true,
               "doCreate":false
            }
         ]
      }
 ],
   "receiveQueueUpdates":true,
   "isPost":true
}

Sample Request:


  • Object To be Searched - Booking_Details__c
  • Filed Name to be Searched - Pnr__c
  • Field value - PNR_3563788883

{
   
   "organizationId":"00XXXXXXxfQ",
   "buttonId":"573xxxxxl",
   "deploymentId":"572xxxxxxx9",
   "userAgent":"",
   "language":"en-US",
   "screenResolution":"1920x1080",
   "visitorName":"John A",
   "prechatDetails":[
       {
         "label":"Pnr__c", // search field api name
         "value":"PNR_3563788883",//Value for pnr to be search
         "displayToAgent":true,
         "transcriptFields": [],
          "entityMaps":[
            {
               "entityName":"Booking_Details__c", //object api name
               "fieldName":"Pnr__c" //search field api name
            }
         ]
      }
     
   ],
   "prechatEntities":[
      {
         "entityName":"Booking_Details__c", //object api name
         "showOnCreate":true,
         "entityFieldsMaps":[
            {
               "fieldName":"Pnr__c", //search field api name
               "label":"Pnr__c", //search field api name
               "doFind":true, //tells to do search or not
               "isExactMatch":true, // do exact match value or not
               "doCreate":false // want to create if not found the record
            }
         ]
      }
 ],
   "receiveQueueUpdates":true,
   "isPost":true
}

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..!




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 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..!







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...



Tuesday, December 31, 2019

typeAttributes in lightning-datatable or lightning:datatable

Basically typeAttributes are the one of the major building blocks of <lightning-datatable>(in LWC) or <lightning:datatable> (in Aura).This will help us to format the data types /output display and attaching some of actions to your data displayed in table at each cell level ,row level or at the columns level.

The typeAttributes are attached to each columns data type when your specifying the columns for your data table in your java script file.

Each typeAttributes is having pre-defined set of attributes/properties basis on the data type of your column (your defining at the time of columns for table).

Usually each column definition will have below properties.

{ 
  label: 'Issue Type',  //Column Header Shown in Table
  fieldName: 'Issue_type__c', //Api name of filed 
  type:"text",   // tells text,date,url,email etc..
  initialWidth: 90 //column width
}

After attaching the typeAttributes the sample code looks like below.The sample code will create a column with name Case Number and Looks like a button ,on click of it you can fire rowAction automatically.


{
        label: 'Case Number',
        type: 'button',
        initialWidth: 90,
        typeAttributes: {
            iconName: 'action:preview',
            title: 'Preview',
            variant: 'border-filled',
            alternativeText: 'View',
            disabled: false,
            label: { fieldName: 'CaseNumber' }, // to assign the value dynamically
            name: { fieldName: 'Case_Age__c' }, // to assign the value dynamically
        }
    },

//other colums

At any time if you want to set any of the typeAttribute object attribute dynamically please use below syntax


  attributeName: { fieldName: 'api name of column/any other variable' }
          

As I have mentioned on the top each typeAttributes will have some set of properties based on the data type of your column.

If you want to know what are the available attributes for each datatype please refer below.




Sample Column Code With typeAttributes:


const datatableColums =[
 //Column 1 of type Url
    {
        label: 'Customer Name', fieldName: 'nameUrl', type: 'url',
        typeAttributes: {
            label: { fieldName: 'Name' },
            target: '_top'
        }
    },
 
 //Column 2 of type button 
 {
        label: 'PNR',
        type: 'button',
        initialWidth: 90,
        typeAttributes: {
            iconName: 'action:preview',
            title: 'Preview',
            variant: 'border-filled',
            alternativeText: 'View',
            disabled: false,
            label: { fieldName: 'Pnr__c' },
            name: { fieldName: 'Pnr__c' },

        }
    },
 
 //Column 3 of type date 
 {
        label: "Closed Date",
        fieldName: "ClosedDate",
        type: "date",
        typeAttributes:{
            weekday: "long",
            year: "numeric",
            month: "long",
            day: "2-digit"
        }
    }
]

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..!






How To Build A Global Search URL In Lightning Experience Using JavaScript

If your in lightning experience on click of any actionable item you want to take the user to the Global Search and perform the search action using the dynamic search string.To get the url of the global search in lightning using java script please refer the below code.


var searchStr = 'Sree'; //Search String
var stringToEncode = '{"componentDef":"forceSearch:search","attributes":{"term":"'+searchStr+ '","scopeMap":{"type":"TOP_RESULTS"},"context":{"disableSpellCorrection":false,"SEARCH_ACTIVITY":{"term":"'+ searchStr + '"}}}}';
var encodedStr = btoa(stringToEncode); //Base64 encoding
var redirectUrl='/one/one.app?source=aloha#'+encodedStr;

The format of url must be

     String redUrl = '/one/one.app?source=aloha#'+base64EncodedSearchString;

If you want to the same thing using apex please refer this post

Hope this helps you..Enjoy..!

How To Build A Global Search URL In Lightning Experience Using Apex

If your in lightning experience on click of any actionable item you want to take the user to the Global Search and perform the search action using the dynamic search string.To get the url of the global search in lightning using apex please refer the below code.


String searchStr = 'Sree'; //Search String
String stringToEncode = '{"componentDef":"forceSearch:search","attributes":{"term":"'+searchStr+ '","scopeMap":{"type":"TOP_RESULTS"},"context":{"disableSpellCorrection":false,"SEARCH_ACTIVITY":{"term":"'+ searchStr + '"}}}}';
String encodedStr = EncodingUtil.base64Encode(Blob.valueOf(stringToEncode)); //Base64 encoding
String redirectUrl='/one/one.app?source=aloha#'+encodedStr;

The format of url must be

     String redUrl = '/one/one.app?source=aloha#'+base64EncodedSearchString;

If you want to the same thing using java script please refer this post


Hope this helps you..Enjoy..!




Saturday, December 28, 2019

What is Salesforce API Portal?

This is the new feature of Salesforce which will help us to find all api details to connect with any of their platforms/entire ecosystem through apis.

We can also say it's one place store to check out all salesforce api details.

This will also shows what is the sample request and response details for each and every api.

It will also helps the developers to learn with an interactive document.

This helps the developer to connect with entire eco system.

This is built on Mulesoft API Community Manager.

Sample API/Clouds:

  • B2C Commerce Developer Sandbox API's
  • Einstein Prediction Service Scoring API
  • Einstein Vision and Einstein Language
  • Marketing Cloud REST API

For more details please check Salesforce Official Documentation.

Hope this helps you..Enjoy..!



Tuesday, December 17, 2019

LWC Toast Messages in Classic Environment Using Lightning Out

Usually we will use Lightning Out feature to use the any lightning components to worked/used in classic environment .Basically we will be embedding our lightning components inside the visual force page to use them in Classic environment.

If your using LWC Lightning Out feature and if you want to show the any notifications similar to apexpagemessages in visual force pages we will be using the toast messages in lightning environment.But unfortunately the toast messages will not work in classic environments.

To overcome this issue I have built a generic component with help of slds styles to bring the same style of toast messages so that we can use this component in classic as well.

genericToastMessages.html


<template>
    <template if:true={showAnyMsg}>
        <div class={notifcationThemeClass} role="alert">
            <span class="slds-assistive-text">{notificationtype}</span>
            <span class={notifcationContainerClass} title="Description of icon when needed">
                <svg class="slds-icon slds-icon_x-large" aria-hidden="true" xmlns="http://www.w3.org/2000/svg">
                    <use xlink:href={iconClass}></use>
                </svg>
            </span>
            <h2>{notificationmessage}</h2>
          
        </div>
    </template>
</template>

genericToastMessages.js


/* eslint-disable no-console */
/* eslint-disable no-unused-vars */
/* eslint-disable no-else-return */

import { LightningElement,api, track } from 'lwc';
export default class GenericToastMessages extends LightningElement {
    @api notificationtype; //='success';
    @api notificationmessage;//='All working!';
   
   get showAnyMsg()
   {
        if (this.notificationtype !== undefined && this.notificationmessage !== undefined)
            return true;
       else return false;
   }

   get notifcationThemeClass() {
     return (
            "slds-notify slds-notify_alert slds-theme_alert-texture slds-theme_" +this.notificationtype
        );
    }

    get notifcationContainerClass() {
       return (
            "slds-icon_container slds-icon-utility-" +this.notificationtype +" slds-m-right_x-small"
        );
    }

    get iconClass() {
        return (
            "/apexpages/slds/latest/assets/icons/utility-sprite/svg/symbols.svg#" + this.notificationtype
        );
    }
}

This component mainly takes 2 params as an input or attributes those are

  • notificationtype - success/error/warning/info
  • notificationmessage - Any message you want to show to user

How to call this component from other components

Just include the below 2 lines in your component where you want show notification


 <template if:true={showErrMsg} >
<c-generic-toast-messages notificationtype="error/success"
 notificationmessage="All Records Updated successfully">
</c-generic-toast-messages>
        <div class="slds-m-bottom_x-small"></div>
</template>


Other way of doing is passing the type and messages dynamically even in your parent component.You can even find example in the post How To Select And Update Multiple Records Using LWC


 <template if:true={showErrMsg} >
<c-generic-toast-messages notificationtype={notifType} 
notificationmessage={notifMsg}>
</c-generic-toast-messages>
        <div class="slds-m-bottom_x-small"></div>
</template>


In the above code {notifTpe} and {notifMsg} are the parent component js controller variables where I'm setting the value basis on the server response.





Hope this helps you..Enjoy..!