function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

S-Control To Populate Custom Lead Fields with SFGA information

Here's an S-control I made and presented on at Dreamforce that will populate custom lead fields with the Google Campaign, AdGroup, and AdWord.  The prerequisite for this to work is to have Salesforce for Google Adwords setup and to have the custom lead fields (Google_Campaign__c, AdKeyword__c, Google_AdGroup__c) created.  It also uses a custom lead field called "Last4__c" which is actually a formula field that returns "Yes" if the lead has been created in the last 4 days. I use this to keep the number of leads being updated to a manageable number. I’m posting both to share but also because I bet some of the real talents on this board can improve upon this code.  I’m not a programmer and I owe those on this board a lot of thanks for helping me to learn how to write S-Controls. 

The benefit to this code is that with the SFGA info in custom fields as opposed to tasks you can do all sorts of custom reporting.  I launch this S-Control from a custom homepage component daily in order to populate the custom lead fields before I start the lead conversion process.

Thanks to all on this board who give their time and advice, it is most appreciated.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">

<script src="" type="text/javascript"></script>
<script language="JavaScript">

function initPage()
sforceClient.init("{!API.Session_ID}", "{!API.Partner_Server_URL_70}", true);

function updateObjects()
var queryResult3 = sforceClient.query("Select ID, Google_Campaign__c, AdKeyword__c, Google_AdGroup__c From Lead where Last4__c = 'Yes' and LeadSource = 'Google AdWords'")

var updateObjects = new Array();

for (var i=0;i<queryResult3.size;i++)
var lhaccount2 = queryResult3.records[i];
var ids=lhaccount2.get("Id");

var queryResultP = sforceClient.query("Select ID, Name From SFGA__Google_Campaign__c where SFGA__Lead__c = '"+ids+"'")
var queryResultK = sforceClient.query("Select ID, Name From SFGA__Keyword__c where SFGA__Lead__c = '"+ids+"'")
var queryResultA = sforceClient.query("Select ID, Name From SFGA__Ad_Group__c where SFGA__Lead__c = '"+ids+"'")

var lhaccount3 = queryResultP.records[0];
var Name=lhaccount3.get("Name");

var lhaccount4 = queryResultK.records[0];
var Keyword=lhaccount4.get("Name");

var lhaccount5 = queryResultA.records[0];
var Group=lhaccount5.get("Name");

lhaccount2.set("Google_Campaign__c", Name);
lhaccount2.set("AdKeyword__c", Keyword);
lhaccount2.set("Google_AdGroup__c", Group);



var saveResults = sforceClient.Update(updateObjects)[0];
<body onload="initPage()">

Awesome!!!  Thanks!!!