Salesforce - Developer - Misc

salesforce-developer

When looking at salesforce IDs the first 3 characters mean something. Campaigns 
will always begin with 701, just like contacts are 003. Broadcasts will be a**. Those 
star values will differ from client to client as the broadcast object is custom, so a00 
is the first custom object, a01 the second, etc.

To this end, it’s important to note that most things can be done with clicks, 
and the golden rule is to always try to build with clicks before going to code, 
as it is more sustainable and maintainable moving forward. Code is meant to 
give you more granular control of customization when clicks aren't enough.

With Advanced Setup Search, you can search across multiple areas, using the 
same search box. Here’s how.  Let’s say you want to find a specific user. 
Enter part or all of the user’s name, and press Enter.

Setup Search looks for your keywords across users, objects, fields, and more. 
In the example below, we’ve located Email Templates, Workflow Rules, Field 
Updates, and Approval Processes.

var async = require('async');
var express = require('express');
var util = require('util');
var http = require('https');

// Creates an express web server
var app = express.createServer(
  express.logger(),
  express.static(__dirname + '/public'),
  express.bodyParser(),
  express.cookieParser(),
  // set this to a secret value to encrypt session cookie
  express.session({ secret: process.env.SESSION_SECRET || 'secret123'}),
  require('faceplate').middleware({
    app_id: process.env.FACEBOOK_APP_ID,
    secret: process.env.FACEBOOK_SECRET,
    scope: 'email'
  })
);

var api = process.env.API || '25.0';
var oauth;
var nforce = require('nforce');

// Login to Force.com using the OAuth username/password flow
var sfdcOrg = nforce.createConnection({
  clientId: process.env.FORCE_DOT_COM_CONSUMER_KEY,
  clientSecret: process.env.FORCE_DOT_COM_CONSUMER_SECRET,
  redirectUri: 'http://localhost',
  apiVersion: api, // optional, default to v24.0
  environment: 'production' // optional, sandbox or production
});

function sfdcAuthenticate(callback) {
  sfdcOrg.authenticate({
    username: process.env.FORCE_DOT_COM_USERNAME,
    password: process.env.FORCE_DOT_COM_PASSWORD
  }, function(err, resp) {
    if (err) {
      console.log('Error: ' + err.message);
    } else {
      console.log('Access Token: ' + resp.access_token);
      oauth = resp;
    }
    if (callback) {
      callback();
    }
  });
}

sfdcAuthenticate();

app.post('/lead', function(request, response) {
  request.body.leadRec.Phone = request.body.Phone;
  // Create the Lead record in Salesforce
  createLead(request.body.leadRec, request, response);
  // addLead2Queue(request.body.leadRec, request, response);
});

function createLead(LeadRec, request, response) {
  var obj = nforce.createSObject('Lead', leadRec);
  sfdcOrg.insert(obj, oauth, function(err, resp) {
    if (err) {
      console.log(err);
      if (err.statusCode == 401) {
        console.log('Logging in again...');
        sfdcAuthenticate(createLead(leadRec, request, response));
      } else {
        response.send(err.message, 400);
      }
    } else {
      if (resp.success == true) {
      }
    }
  });
}

redis.llen('leads', function(err, total) {
  console.log('Length is:' + total);
  var leads = new Array();
  for (var i = 0; i < total; i++) {
    redis.lpop('leads', function(err, x) {
      leads.push(JSON.parse(x));
      if (leads.length == total) {
        insertLeads(leads);
      }
    }
  }
}

Standard REST API does not support bulk insert.  However, we can create a
a custom Apex REST endpoint to do bulk insert.

@RestResource(urlMapping='/bulk_lead_insert/*')
global class BulkLeadInsertSvc {
  @HttpPost
  global static void insertLeads (List<Lead> leads) {
    Database.DMLOptions dlo = new Database.DMLOptions();
    dlo.optAllOrNone = true;
    Database.insert(leads, dlo);
  }
}

function insertLeads(LeadRecords) {
  var data = '';
  var host = (require('url').parse(oauth.instance_url))['host'];
  console.log('Host:' + host);
  var options = {
    host: host,
    path: '/services/apexrest/bulk_lead_insert/',
    method: 'POST',
    headers: {
      'Host': host,
      'Authorization': 'OAuth' + oauth.access_token,
      'Accept': 'application/jsonrequest',
      'Cache-Control': 'no-cache,no-store,must-revalidate',
      'Content-Type': 'application/json'
    }
  }
}

Heroku Pusher does realtime push using WebSocket.  On the Salesforce side, we 
have written a trigger that call Heroku Pusher, and then Pusher takes care of
the rest.

How can we determine Sandbox from production?

SELECT IsSandbox FROM Organization LIMIT 1

How can we determine the namespace prefix of an Apex class?

select NamespacePrefix from ApexClass where Name = 'BroadcastService' LIMIT 1
public static String namespace()
{
    ApexClass ac = [SELECT NameSpacePrefix 
                    FROM ApexClass
                    WHERE Name = 'MyClassName'];
    return ac.NameSpacePrefix;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License