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
Showing posts with label sobject. Show all posts
Showing posts with label sobject. Show all posts
Monday, June 29, 2020
Export Salesforce Object field names into CSV file
Sample Code
Labels:
Apex,
CSV File,
objectName,
Salesforce,
sfdc,
sobject,
sobject details
Friday, February 14, 2020
How to access the child records from sObject using apex in Salesforce
Sometimes we run the dynamic query based on different inputs at the time we store the data in sObject or list<sObject>
from that data sometimes we have to get the child records info, to get the child records info use the
getSobject('<relationshipName>') or getSobjects('relationshipname')
Note
custom object relationships you would use the something similar to
sObj.getSObject('Your_Custom_Object__r')
Sample Code
Output
from that data sometimes we have to get the child records info, to get the child records info use the
getSobject('<relationshipName>') or getSobjects('relationshipname')
Note
custom object relationships you would use the something similar to
sObj.getSObject('Your_Custom_Object__r')
Sample Code
sObject sObj = [SELECT Id, Name, Account.Name, Account.Owner.Name FROM Contact WHERE Id = '003B000000FgA2JIAV']; String strContactName = (String)sObj.get('Name'); String strAccName = (String)sObj.getSobject('Account').get('Name'); String strAccOwnerName = (String)sObj.getSobject('Account').getSobject('Owner').get('Name'); System.debug('Contact Name ====> '+strContactName); System.debug('Account Name ====> '+strAccName); System.debug('Account Owner Name ====> '+strAccOwnerName);
Output
Friday, January 24, 2020
How to get All parent objects and child objects related to the current object
Assume Opportunity is the current object, you want to know all parent object and child objects info of the current object.
All Parent Objects
Sample Code
for(Schema.SobjectField iterator: Opportunity.SobjectType.getDescribe().fields.getMap().Values()) {
if(iterator.getDescribe().getType() == Schema.DisplayType.REFERENCE) {
system.debug('Parent Objects ====> '+iterator.getDescribe().getReferenceTo());
}
}
Sample Code
Schema.DescribeSObjectResult sObjectInfo = Opportunity.SObjectType.getDescribe();
for (Schema.ChildRelationship iterator: sObjectInfo.getChildRelationships()) {
system.debug('Child Objects ====> '+iterator.getChildSObject());
}
Labels:
Apex,
child objects,
current object,
parent objects,
Salesforce,
sfdc,
sobject,
sobjecttype
Thursday, May 2, 2019
How to check field exists or not on sobject using Apex?
Some scenarios, we need to validate field is exists or not on the object.
use below simple code it checks the field on an object if the field has existed on the object it returns true otherwise it returns false.
Result:
use below simple code it checks the field on an object if the field has existed on the object it returns true otherwise it returns false.
Example:
Boolean bHasField = Account.getSobjectType().getDescribe().fields.getMap().keySet().contains('phone');
System.debug('bHasField =====> '+bHasField);
Labels:
Apex,
check field on sobject,
Field on sobject,
Salesforce,
sobject
Subscribe to:
Posts (Atom)