+ Start a Discussion
AK2017AK2017 

How to create dataset/datatable and send it to a webservice using Apex.

Hello All,

How to create dataset/datatable and send it to a webservice using Apex? One of our service provider has opened its API and they have given a service URL and sample code. In the sample code they are creating datatable, column and adding values as dataset and calling a method in the service URL. i am not sure how to do this? is this even possible in Salesforce.

Below is the sample c# code provided.
 
Function InitialCallToGetQuestion()
        {
            /* column names: id, name, value -  for table to associate candidate to transaction */
            DataTable candidateInfo = new DataTable();
            candidateInfo.Columns.Add("id", typeof(int));
            candidateInfo.Columns.Add("name", typeof(string));
            candidateInfo.Columns.Add("value", typeof(string));

            /* can be anything but should identify candidate */
            /* must be at least one entry */
            candidateInfo.Rows.Add(1, "SalesForceID", "12@3934X2");
            candidateInfo.Rows.Add(2, "Name", "David Smith");
            candidateInfo.Rows.Add(3, "CallRep", "Lucy Jones");

            DataSet packageInfo = new DataSet();
            packageInfo.Tables.Add(candidateInfo);

            string clientIdentifier = "E764FCA0";  /* 
            string optionalClientTag = "Region1";  /* can be blank */
            PeerlessService.QuestionSet qs;
            using (PService.PServiceClient ps = new PService.PServiceClient())
            {
                qs = ps.GetQuestion(clientIdentifier, optionalClientTag, packageInfo);
            }

	/*   break the return table into viewable question/response combinations */
            var model = new testModel();
            model.transId = qs.transID;
            model.questPrompt = qs.quesPrompt;
            model.question = new List<testQuestion>();
            for (int i = 0; i < qs.questions.Length; i++)
            {
                var q = new testQuestion();
                q.questKey = qs.questions[i].questNbr;
                q.questTxt = qs.questions[i].questTxt;
                model.question.Add(q);
            }
            model.response = new List<testResponse>();
            for (int i = 0; i < qs.responses.Length; i++)
            {
                var r = new testResponse();
                r.respKey = qs.responses[i].respNbr;
                r.respTxt = qs.responses[i].respTxt;
                model.response.Add(r);
            }
            return View(model);
        }