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
Ean9Ean9 

Web service callout from salesforce to .net using SOAP

When i update a picklist value from 'Requested' to 'Approved' then it should invoke a trigger which in turn calls an external web service (.net web application) and pass those data of the updated record along with it and those records should be received by the .net application and saved on sql server database. I am totally new to Salesforce and Web service.

Trigger,
trigger AdoDetailTrigger on PCS_Detail__c (after update)
{
List<PCS_Detail__c> pcsDetail = [Select Id,Tracking_Details__c From PCS_Detail__c Where Id IN :Trigger.new]; if(pcsDetail[0].Tracking_Details__c == 'Approved')
{
Set<Id> resultIds = (new Map<Id,SObject>(pcsDetail)).keySet();
InvokeWebServiceOnStatusChange.callWebService(resultIds);
}
}

Apex class future callout,
public class InvokeWebServiceOnStatusChange
{
@future(callout=true)
public static void callWebService(Set<Id> resultIds)
{
try
{
List<PCS_Detail__c> pcsDetail = [Select Name__r.Id,Name__r.Name,Name__r.Breed__c,Name__r.Species__c,Name__r.Gender__c From PCS_Detail__c Where Id IN :resultIds];
NewPetInformation.PackageServiceSoap obj = new NewPetInformation.PackageServiceSoap();
obj.UpdateDatabase(pcsDetail);
}
catch(System.AsyncException e)
{
System.debug('Exception' +e);
}
}

.NET web method,
[WebMethod]
public void UpdateDatabase(List<PCSDetail> values)
{
SqlConnection constr = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
SqlCommand comm = new SqlCommand();
comm.CommandText = "Insert into PetInformation(Id,Name,Breed,Species,Gender) values(@p0,@p1,@p2,@p3,@p4)"; comm.Parameters.AddWithValue("@p0", values[0].Id); comm.Parameters.AddWithValue("@p1", values[0].Name); comm.Parameters.AddWithValue("@p2", values[0].Breed); comm.Parameters.AddWithValue("@p3", values[0].Species); comm.Parameters.AddWithValue("@p4", values[0].Gender);
comm.Connection = constr;
comm.ExecuteNonQuery();
}

Model,
public class PCSDetail
{
public string Id { get; set; }
public string Name { get; set; }
public string Breed { get; set; }
public string Species { get; set; }
public string Gender { get; set; }
}

After generating WSDL and parsed the WSDL into apex classes when i save the apex class which calls the web service, i get the error saying 

Method does not exist or incorrect signature: [NewPetInformation.PackageServiceSoap].UpdateDatabase(List). How to solve this.
http://salesforce.stackexchange.com/questions/115453/web-service-callout-from-salesforce-to-net-using-soap
Martijn SchwarzerMartijn Schwarzer
Hi Ean9,

Can you please post the apex code that was generated from the WSDL? I need that in order to help you with this issue.

Best regards,
Martijn Schwärzer
Ean9Ean9
@Martijn Schwarzer I got an alternative way of accomplishing the scenario. Can you just tell me whether can i pass a List<PCS_Detail__c> pcsDetail via web service callout or not. As of now i am converting it to json string via serialize and then deserializing it in .NET side.
Ean9Ean9
As you have asked I am also posting the code generated from WSDL
//Generated by wsdl2apex

public class NewPetInformation {

