+ Start a Discussion

updating custom object with returned value

  I have a custom object Custom__c which has custom field Test__c. I have a custom button which calls(onclick javascript) a .net webservice returning some value that I want to update the Test__c with.
 Custom__c.Test__c = value didn't work. It says Custom__c is not defined.
I think I need to create a current instance of Custom__C class. But I don't have id for it. Can I create it on Name(field)? and how?
How should I go for it?

Message Edited by NewToSF on 04-15-2008 02:39 PM
The javascript should be able to glean the ID via {!Custom__c.Test__c} if it's being called form a detail page from that object. 

Here's an example of both that in action, and also how I tie that into Apex code to do some other stuff.

This revolved around a combination of creating my own WebService in Apex, and then modifying my javascript to pass that variable to it.

Here's the Apex, note the bolded lines.  ":sig" refers to the variable I'm passing via javascript.

WebService static string reportFalseNegative(string sig){
//set up our request object
httprequest req = new httprequest();

//and paramenters
List<global__c> apikeyrequest = [select Value__c from Global__c where Name = 'defensiokey' limit 1];
string apikey = apikeyrequest[0].Value__c;
List<global__c> ownerurlrequest = [select Value__c from Global__c where Name = 'defensiourl' limit 1];
string ownerurl = ownerurlrequest[0].Value__c;

string apiurl = 'http://api.defensio.com/app/1.1/';
string action = 'report-false-negatives/';
string responsetype = 'xml';
string body = 'owner-url=' + ownerurl +'&signatures=' + sig;
string endpoint = apiurl + action + apikey + '.' + responsetype;

//do the request
Map<string, string> headers = new Map<string, string>();
headers.put('content-length', '0');
req.setHeader('content-length', headers.get('content-length'));
http http = new http();
httpresponse auditCommentResponse = http.send(req);
string response = auditCommentResponse.getBody();
system.debug('\n\n---Response: ' + response + '---\n\n');

//now we set the defensio__c object to trained = true
defensio__c[] d;
d = [select id, trained__c from defensio__c where name = :sig limit 1];
d[0].trained__c = true;

update d;
return response;

And here's the javascript.  I call this via an OnClick JS via a button on the detail page of this object.

var signature = "{!Defensio__c.Name}";
var result = sforce.apex.execute(
{sig : signature});
alert("result: " + result);