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
LaceySnrLaceySnr 

Return Complex Type from Webservice?

Hey Guys,

 

I'm trying to work out how I can return an array of a class in a webservice, I know I can do lists / arrays of sobjects and standard types, but if I have a class such as:

 

 

	global class CObject
	{
		string	ObjectName;
		double	Value;
	}

And I try and return a class which includes a list/array of those:

 

 

	global class GetObjectResult
	{
		webservice integer					NumResults;
		webservice boolean					Error;
		webservice string					ErrorMessage;
		webservice list<CObject>			        Objects;
		
		global GetObjectResult()
		{
			NumResults = 0;
			Error = false;
			ErrorMessage = '';
			Objects = new list<CObject>();
		}
	}

 

The web service method being:

 

	webservice static GetObjectResult GetObject(GetObjectRequest req)
	{
		GetObjectResult res = new GetObjectResult();
		
		
		if(req == null)
		{
			res.ErrorMessage = 'Invalid request.';
			res.Error = true;
			return res;
		}
		
		if(req.ObjectName == null)
		{
			res.ErrorMessage = 'ObjectName can not be null.';
			res.Error = true;
			return res;
		}
		
		string strNameMatch = '%' + req.ObjectName + '%';
		
		for(Basic_Object__c sObj : [select	Id, Name__c, Value__c
						from	Basic_Object__c
						where	Name__c like : strNameMatch
						limit	100])
		{
			CObject obj = new CObject();
			obj.ObjectName = sObj.Name__c;
			obj.Value = sObj.Value__c;
			res.Objects.add(obj);
		}

		res.NumResults = res.Objects.size();
		
		return res;
	}

 

 

Then I only get the name of the array / list member variable repeated n times:

 

 

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/schemas/class/IPhoneWebServices">
   <soapenv:Body>
      <GetObjectResponse>
         <result>
            <Error>false</Error>
            <ErrorMessage/>
            <NumResults>3</NumResults>
            <Objects/>
            <Objects/>
            <Objects/>
         </result>
      </GetObjectResponse>
   </soapenv:Body>
</soapenv:Envelope>

 

Anyone know of a way to actually show my fields and values?

 

Cheers :)

 

Best Answer chosen by Admin (Salesforce Developers) 
iBr0theriBr0ther

 

What I can suggest  is to declare as webservice for both properties: 
global class CObject
{
webservice string ObjectName;
webservice double Value;
}

 

What I can suggest  is to declare as webservice for both properties: 

 


global class CObject {

 

        webservice string ObjectName;

         webservice double Value;

 

}

 

 

Cheers,

All Answers

iBr0theriBr0ther

 

What I can suggest  is to declare as webservice for both properties: 
global class CObject
{
webservice string ObjectName;
webservice double Value;
}

 

What I can suggest  is to declare as webservice for both properties: 

 


global class CObject {

 

        webservice string ObjectName;

         webservice double Value;

 

}

 

 

Cheers,

This was selected as the best answer
LaceySnrLaceySnr

I can't believe I didn't think of trying that, thanks for the suggestion - worked perfectly and I can't really see how I thought it'd work without the keyword being there.

 

Time to go give myself a slap :)