Skip to content

Instantly share code, notes, and snippets.

View renatoliveira's full-sized avatar

Renato Oliveira renatoliveira

View GitHub Profile
@renatoliveira
renatoliveira / app.py
Created October 13, 2016 16:08
Simple script to get a XML from a service and translate it to JSON
import requests
from json import dumps
from xmljson import parker as pk
from xml.etree.ElementTree import fromstring
endpoint = 'http://webserviceurlhere/number'
xml = requests.get(endpoint).text
result = dumps(pk.data(fromstring(xml)))
print(result)
global class Telegram {
public static void sendMessage (String message, User user) {
sendMessage(message, user.TelegramId__c);
}
public static void sendMessage (String message, String telegram_id) {
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.telegram.org/bot' + TelegramBot__c.getOrgDefaults().Token__c + '/sendMessage');
req.setMethod('POST');
req.setHeader('content-type', 'application/json');
@renatoliveira
renatoliveira / GoogleDriveCalloutMock.cls
Last active October 2, 2023 16:39
This is a refactored class that uploads files to Google Drive using the service's REST API.
/**
* Callout mock for the Google Drive API class
*
* Please note that the "generateRandomString" was not originally part of this class. I've added this method because there
* were lots of IDs hardcoded into the URL's inside the JSON responses. Those should not, in theory, affect the tests where
* this mock is used.
*/
@IsTest
global class GoogleDriveCalloutMock implements HttpCalloutMock {
global HTTPResponse respond (HTTPRequest req) {
@renatoliveira
renatoliveira / IDGenerator.cls
Created August 2, 2018 22:58
Class that generates a valid Salesforce ID given a object type.
/**
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
@renatoliveira
renatoliveira / IDGeneratorTest.cls
Created August 2, 2018 22:58
Test for IDGenerator.cls
/**
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
@renatoliveira
renatoliveira / getRecordWithAllFields.cls
Created November 14, 2018 15:02
Method in which the platform gets all the fields for an object, and then executes a dynamic query with all of the fields. Retrieves one or more records.
public List<SObject> getObjects (Set<Id> recordIds) {
Id dummyId = (new List<Id>(recordIds))[0];
String objectName = Id.valueOf(dummyId).getSObjectType().getDescribe().getLocalName();
Set<String> fields = Id.valueOf(dummyId).getSObjectType().getDescribe().fields.getMap().keySet();
String query = 'SELECT ';
for (String f : fields) {
query += f + ', ';
}
@renatoliveira
renatoliveira / eraselogs2.py
Created December 27, 2018 16:24
Erase all specified ApexLog records from Salesforce
from simple_salesforce import Salesforce
import argparse
parser = argparse.ArgumentParser(description='Erase logs')
parser.add_argument('-q', '--query', dest='query', help='ApexLog query')
parser.add_argument('-u', '--username', dest='username', help='Salesforce username')
parser.add_argument('-p', '--password', dest='password', help='Salesforce password')
parser.add_argument('-t', '--token', dest='token', help='Salesforce token')
@renatoliveira
renatoliveira / TimezoneRunAsSample.cls
Last active January 29, 2019 10:50
This gist shows that you can use the 'runAs' method to simulate timezone differences between one or more users in a test.
// Considering that 'neutral_user' has 'GMT' as its 'TimeZoneSidKey' field,
// and that the other two users run with 'America/Puerto_Rico' and
// 'America/Sao_Paulo'.
Datetime christmas2018utc;
System.runAs(neutral_user) {
// Creates a datetime instance on December 25th, at 12:00 (GMT).
// Using the 'runAs' method because if we don't, then the date instance will be
// on the same timezone as the test running user.
christmas2018utc = Datetime.newInstance(Date.newInstance(2018, 12, 25), Time.newInstance(12, 0, 0, 0));
public class ComparisonTableController {
@AuraEnabled(cacheable=true)
public static List<PricebookEntry> getProducts (Id pricebookId, Integer countLimit) {
return [
SELECT
Id
,Product2.Name
,Product2.Family
,Product2.ProductCode
@renatoliveira
renatoliveira / AssignPermissions.cls
Last active April 26, 2019 19:25
Assigns a permission "PermissionName" to users from profiles that were specified.
// Permission set's name that I want to assign to all the users
String psName = 'PermissionName';
// Query the permission set
PermissionSet ps = [
SELECT
Id,
Label,
Name
FROM PermissionSet