Monday, June 29, 2020

Export Salesforce Object field names into CSV file

Sample Code
Map<String, Schema.SObjectField> fMap = Schema.getGlobalDescribe().get('Account').getDescribe().Fields.getMap();
list<string> selectFields = new list<string>();
string excelHeader = 'ApexName\n';
if (fMap != null){
    for (Schema.SObjectField ft : fMap.values()){
        Schema.DescribeFieldResult fd = ft.getDescribe();
        excelHeader += fd.getName() +'\n';
    }
}
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(excelHeader);
string csvname= 'AccountFields.csv';
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] toAddresses = new list<string> {'Your Email'};
String subject = 'Account fields CSV';
email.setSubject(subject);
email.setToAddresses(toAddresses);
email.setPlainTextBody('Account Fields');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
Output



How to convert Set Id to Set String in Apex code?


Apex code is not supporting converting set<Id> to set<String> direct way – we have to do a workaround

Sample Code
Set<Id> ids = new Set<Id>{'001G0000023lIWq','a61G0000000TOSE'};

Set<String> idStrs = (Set<String>)JSON.deserialize(JSON.serialize(ids), Set<String>.class);

System.debug('idStrings=' + idStrs);

Tuesday, June 23, 2020

How to check Lightning Web Component(lwc) Is Connected to the DOM?

we have already connectedCallback() and disconnectedCallback() lifecycle hooks to react when a component is connected to and disconnected from the DOM. However, using the Node.isConnected property is more ergonomic developer experience.

The Node.isConnected property, which returns a Boolean indicating whether the component is connected to the DOM. Simply use this.isConnected in a component’s JavaScript.

import { LightningElement } from 'lwc';
export default class MyComponent extends LightningElement {
    someMethod() {
        const cmp = this;
        const aPromise = new Promise(tetherFunction);
        aPromise.then((result) => {
           if (cmp.isConnected) {
               // Update the component with the result.
           } else {
               // Ignore the result and maybe log.
           }
        })
    }
}

Friday, June 19, 2020

Display account related contacts using map in salesforce

Sample Code
map<String, list<Contact>> mapAccAndCons = new map<String, list<Contact>>();
list<Contact> lstCons = [Select Id, Name, Account.Name from Contact];

for(Contact conIterator : lstCons) {
    if(!mapAccAndCons.containsKey(conIterator.Name)) {
        mapAccAndCons.put(conIterator.Name, new list<Contact>{conIterator});
    }
    else {
        mapAccAndCons.get(conIterator.Name).add(conIterator);
    }
}

System.debug('mapAccAndCons => '+mapAccAndCons);

Output
 

Monday, June 8, 2020

How to get Org Instance name using SOQL

SOQL Query
String strInstanceName = [Select instanceName From Organization LIMIT 1].instanceName;
System.debug('strInstanceName ===> '+strInstanceName);

Output