Friday, 18 February 2022

How to pass wrapper class object to LWC component from Apex method ?

 Use Case - We need to have validation in apex code if any of the information missed we need to throw error. First Name, Last Name, SSN or DOB etc. Assume we have opportunity Id as parameter, will query Account information and add into a wrapper class.

Public class ResidentDetailRequest {

    @AuraEnabled public Integer FacilityNumber;
    @AuraEnabled public String FacilityName; 
    @AuraEnabled public String LastName;
    @AuraEnabled public String FirstName; 
    @AuraEnabled public String MiddleName;
    @AuraEnabled public String SSN;
    @AuraEnabled public String DOB;
    @AuraEnabled public String Suffix;
}

Main class.
public class ResidentValidationController {
    //public  static ResidentDetailRequest resident;
@AuraEnabled(cacheable=true)
    public static ResidentDetailRequest getResidentFromOpportunity(String opptyId){
        Opportunity oppy = getOpportunityFromId(opptyId);
        ResidentDetailRequest residentTemp;
        if(oppy != null){
        
            residentTemp = new ResidentDetailRequest();
            residentTemp.FirstName = oppy.Patient_Name_pc__r.FirstName;
            residentTemp.LastName = oppy.Patient_Name_pc__r.LastName;
            residentTemp.DOB = String.valueOf(oppy.DOB__c);
            residentTemp.SSN = oppy.SSN_ShieldEncryption__c;
            residentTemp.FacilityName = oppy.Facility_Name__c;
            residentTemp.FacilityNumber = Integer.valueOf(oppy.Facility__r.Facility_Number__c);
            residentTemp.MedicalRecordNumber = oppy.MedicalRecordNumber__c;
            
        }

       
        return residentTemp;
    }
    
  private static Opportunity getOpportunityFromId(Id opptyId){
        return [SELECT Id, DOB__c,Patient_Name_pc__c, Facility_Name__c, Last_Validation__c,                             Facility__r.Facility_Number__c,
                   SSN_ShieldEncryption__c,MedicalRecordNumber__c,
                   Patient_Name_pc__r.FirstName,Patient_Name_pc__r.LastName
                  FROM Opportunity WHERE Id =: opptyId];
    }
}

// LWC component code.
import { LightningElement,api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import getResident from 
'@salesforce/apex/ResidentValidationController.getResidentFromOpportunity';
@api recordId
connectedCallback() {
        console.log('this is oppy Id',this.recordId);
        if(this.recordId){
            getResident({opptyId:this.recordId})
             .then(res=>{
                this.resident = res;
                
                let missingFields = this.getMissingFields(res);
                console.log('field missed',missingFields);
                if(missingFields){
                    let msg = "Opportunity is missing some required fields ("+missingFields+")";
                    this.showError(msg);
                }
}).catch(err=>{
                    this.showError(err.body.message);
});
}
}
getMissingFields(resident){
        let missing = [];
 
        if(resident.FirstName == null || resident.FirstName.trim() == ''){
            missing.push("First Name");
        }
        if(resident.LastName == null || resident.LastName.trim() == ''){
            missing.push("Last Name");
        }
        if(resident.SSN == null || resident.SSN.trim() == ''){
            missing.push("SSN");
        }
        if(resident.DOB == null || resident.DOB.trim() == ''){
            missing.push("DOB");
        }

        console.log('missing fields', missing);
        if(missing.length > 0){
            return missing.join(', ');
        }else{
            return null;
        }
    }
showError(msg){
        const evt = new ShowToastEvent({
            title: 'Resident Validation Error',
            message: msg,
            variant: "error"
        });
        this.dispatchEvent(evt);
    }

The idea here is just to make one call to apex, get information and throw error
if any information missing combing.

Tuesday, 15 February 2022

How to get all country code and state code thorough Apex ?

 If we need to get state name or country from the code selected from country and state picklist, here is the sample code.

To access all the country codes in the ORG.

Schema.DescribeFieldResult fieldResult = User.Countrycode.getDescribe();

List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

System.debug('Picklist::'+ple);

for( Schema.PicklistEntry f : ple){

System.debug(f.getLabel() +'::'+ f.getValue());

}

To access all the state code in the ORG

Schema.DescribeFieldResult fieldResult = User.statecode.getDescribe();

List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

System.debug('Picklist::'+ple);

for( Schema.PicklistEntry f : ple){

System.debug(f.getLabel() +'::'+ f.getValue());

}

Tuesday, 8 February 2022

How to extract plain number from a Phone number?

We have different format of phone number, we may need to extracts just plain number from the phone number.
Here is the utility method we can add.

public static String normalizePhone(String initialString){
        String resultNumber;
        if(initialString != null){
            resultNumber = initialString.replaceAll('[^0-9.]', '');
        }
        return resultNumber;
    }

Thursday, 3 February 2022

How to give edit access of file attachments(content document ) to user ?

Use case - User should have ability to make change in file name which are getting created from third party. We are getting patient details from a hospitals using a third parry referral source. so Leads with all file attachments created in salesforce. Once lead is converted user should be able to change file name attached to opportunity/Lead.

Implementation :When content document/content document link created thorough integration, by default it must be provided ShareType as "V" that is why all the users can view, only admin can have access  to modify. 

https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_contentdocumentlink.htm

We need to update sharetype as "I" so that whoever has access to modify opportunity can modify file name.

There are different approach to achieve it. I did it through flow.

Step -1 Create Record Triggered flow

Step - 2 Entry criteria - when lead is converted, so IsConevrted is true

Step - 3 Query Content Document Link record List and pull shareType

Step -4 Have a loop running Content Document Link List.
Step -5   Get content Id from loop and update ShareType and sore updated content document in a list to update at a time.
Step 6- Check if Content Document list is not empty 
Step - 7 Update content Document List

Here is the total flow