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
RKRK 

AJAX Code in S-Control...Need a nudge

Hey all,
As I've mentioned previously in these boards, I have next to no programming knowledge.  I'm not given the time or opportunity to do much actual study, so I try to learn by reverse engineering.

At DF this year, I attended the SForce For Dummies (very fitting for me :) ).  During that class, I was able to get my hands on a piece of AJAX code that will update all the addresses on contacts under an account.

Although this specfic data (address) is not applicable in my org, there are many data elements on an account that I would love to have update down to the contact level.

Attached below is the code that was provided in the PPT from the demo.  I've tried tweaking, adding (I know that I need to call out the toolkit at the start of the code), subtracting, changing but can not get it to work.

Anyone have any quick suggestions?  I know I'm missing something, but I'm not sure if I'm missing something big or something small.

Any guidance would be much appreciated by an ignorant programmer.
Thanks much,
RK


var queryResult =sforceClient.Query("Select BillingCity, BillingCountry, BillingPostalCode, BillingState, BillingStreet from Account Where Id = '" + accId + "'");
    
    var BillingCity = "";
    var BillingCountry = "";
    var BillingPostalCode = "";
    var BillingState = "";
    var BillingStreet = "";
    //move results into readable format   
    var address = queryResult.records[0];
   // assign address attribs to variables
    BillingCity = address.billingcity;
    BillingCountry = address.billingcountry;
    BillingPostalCode = address.billingpostalcode;
    BillingState = address.billingstate;
    BillingStreet = address.billingstreet;

//query to get contacts associated with account
    var queryResult2 =sforceClient.Query("Select Id,LastName,MailingCity, MailingCountry, MailingPostalCode, MailingState, MailingStreet from Contact Where AccountId = '" + accId + "'");
//new array to hold results of updates
    var updateObjects = new Array();

// go through list of contacts to update addresses
    for (var i=0;i<queryResult2.records.length;i++){
        var contact = queryResult2.records[i];

        contact.set("MailingCity",BillingCity);
        contact.set("MailingCountry",BillingCountry);       
        contact.set("MailingPostalCode",BillingPostalCode);
        contact.set("MailingState",BillingState);   
        contact.set("MailingStreet",BillingStreet);
// have to push updated record into new array for update process
        updateObjects.push(contact);
        }
// submit new array with updated contacts
    var saveResults = sforceClient.Update(updateObjects);
gsickalgsickal
One thing you need to change are the statements that get your account info (use get method and javascript is case sensitive for the variable names). as in:
 
var accId = "xxxxxxxxxx";
var queryResult =sforceClient.Query("Select BillingCity, BillingCountry, BillingPostalCode, BillingState, BillingStreet from Account Where Id = '" + accId + "'");
 
//move results into readable format
var address = queryResult.records[0];
 
// assign address attribs to variables
var BillingCity = address.get("BillingCity");
var BillingCountry = address.get("BillingCountry");
var BillingPostalCode = address.get("BillingPostalCode");
var BillingState = address.get("BillingState");
var BillingStreet = address.get("BillingStreet");
 
//Also, to iterate through the contacts associated with the account, use the size property of the result instead of "records.length" and to update the contacts, you should use a DynaBean construct, as in:
 
//query to get contacts associated with account
var queryResult2 =sforceClient.Query("Select Id,LastName,MailingCity, MailingCountry, MailingPostalCode, MailingState, MailingStreet from Contact Where AccountId = '" + accId + "'");
 
//new array to hold results of updates
var updateObjects = new Array();
 
// go through list of contacts to update addresses
for (var i=0;i<queryResult2.size;i++){
var contact = queryResult2.records[i];
contact.set("MailingCity",BillingCity);
contact.set("MailingCountry",BillingCountry);
contact.set("MailingPostalCode",BillingPostalCode);
contact.set("MailingState",BillingState);
contact.set("MailingStreet",BillingStreet);
// push updated record into new array for update process
updateObjects.push(contact);
}
// submit new array with updated contacts
var saveResults = sforceClient.Update(updateObjects)[0];