Skip to content

Instantly share code, notes, and snippets.

View Sunil02kumar's full-sized avatar

Sunil Kumar Sunil02kumar

View GitHub Profile
@Sunil02kumar
Sunil02kumar / SK_AttachmentJSONParser.apxc
Last active February 4, 2020 03:03
Migrate Attachments from one Salesforce to another Salesforce Org
public class SK_AttachmentJSONParser {
public Integer totalSize;
public boolean done;
public cls_records[] records;
public class cls_records {
public String Id;
public string ParentId;
public String Name;
}
}
@Sunil02kumar
Sunil02kumar / gist:82ac1ca4a2965dc7ea91f6b5320340e7
Last active July 3, 2020 10:07
Custom Label : Fetch all Custom Label Information through Execute Anonymous Script
public static string finalCSVString ='Id,Name,Value\n';
findAllCustomLabels('');
public static void findAllCustomLabels(string endpointURL){
if(endpointURL=='' || endpointURL==null){
endpointURL='/services/data/v45.0/tooling/query/?q=Select+id,Name,Value+from+CustomLabel';
}
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');
@Sunil02kumar
Sunil02kumar / SK_BoxAPIUtility.apxc
Last active September 23, 2019 05:50
Box API-How to Regenerate Access Token from Refresh Token
public class SK_BoxAPIUtility {
//static method to get access token from refresh token
public static List<string> regenerateAccessToken(string clientId, string clientSecret, string refresh_token){
list<string> tokenlist = new List<string>();
HttpRequest req = new HttpRequest();
string endpointUrl='https://api.box.com/oauth2/token';
req.setbody('grant_type=refresh_token'+
'&refresh_token='+refresh_token+
'&client_id='+clientId+
'&client_secret='+clientSecret);
@Sunil02kumar
Sunil02kumar / SK_CustomlabelUtility.apxc
Created September 3, 2019 14:01
Fetch all Custom Label Information using Tooling API
public class SK_CustomlabelUtility {
public static string finalCSVString ='Id,Name,Value\n';
//variables to parse customlabel json response
public Integer size;
public Integer totalSize;
public boolean done;
public String nextRecordsUrl;
public String queryLocator;
public String entityTypeName;
@Sunil02kumar
Sunil02kumar / AccessingChildRecords.apex
Created August 11, 2019 03:49
Accessing Child Records from sObjects in Dynamic Queries Using Dynamic Apex
String qstring ='Select id,Name,(select id,Lastname,Email from Contacts),(Select id,Name,Stagename from Opportunities) from Account where id=\'00190000004Awos\' Limit 1';
List<sobject> sobjectList = database.query(qstring);
for(sObject sb:sobjectList){
System.debug('****Parent record-Account details:'+sb.get('Name'));
//check if account has any contact child records
List<sobject> contactChildObjects = sb.getSobjects('Contacts');
System.debug('****Child Contact Records size:'+contactChildObjects.size());
if(contactChildObjects.size()>0){
for(sobject con:contactChildObjects){
system.debug('Contact Id:'+ con.get('Id'));
@Sunil02kumar
Sunil02kumar / sObjectScript.apxc
Last active October 30, 2024 05:07
Accessing Parent Field Values from sObjects in Dynamic Queries Using Dynamic Apex
//------------Accessing parent field values in child to parent dynamic query----
//Below is static method which can be used to fetch field values.
//You need to pass sObject and fieldAPIName to get field value
public static string ExtractFieldValues(sObject sb, string fieldAPIName){
string fvalue='';
if(fieldAPIName.contains('.')){
List<string> splitedFields = fieldAPIName.split('\\.');
try{
for(integer i=0;i<splitedFields.size()-1;i++){
sb=sb.getSobject(splitedFields[i]);
@Sunil02kumar
Sunil02kumar / SK_AccountProcessBatch.cls
Created June 6, 2019 15:00
Fire Platform Events from Batch Apex using Database.RaisesPlatformEvents Interface
global class SK_AccountProcessBatch implements Database.Batchable<sObject>,Database.Stateful,Database.RaisesPlatformEvents{
global List<string> errorRecordsId=new List<String>();
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator('Select id,name,Type,rating,industry from Account Limit 1000');
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
List<Account> accountListForUpdate = new List<Account>();
for(sobject s : scope){
Account acc =(Account)s;
@Sunil02kumar
Sunil02kumar / script_to_find_All_child_records
Created June 1, 2019 03:04
How to get List of All Child Records for Given Parent Records in .csv File using APEX
string parentObjectName ='Account';//Specify Object API Name
string parentRecordId = '0019xxxxxxx7MqU'; //Specify record Id
//to store childRelationShipName
Map<integer,List<string>> childObjectmap = new Map<integer,List<string>>();
//to store inner query string for child objects
Map<integer,List<string>> childObjectQStringmap = new Map<integer,List<string>>();
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
for(String ss1: schemaMap.keyset()){
Schema.SObjectType objToken=schemaMap.get(ss1);
if(ss1.equalsignorecase(parentObjectName)){
@Sunil02kumar
Sunil02kumar / gist:0faf96afb2f300eaf225be431578afe9
Last active May 30, 2019 02:24
How to get all Child Relationship Names related to Parent Object using Apex
String parentObjectName = 'Account';
Map<string,string> objectRelationshipMap = new Map<string,string>();
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
for(String ss1: schemaMap.keyset()){
Schema.SObjectType objToken=schemaMap.get(ss1);
if(ss1.equalsignorecase(parentObjectName)){
//find details about sobject
Schema.DescribeSObjectResult objDescribe=objToken.getdescribe();
List<Schema.ChildRelationship> childRelationshipList = objDescribe.getChildRelationships();
system.debug('******childRelationshipList.size():'+childRelationshipList.size());
@Sunil02kumar
Sunil02kumar / SK_LightningNotificationToastDemoCmp.cmp
Created May 19, 2019 03:02
Lightning:NotificationsLibrary : Easy Way to Display Toast in Lightning
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes" access="global" >
<lightning:notificationsLibrary aura:id="notifLib"/>
<lightning:button name="toast" label="Display Toast" onclick="{!c.showToast}"/>
</aura:component>