+ Start a Discussion
jpwagnerjpwagner 

button to obtain html blob

Hi,

 

I'm looking to create a button that obtains an html blob from a URL (in the real implementation I'd actually be parsing that html to find some piece of information, but that's not important.)

 

So far I believe this is a pretty straight forward class to get that info:

global class htmlblob {

webservice static String getblob(String someurl) {

PageReference pageRef = new PageReference(someurl);
String htmlblob = pageRef.getContent().toString();
return htmlblob;

}

}

 

and I'm calling that class from the button like this:

 

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js" )}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js" )}
var someurl = '/{!Opportunity.Id}';
var records = {!GETRECORDIDS($ObjectType.Opportunity)};
var result = sforce.apex.execute("htmlblob","getblob",{a:someurl});
var myObj = new sforce.SObject("Opportunity" );
myObj.id = records[0];
myObj.htmlblob__c = result.substring(0,50);
sforce.connection.update([myObj]);
window.parent.location="/"+'{!Opportunity.Id}';

 

Just a reminder that I don't actually care about saving the blob to a field so if that's a problem for some reason, I can parse it first, I just want to validate that I am receiving the correct data back.

 

Any help on this is appreciated!

 

Thanks

Message Edited by jpwagner on 04-08-2009 12:19 PM
Message Edited by jpwagner on 04-08-2009 12:20 PM
Message Edited by jpwagner on 04-08-2009 12:20 PM
Message Edited by jpwagner on 04-08-2009 12:47 PM
Message Edited by jpwagner on 04-08-2009 01:05 PM
Best Answer chosen by Admin (Salesforce Developers) 
jpwagnerjpwagner

Ok figured it all out.

 

Couple of rookie mistakes.

1.  can't use substring (was the issue with initial post)

2.  update function must be webservice method (issue in second post)

 

hope this is a helpful reference.

 

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}
var someurl = '/somepartialurl';

var result = sforce.apex.execute("htmlblob","getblob",{a:someurl});

var oppid = '{!Opportunity.Id}';

sforce.apex.execute("htmlblob","writeblob",{a:result, b:oppid});

window.parent.location="/"+'{!Opportunity.Id}';

All Answers

jpwagnerjpwagner

New piece of info:

If I take out the update, (which I think I screwed up anyway by not including whatever needs to be included to create a new sobject) I can see that it correctly grabs the html:

 

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js" )} 
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js" )}
var someurl = "/"+'{!Opportunity.Id}';

var result = sforce.apex.execute("htmlblob","getblob",{a:someurl}); 

alert(result);
window.parent.location="/"+'{!Opportunity.Id}';

 

If I then write a method to update the Opportunityfield with this result, it fails with a cryptic popup error:

'push' is null or not an object

 

Any thoughts on this?

 

THANKS!

Message Edited by jpwagner on 04-08-2009 01:26 PM
jpwagnerjpwagner

Ok figured it all out.

 

Couple of rookie mistakes.

1.  can't use substring (was the issue with initial post)

2.  update function must be webservice method (issue in second post)

 

hope this is a helpful reference.

 

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}
var someurl = '/somepartialurl';

var result = sforce.apex.execute("htmlblob","getblob",{a:someurl});

var oppid = '{!Opportunity.Id}';

sforce.apex.execute("htmlblob","writeblob",{a:result, b:oppid});

window.parent.location="/"+'{!Opportunity.Id}';

This was selected as the best answer