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
CaptaConsultingCaptaConsulting 

AJAX sforceclient.js beta

Hi,

i am trying to adds leads to a campaign with the AJAX toolkit beta1 and the CampaignMember object, but it fails.

A snippet of code:

..........

sforceClient.setBatchSize(10);
_qrLeads = sforceClient.Query("Select Id, LastName, FirstName, State From Lead where State = '"+provincia+"'");

if (_qrLeads.size > 0) {
for (var i=0;i var lead = _qrLeads.records[i];
var campMember = new DynaBean("CampaignMember");
campMember.set("CampaignId","{!Campaign_ID}");
campMember.set("LeadId",lead.id);
campMember.set("Status","Sent");
var saveResult = sforceClient.Create(campMember)[0];
}
}

.......

Any Help??

Thanks.

Victor Alvarez Fuente
benjasikbenjasik
What error do you get?
CaptaConsultingCaptaConsulting

Hi,

I am going to explain the test more.

------------

I have created a campaign manually in salesforce.com

The campaign is "Active" and with no Leads or Contacts.

------------

Now i want to add some Leads via the AJAX Toolkit to this campaign.

But when i click in the Custom Link, i don´t have any execution errors but when i reloaded the campaign page there´s no Leads associated to the campaign.

If i add some new javascript lines with alerts to inspect the "saveResult" value,

..........

sforceClient.setBatchSize(10);
_qrLeads = sforceClient.Query("Select Id, LastName, FirstName, State From Lead where State = '"+provincia+"'");

if (_qrLeads.size > 0) {
for (var i=0;i<_qrLeads.records.length;i++) {
    var lead = _qrLeads.records[i];
    var campMember = new DynaBean("CampaignMember");
    campMember.set("CampaignId","{!Campaign_ID}");
    campMember.set("LeadId",lead.id);
    campMember.set("Status","Sent");
    var saveResult = sforceClient.Create(campMember)[0];
    if (saveResult == null) alert("ERROR")                                                       //NEW LINE ADDED
    else alert(saveResult);                                                                                 //NEW LINE ADDED

}
}

The return alert  is always "ERROR", "ERROR", "ERROR", ...........

Thanks.

Victor Alvarez Fuente

 

sommerscmsommerscm

Did you ever get this resolved?  I'm trying to insert one row with

var saveResult = sforceClient.Create(newCase, createResult(), true);

my alerts keep telling me that it's "undefined"

Not sure if this is related to your issues, but would love to know if you encountered this, or if you get your own issue resolved.  thanks!

adamgadamg
This isn't going to answer your question, but may help generally:

- I find its best to develop the scontrol locally as a HTML file (logging in via the AJAX toolkit) rather than as a "live" scontrol - easier to edit/update the code that way

- If you use firefox, there is a great & free javascript debugger available here - http://extensionroom.mozdev.org/more-info/venkman

Adam
DevAngelDevAngel

Hi sommerscm,

The problem with your code is the call back.  It needs to just be the name of the function.

var saveResult = sforceClient.Create(newCase, createResult, true);  NO PARAMS on createResult.

DevAngelDevAngel

Hi CaptConsulting,

Looks like it should be ok.  Try this code.  This does error handling and will also give you the results of the Create call (which you should batch as demonstrated here).

	var cmBatch = new Array();
	for (var j=0;j<_qrLeads.records.length;j++) {
		var one_lead = _qrLeads[j];
		
		var cm = new DynaBean("CampaignMember");
		cm.set("CampaignId", "{!Campaign_ID}");
		cm.set("LeadId", one_lead.get("Id"));
		cm.set("Status", "Sent");
		cmBatch.push(cm);
	}
	var sr = sforceClient.Create(cmBatch);
	if (dltypeof(sr) == "array") {
		for (var i=0;i<sr.length;i++) {
			if (sr[i].success == true) {
				alert("Member created...");
			} else {
				alert("Member creation failed: " + sr[i].errors[0].message);
			}
		}
	} else {
		alert("Soap fault: " + sr.toString());
	}
 
DevAngelDevAngel

Also, it is worth mentioning that error handling is done a little differently in the AJAX toolkit than elsewhere.  Every call will return an object if the web service is contacted and responds.  The object that is returned is either what you would expect assuming there are no data errors (an array of SaveResults, a QueryResult, a SetPasswordResult etc).  If the server returns a fault, which is different from a javascript error, the object returned will be a SoapFault object.  This means that you should always check the type of the object returned. 

There are two ways to do this. 

First, each object contains a getClassName() method that will return the name of the object.

var r = sforceClient.MethodCall(p1, p2);

if (r.getClassName() == "MethodCallResult") { alert("got what I thought I should."); }

else { alert("Got a soap fault");}

The second way is to use dltypeof.  This is appropriate for the calls that return arrays of objects like the Create call.  Because a SoapFault object is not an array you could do this:

var r = sforceClient.Create([lead]);

if (dltypeof(r) != "array") { alert("got what I thought I should."); }

else {alert("Got a soap fault.");}

For the saveResult objects returned from the create, update and delete calls you should always inspect each element for the success flag to determine if the operation was successful.  Doing this gives you the ability to also determine the cause for failure by inspecting the errors collection which contains any information about why the service was unable to successfully complete the request.

One other thing to note.  Most if not all the Toolkit objects have a toString() method.  This is useful in the SoapFault instance because you can let the user know what the problem is by

var r = sforceClient.Create([lead]);

if (dltypeof(r) != "array") { alert("got what I thought I should."); }

else {alert("Got a soap fault.\nThe error was: " + r.toString());}

 

More documentation and examples will be coming in the next few weeks.

 

Cheers

KraulinKraulin
What's involved in setting up our environment so that we can develop ajax scontrols locally?
Ron HessRon Hess

to set up a page to work "localy" is basicaly getting the login done without coming from an scontrol, so you would need to pop a login box ( or hard code a login / passwd) , process the login , then jump into your "normal" code.

here is an example, not tested:

function initPage() {

 sforceClient.init("{!API_Session_ID}", "{!API_Partner_Server_URL_60}");
 //Check to see if we are standalone (local) or hosted in scontrol
 if (sforceClient.getSessionId().indexOf("API_Session_ID") != -1) {

// do a local login

// sforceClient.login ( ... );

 }

//Continue with our initialization
  pageSetup();
   

so if you are "local" then you would fall into the if statement, do a login, then continue with the whole thing running outside salesforce.  very handy for debugging.

this was taken from the helloworld2.htm example in the ajax toolkit beta download

Message Edited by Ron Hess on 09-20-2005 10:12 AM