0

Below given is the simple web service.This web service has been written in apex and I am calling it from Apex as I am working on two salesforce orgs.I am calling getMessage () from my org and passing opportunity name as parameter and method queries salesforce database and then return the fetched record.Uptill now it has worked fine and it works fine with one record.It generated an esception that list has more tham one row for assignement.How do I make it work with multipe records.How do I send more than one record as return value to the calling method.Can somebody please help.

Thanks a lot Daniel,

I have changed my web services a bit and instead of string I am trying to return list of opportunity records to the the calling apex code.However I get an error message,illegal assignment from list to list.Can u please suggests.

This is how I am making callout.

     list<opportunity>q=new list<opportunity>();
     q=apx.GetMessage('Newone')

global class Greatone
{
//set p=new set();

    WebService static list<opportunity> GetMessage(string msg)  
           {  
                 string g;
                 list<opportunity> opp=[select id,name from opportunity where name=:msg];
              return opp;  
          }

}

2
  • Please don't rework your question like that. It makes any existing answers confusing as they no longer apply to what was asked. It is better to ask a new question to address the new issue and leave the original question unchanged. Commented Jul 9, 2013 at 19:53
  • I apologize.Due to the restriction on the number of characters I could not type my question which was in continuation to an existing problem .Any way I will be careful
    – Sales
    Commented Jul 9, 2013 at 20:06

1 Answer 1

5

Modified question answer - "Illegal assignment from list to list" exception

The list of Opportunity being returned by the web service is not compatible with the native Opportunity object (You have a class with the same name as a native sObject). E.g. the fields on the Opportunity in the web service org could be significantly different to those in the calling Org.

In general, I'd avoid creating classes with the same name as a native sObject. You will cause yourself a world of hurt. You could create a dedicated class in the web service for returning the required data or modify the WSDL2Apex generated class to have a different name.

Alternatively, you could try prefixing the web service Opportunity with the class name you created using WSDL2Apex to distinguish it from the native Opportunity sObject. I wouldn't recommend this, as you will probably run into issues elsewhere in determining the correct class.


Answer for the original question.

Have a read of the documentation - Using SOQL Queries That Return One Record.

SOQL queries can be used to assign a single sObject value when the result list contains only one element. When the L-value of an expression is a single sObject type, Apex automatically assigns the single sObject record in the query result list to the L-value. A runtime exception results if zero sObjects or more than one sObject is found in the list.

Your SOQL query is returning more than one Opportunity. You need to either assign the query result to a list or (if you are certain there will only ever be one result) continue assigning to a single object and limit the number of records returned.

E.g.

List<Opportunity> opps = [select id,name from opportunity where name=:msg];
if(opps.size() > 0) {
    g += string.valueof(opp);
}

Or

opp = [select id,name from opportunity where name=:msg LIMIT 1];
3
  • The code clearly shows that they are using List<Opportunity> correctly. Therefore this answer has nothing to do with problem at this location. The problem is somewhere else in their code.
    – sfdcfox
    Commented Jul 9, 2013 at 16:04
  • @sfdcfox It appears the question was modified significantly after I answered it. It is now dealing with a completely different issue. Commented Jul 9, 2013 at 19:50
  • And that'll teach me to check revisions first!
    – sfdcfox
    Commented Jul 9, 2013 at 20:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .