Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
$.getScript("https://mandrillapp.com/api/docs/js/mandrill.js")
.done(function(script, textStatus) {
console.log(textStatus);
})
.fail(function(jqxhr, settings, exception) {
console.log("Triggered ajaxError handler.");
});
// Create a function to log the response from the Mandrill API
function log(obj) {
console.log(JSON.stringify(obj));
}
//This is the config for the entire script.
//If you need to add a date field you must have the case sensitive word "Date"
var template = {
jobSheet: {
timestampField:"_fid_342",
fields: {
relatedProducerIDContactNickname: "113",
productionMgrNickname: "111",
generatedJobID: "207",
job: "6",
productionCompany: "10",
producer: "13",
productionMgrName: "110",
jobStatus: "61",
startDate: "16",
endDate: "51",
days: "52",
jobType: "58",
dopName: "125",
recciePreNotes: "99",
gaffer: "29",
bestBoy3: "75",
thirdLX: "77",
crewMember1Phone: "213",
crewMember2Phone: "216",
crewMember3Phone: "224",
crewMember1Email: "214",
crewMember2Email: "217",
crewMember3Email: "225",
crewMember1Diet: "211",
crewMember2Diet: "215",
crewMember3Diet: "223",
crewMember1AgentAcronym: "195",
crewMember2AgentAcronym: "201",
crewMember3AgentAcronym: "234",
primaryVehicleBookedAs: "11",
secondaryVehicleBookedAs: "210",
gearRequests: "78",
van: "25",
van2: "67",
primaryVehicleMake: "173",
secondaryVehicleMake: "177",
primaryVehicleModel: "174",
secondaryVehicleModel: "179",
primaryVehicleLength: "168",
secondaryVehicleLength: "182",
primaryVehicleLengthWithTailgate: "172",
secondaryVehicleLengthWithTailgate: "181",
primaryVehicleHeight: "170",
secondaryVehicleHeight: "180",
primaryVehicleWeight: "169",
secondaryVehicleWidth: "183",
primaryVehicleWidth: "169",
primaryVehicleImageCode: "241",
secondaryVehicleImageCode: "242",
producerEmail: "115",
productionMgrEmail: "112",
laborNotes: "34",
jobSheetBestBoyDisplayFormula: "247",
jobSheet3rdLXDisplayFormula: "248",
jobSheetSecondaryVanDisplayFormula: "246",
jobSheetBestBoyDisplayTRFormula: "250",
jobSheet3rdLXDisplayTRFormula: "249",
gearFormula: "231",
quoteVariableTimeline: "243",
quoteVariable_BeingPrepared: "258",
bookingTicketMailTo: "256",
quoteVariable_AwaitingApproval: "259",
quoteSentDate: "261",
quoteNumber: "35",
rate: "12",
quoteVariable_Accepted: "260"
},
msgObj: {
"template_name": "job",
"template_content": [{
"name": "Job Sheet",
"content": "Job Sheet Information"
}],
"message": {
"from_email": "FROM_EMAIL",
"to": "",
"subject": "Job sheet for ",
"text": "Find your job sheet information below.",
"merge": true,
"global_merge_vars": [],
"merge_vars": ""
}
}
},
bookingTicket: {
timestampField:"_fid_341",
fields: {
relatedProducerIDContactNickname: "113",
productionMgrNickname: "111",
generatedJobID: "207",
job: "6",
productionCompany: "10",
producer: "13",
productionMgrName: "110",
jobStatus: "61",
startDate: "16",
endDate: "51",
days: "52",
jobType: "58",
dopName: "125",
recciePreNotes: "99",
gaffer: "29",
bestBoy3: "75",
thirdLX: "77",
crewMember1Phone: "213",
crewMember2Phone: "216",
crewMember3Phone: "224",
crewMember1Email: "214",
crewMember2Email: "217",
crewMember3Email: "225",
primaryVehicleBookedAs: "11",
secondaryVehicleBookedAs: "210",
gearRequests: "78",
van: "25",
van2: "67",
primaryVehicleMake: "173",
secondaryVehicleMake: "177",
primaryVehicleModel: "174",
secondaryVehicleModel: "179",
primaryVehicleLength: "168",
secondaryVehicleLength: "182",
primaryVehicleLengthWithTailgate: "172",
secondaryVehicleLengthWithTailgate: "181",
primaryVehicleHeight: "170",
secondaryVehicleHeight: "180",
primaryVehicleWeight: "169",
secondaryVehicleWidth: "183",
primaryVehicleWidth: "169",
producerEmail: "115",
productionMgrEmail: "112",
laborNotes: "34"
},
msgObj: {
"template_name": "bookingticket",
"template_content": [{
"name": "Booking Ticket",
"content": "Booking Ticket Information"
}],
"message": {
"from_email": "FROM_EMAIL",
"to": "",
"subject": "Booking ticket for ",
"text": "Find your booking ticket information below.",
"merge": true,
"global_merge_vars": [],
"merge_vars": ""
}
}
}
}
//QUICKBASE API LOAD RECORD DATA
var qbtoken = "XXXXXX",
jobsDBID = "XXXXXX",
querystring = document.location.search,
xml = $.get(jobsDBID, {
act: "API_GetRecordInfo",
rid: kRid,
apptoken: qbtoken
}),
//populate an object with all the field values from the quickbase record
record = {};
//Run it all! This object creates a collection of functions as a way to chain them together.
var run = { // every method returns obj---------
getRecord: function(fields) {
getRecord(fields);
return run;
},
recordToMSG: function(record, msg) {
recordToMSG(record, msg);
return run;
},
sendMail: function(msg,timestampFID) {
sendMail(msg, timestampFID);
return run;
}
};
//This just runs the run object given the template name to use. e.g mail(template.jobsheet)
var mail = function(templateName) {
return run.getRecord(templateName.fields).recordToMSG(record, templateName.msgObj).sendMail(templateName.msgObj, templateName.timestampField)
}
var getRecord = function(fields) {
$.each(fields, function(f, fid) {
if (f.indexOf("Date") !== -1) {
var date = new Date(Number($("field fid", xml.responseXML).filter(function() {
return $(this).text() === fid
}).parent().find("value").text()));
record[f] = date.toLocaleDateString();
} else {
record[f] = $("field fid", xml.responseXML).filter(function() {
return $(this).text() === fid
}).parent().find("value").text();
}
});
return record;
}
var recordToMSG = function(record, msgObj) {
var fieldNames = Object.keys(record);
for (var i = fieldNames.length - 1; i >= 0; i--) {
msgObj.message.global_merge_vars.push({
name: fieldNames[i],
content: record[fieldNames[i]]
})
msgObj.message["to"] = [{
"email": record["producerEmail"],
"name": record["relatedProducerIDContactNickname"]
},
{
"email": record["productionMgrEmail"],
"name": record["productionMgrNickname"]
}
];
msgObj.message["merge_vars"] = [{
"rcpt": record["producerEmail"],
"vars": [{
"name": "fname",
"content": record["relatedProducerIDContactNickname"]
}]
},
{
"rcpt": record["productionMgrEmail"],
"vars": [{
"name": "fname",
"content": record["productionMgrNickname"]
}]
}
];
msgObj.message["subject"] = record["job"] + " - " + record["startDate"];
}
return msgObj;
}
var sendMail = function(msg, timestampFID) {
// MANDRALL API MAIL
// create a new instance of the Mandrill class with your API key
var m = new mandrill.Mandrill('XXXXXX');
m.messages.sendTemplate(msg, function(res) {
log(res);
//Check view mode. If Edit mode save the record after sending the email.
if (/a=er/.test(querystring)) {
var run = {
addTimeStamp: function() {
timestampFID.value = new Date();
return run;
},
save: function() {
DoSave();
return run;
}
}
run.addTimeStamp().save();
} else {
var params = {
act: "API_EditRecord",
rid: kRid
}
params[timestampFID] = new Date();
$.post(jobsDBID,params)
}
alert("Sending Email! Status: " + res[0]["status"])
}, function(err) {
log(err);
})
return msg
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.