    public class ArrayOfPCSDetail_x {
        public NewPetInformation.PCSDetail_x[] PCSDetail_x;
        private String[] PCSDetail_x_type_info = new String[]{'PCSDetail','http://businesssharp.com/packageservice/',null,'0','-1','true'};
        private String[] apex_schema_type_info = new String[]{'http://businesssharp.com/packageservice/','true','false'};
        private String[] field_order_type_info = new String[]{'PCSDetail_x'};
    }
    public class PCSDetail_x {
        public String Id;
        public String Name;
        public String Breed;
        public String Species;
        public String Gender;
        private String[] Id_type_info = new String[]{'Id','http://businesssharp.com/packageservice/',null,'0','1','false'};
        private String[] Name_type_info = new String[]{'Name','http://businesssharp.com/packageservice/',null,'0','1','false'};
        private String[] Breed_type_info = new String[]{'Breed','http://businesssharp.com/packageservice/',null,'0','1','false'};
        private String[] Species_type_info = new String[]{'Species','http://businesssharp.com/packageservice/',null,'0','1','false'};
        private String[] Gender_type_info = new String[]{'Gender','http://businesssharp.com/packageservice/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://businesssharp.com/packageservice/','true','false'};
        private String[] field_order_type_info = new String[]{'Id','Name','Breed','Species','Gender'};
    }
    public class UpdateDatabase_element {
        public NewPetInformation.ArrayOfPCSDetail_x values;
        private String[] values_type_info = new String[]{'values','http://businesssharp.com/packageservice/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://businesssharp.com/packageservice/','true','false'};
        private String[] field_order_type_info = new String[]{'values'};
    }
    public class UpdateDatabaseResponse_element {
        private String[] apex_schema_type_info = new String[]{'http://businesssharp.com/packageservice/','true','false'};
        private String[] field_order_type_info = new String[]{};
    }
    public class PackageServiceSoap {
        public String endpoint_x = 'http://localhost:3251/Services/PackageService.asmx';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'http://businesssharp.com/packageservice/', 'NewPetInformation'};
        public void UpdateDatabase(NewPetInformation.ArrayOfPCSDetail_x values) {
            NewPetInformation.UpdateDatabase_element request_x = new NewPetInformation.UpdateDatabase_element();
            request_x.values = values;
            NewPetInformation.UpdateDatabaseResponse_element response_x;
            Map<String, NewPetInformation.UpdateDatabaseResponse_element> response_map_x = new Map<String, NewPetInformation.UpdateDatabaseResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              'http://businesssharp.com/packageservice/UpdateDatabase',
              'http://businesssharp.com/packageservice/',
              'UpdateDatabase',
              'http://businesssharp.com/packageservice/',
              'UpdateDatabaseResponse',
              'NewPetInformation.UpdateDatabaseResponse_element'}
            );
            response_x = response_map_x.get('response_x');
        }
    }
}
Temoc MunozTemoc Munoz
Hi Ean.

You're calling the wrong method. Take a look at your wsdl one more time. You need to call this one:
public void UpdateDatabase(NewPetInformation.ArrayOfPCSDetail_x values)

Then, you need to pass an ArrayofPCSDetails which will contain your PCSDetail_x list as follows:
NewPetInformation.ArrayOfPCSDetail_x values = new NewPetInformation.ArrayOfPCSDetail_x();
List<NewPetInformation.PCSDetail_x> pcsDetails = new List<NewPetInformation.PCSDetail_x>();

// Add elements as you need them here
pcsDetails[0].Id = 'someId';
........
values.PCSDetail_x = pcsDetails ;

// Finally, call your method:
obj.UpdateDatabase(values);

Thanks
 
Martijn SchwarzerMartijn Schwarzer
Hi Ezhilan,

Your web service callout takes a list of PCSDetail_x objects as argument (in the ArrayOfPCSDetail_x argument).

So you will have to convert your PCSDetail__c objects to PCSDetail_x objects, add them to the ArrayOfPCSDetail_x and use that as an argument in your webservice callout.

I've prepared some code to do that, no JSON involved:
 
//You start with a list of PCSDetail__c objects
List<PCSDetail__c> details; //assume this is a list with actual entries

//first create new ArrayOfPCSDetail_x
NewPetInformation.ArrayOfPCSDetail_x detailsArray = new NetPetInformation.ArrayOfPCSDetail_x();

List<NewPetInformation.PCSDetail_x> details_x = new List<NewPetInformation.PCSDetail_x>();

// Convert to PCSDetail_x objects
for(PCSDetail__c detail : details){
    NewPetInformation.PCSDetail_x detail_x = new NewPetInformation.PCSDetail_x();
    detail_x.Id = detail.Id;
    detail_x.Name = detail.Name;
    detail_x.Breed = detail.Breed;
    detail_x.Species = detail.Species;
    detail_x.Gender = detail.Gender;

    //add detail_x record to detailsArray
    details_x.add(detail_x);
}

detailsArray.PCSDetail_x = details_x;

//Perform the callout
NewPetInformation.PackageServiceSoap soap = new NewPetInformation.PackageServiceSoap();
soap.updateDatabase(detailsArray);

//Handle response
NewPetInformation.UpdateDatabaseResponse_element response = soap.response_x;
//code to handle response goes here
Hope this helps!

Best regards,
Martijn Schwärzer
 
dragan loncardragan loncar
hi
I am  new  in salesforces  could  PLEASE post  code  of creating  webservices (asmx) file  in visual  studio- to generate  WSDL  in salesforces  . PLEASE .