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
PXForcePXForce 

cannot access data from Apex web services

I have an apex web service in an apex class that is called from the flex front end . The Flex is embedded in a VF page as a flash object <apex:flash> and I am logging into salesforce.com using the session Id (grabbing it from the controller using UserInfo.getSessionId()) and session URL = $Api.Partner_Server_URL_90.

 

I am developing in my sandbox.

 

The code for the apex class holding the webservice is as follows.

 

global class MyWebService 
{
	private static Datetime startDate {get; set;}
	private static Datetime endDate {get; set;}
	
	
	global class LeadObject
	{
		public String name {get; set;}
		public String Id {get; set;}
		public String country {get; set;}
		public String city {get; set;}
		public String status {get; set;}
		public String annual_Rev_Range {get; set;}
		public String score {get; set;}
		public String company {get; set;}
		
		public LeadObject(Lead lead)
		{
			name = lead.Name;
			Id = lead.Id;
			country = lead.Country;
			city = lead.City;
			status = lead.Status;
			annual_Rev_Range = lead.Annual_Revenue_Range__c;
			company = lead.Company;
			score = lead.Score__c.format();
		} 
	}
	
	Webservice static List<LeadObject> getLeads(String startDateString , String startMonthString , String startYearString,
															String endDateString , String endMonthString , String endYearString)
	{
		List<LeadObject> result = new List<LeadObject>();
		
		//set the start and the end dates
		setDates(startDateString , startMonthString , startYearString,
				endDateString , endMonthString , endYearString);
		
		//get Leads Data
		List<Lead_Key_Event__c> leadKEs;
		leadKEs = new List<Lead_Key_Event__c>();
		
		List<String> leadIds = getLeadIds(leadKEs); 
		
		result = new List<LeadObject>();
		
		For(Lead l : [Select l.Annual_Revenue_Range__c, l.City, l.Company, l.Country, l.FirstName,l.Name, l.Id, l.LastName, l.Score__c, l.Status 
					  from Lead l where l.Id IN : leadIds])
		{
			result.add(new LeadObject(l));
		}
		
		return result;
	}
	
	private static List<String> getLeadIds(List<Lead_Key_Event__c> leadKeyEvents)
	{
		List<String> uniqueIds = new List<String>();
		
		System.debug('Start date : ' + startDate.date().format() + ' End Date : ' + endDate.date().format());
		
		
		Map<String,Lead_Key_Event__c> uniqueLeadsMap = new Map<String , Lead_Key_Event__c>();
		
		For(Lead_Key_Event__c obj : [Select l.Id, l.Lead__c, l.Lead__r.Name , l.Name, l.Lead_Created_Date__c, l.Score__c from Lead_Key_Event__c l
					    			Where l.Lead__c != null And l.Lead_Created_Date__c >= : startDate And l.Lead_Created_Date__c <= : endDate])
	    {
	    	leadKeyEvents.add(obj);
	    	
	    	if(uniqueLeadsMap.get(obj.Lead__c) == null)
	    	{
	    		uniqueLeadsMap.put(obj.Lead__c, obj);
	    		uniqueIds.add(obj.Lead__c);
	    	}
	    }
	    
	    return uniqueIds;
	}
	
	private static void setDates(String startDateString , String startMonthString , String startYearString,
							String endDateString , String endMonthString , String endYearString)
	{
		startDate = Datetime.newInstance(Integer.valueOf(startYearString),Integer.valueOf(startMonthString),Integer.valueOf(startDateString));
		endDate = DateTime.newInstance(Integer.valueOf(endYearString),Integer.valueOf(endMonthString),Integer.valueOf(endDateString));
	}
}

 The code for the flex front end thats calling the webservice menthod getLeads() is as follows:

 

 

package Controller
{
	import Model.Model;
	
	import com.salesforce.AsyncResponder;
	import com.salesforce.Connection;
	import com.salesforce.objects.Parameter;
	import com.salesforce.results.Fault;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;

	public class WebServiceClient
	{
		public var results:ArrayCollection;
		
		public var binding:Connection;
		
		public function WebServiceClient(sfdcConnection:Connection)
		{
			this.binding = sfdcConnection;
			
			if (!this.binding.IsLoggedIn)
				throw new Error("Connection to the server is not available.");	
		}
		
		public function execute(startDate:Date , endDate:Date): void
		{
			Alert.show("executing web service client");
			
			//preparing the parameters of the web service method
			var params : Array = new Array(6);
			
			var stDateStringParam:Parameter = new Parameter("startDateString",startDate.date.toString());
			params[0] = stDateStringParam;
			//params.push(stDateStringParam);
			var stMonthStringParam:Parameter = new Parameter("startMonthString",startDate.month.toString());
			params[1] = stMonthStringParam;
			//params.push(stMonthStringParam);
			var stYearStringParam:Parameter = new Parameter("startYearString",startDate.fullYear.toString());
			params[2] = stYearStringParam;
			//params.push(stYearStringParam);
			var endDateStringParam:Parameter = new Parameter("endDateString",endDate.date.toString());
			params[3] = endDateStringParam;
			//params.push(endDateStringParam);
			var endMonthStringParam:Parameter = new Parameter("endMonthString",endDate.month.toString());
			params[4] = endMonthStringParam;
			//params.push(endMonthStringParam);
			var endYearStringParam:Parameter = new Parameter("endYearString",endDate.fullYear.toString());
			params[5] = endYearStringParam;
			//params.push(endYearStringParam);
			
			getLeads(params);
			
		}
		
		private function getLeads(params:Array):void
		{
			try
			{
			// using SFDC Aysync Responder to get the results in flex
			var tempCallBack: AsyncResponder = new AsyncResponder(
				function(result:Object):void 
				{
					try
					{
						if (result != null)
						{
							var reportsDataController:ReportsDataController = new ReportsDataController();
							reportsDataController.setLeadsData(result);
						}
						else
							Alert.show("Leads are empty.", "Info");
					}
					catch(err:Error)
					{
						Alert.show("error in result handler : " + err.message);
					}
				},
				function(result: Fault):void { Alert.show("Operation failed", "Error");  }
			);
			
			// call the execute method of the SFDC connection object to reach out the web service
			Alert.show("Invokin SOAP");
			binding.execute("MyWebService", "getLeads", params, tempCallBack);
			}
			catch(err:Error)
			{
				Alert.show("error in getLeads : " + err.message);	
			}
		}

	}
}

if I look in the Debug logs , I know i am invoking the webservice and querying for the data , my problem is that the data is not being passed back to flex . i.e. the result object in my asyncresponder is coming back as null.

 

Any help would be greatly appreciated.

 

Thanks 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
mulvelingmulveling

IIRC the properties in your LeadObject class must be declared with the "Webservice" modifier, if you wish them to be returned from a Webservice call.

All Answers

mulvelingmulveling

IIRC the properties in your LeadObject class must be declared with the "Webservice" modifier, if you wish them to be returned from a Webservice call.

This was selected as the best answer
PXForcePXForce

Thanks , that worked.

d.tejdeep@nicomatic.ind.tejdeep@nicomatic.in
Hi i am newly writing webservice can you post the correct answer