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
abi dzarabi dzar 

How can I expose this code as webservice?

Hi guys,

 

How can I expose this so that it can be called as webservice.

I tried using webservice, but how can I return string in webservice function. 

 

 String keyword = 'something';
        String query = 'FIND \'' + keyword + '\' RETURNING KnowledgeArticleVersion (title, Summary, UrlName WHERE PublishStatus=\'online\')';
        List<List<SObject>> searchList = search.query(query);
        KnowledgeArticleVersion [] matchedArticle = ((List<KnowledgeArticleVersion>)searchList[0]);
        String XMLResult = '<?xml version="1.0" encoding="UTF-8" ?>';
        XMLResult +='<articles>';
        for( integer i =0; i< matchedArticle.size(); i++){
               XMLResult +='<article>';
               XMLResult +='<title>' + matchedArticle[i].Title + '</title>';
               XMLResult +='<summary>' + matchedArticle[i].Summary + '</summary>';
               XMLResult +='<url>' + matchedArticle[i].UrlName + '</url>';
               XMLResult +='</article>';           
        }       
        XMLResult +='</articles>';
      

 

regards,

Abi Dzar

sh-at-youseesh-at-yousee

Hi,

 

Try accessing Force.com Apex Code Developer's Guide and search for "WebService Methods".

 

The following is copy/pasted from the doc:

 

 

global class MyWebService {
    webService static Id makeContact(String lastName, Account a) {
        Contact c = new Contact(lastName = 'Weissman', AccountId = a.Id);
        insert c;
        return c.id;
    }
}

 

 

That shows exactly how you'd go about creating a class/method as a web service. In this case an Id is returned but of course you can just change that to String, if that's what you need.

 

Two main things to take notice of here:

 

  1. The class must be global
  2. The method must have the webservice keyword

Hope it helps.

 

/Søren Nødskov Hansen

abi dzarabi dzar

Hi, Thanks for the reply,

 

I have done that. But, I'm still having the problem of calling it from other site. 

do we need to have @future(callout=true) ?

 

regards,

Abi Dzar

sh-at-youseesh-at-yousee

Hi,

 

The future keyword has nothing to do with exposing a method as a web service. It only defines if the method should be executed asynchronously.

 

It would probably be easier to help you if you would post your code and more importantly the error you get when trying to call the method.

 

I quick guess could be the user you use to login to SF with in your client code doesn't have the necessary privileges to class with the web service method.

 

/Søren Nødskov Hansen