1

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 AdoptionDetailsTrigger on PCS_Detail__c (after update) {

    List<PCS_Detail__c> adoptionDetail = [Select Id,Tracking_Details__c From PCS_Detail__c Where Id IN :Trigger.new];
    if(adoptionDetail[0].Tracking_Details__c == 'Approved')
    {
    Set<Id> resultIds = (new Map<Id,SObject>(adoptionDetail)).keySet();
    InvokeWebServiceOnAdoptionStatusChange.callWebService(resultIds);
    }
}

Apex class future callout,

public class InvokeWebServiceOnAdoptionStatusChange {

        @future(callout=true)
        public static void callWebService(Set<Id> resultIds) {
        try {
            String strSobjects = '';
            List<PCS_Detail__c> adoptionDetail = [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];
            strSobjects = strSobjects + ',' + JSON.serialize(adoptionDetail);
            PetInformationDetail.PackageServiceSoap obj = new PetInformationDetail.PackageServiceSoap();
            obj.UpdateDatabase(strSobjects);
        }
        catch(System.AsyncException e) {
            System.debug('Exception' +e);    
        }
    }
}

.NET web method,

    [WebMethod]
    public void UpdateDatabase(string jsonValues)
    {
        List<PCSDetail> values = JsonConvert.DeserializeObject<List<PCSDetail>>(jsonValues);

        SqlConnection constr = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
        SqlCommand comm = new SqlCommand();
        comm.CommandText = "Insert into PetInformation(PetId,PetName,PetBreed,PetSpecies,PetGender) 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; }
}

Apex class generated using the web service WSDL,

//Generated by wsdl2apex

public class PetInformationDetail {
    public class UpdateDatabase_element {
        public String jsonValues;
        private String[] jsonValues_type_info = new String[]{'jsonValues','http://tempuri.org/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
        private String[] field_order_type_info = new String[]{'jsonValues'};
    }
    public class UpdateDatabaseResponse_element {
        private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','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://tempuri.org/', 'PetInformationDetail'};
        public void UpdateDatabase(String jsonValues) {
            PetInformationDetail.UpdateDatabase_element request_x = new PetInformationDetail.UpdateDatabase_element();
            request_x.jsonValues = jsonValues;
            PetInformationDetail.UpdateDatabaseResponse_element response_x;
            Map<String, PetInformationDetail.UpdateDatabaseResponse_element> response_map_x = new Map<String, PetInformationDetail.UpdateDatabaseResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              'http://tempuri.org/UpdateDatabase',
              'http://tempuri.org/',
              'UpdateDatabase',
              'http://tempuri.org/',
              'UpdateDatabaseResponse',
              'PetInformationDetail.UpdateDatabaseResponse_element'}
            );
            response_x = response_map_x.get('response_x');
        }
    }
}

Will this work properly?

  • 2
    UpdateDatabase method accept the NewPetInformation.ArrayOfPCSDetail not List<PCS_Detail__c>. first create ``NewPetInformation.ArrayOfPCSDetail` and pass into UpdateDatabase method. – Ratan Paul Mar 25 '16 at 9:26
  • @sfdcweb Can you be able to post the sample code. – Ean Mar 25 '16 at 9:28
  • Can you add NewPetInformation class in your question ? – Ratan Paul Mar 25 '16 at 9:29
  • @sfdcweb updated the code. – Ean Mar 25 '16 at 9:34
  • Ean.. this is not clear to how you will create NewPetInformation.ArrayOfPCSDetail with List<PCS_Detail__c> this s not clear – Ratan Paul Mar 25 '16 at 9:44
2

@Ean, You need to pass object of PCSDetail not List: As per my understanding it should be somewhat as below:

public class InvokeWebServiceOnStatusChange {

    @future(callout=true)
    public static void callWebService(Set<Id> resultIds) {
    try {
    PCSDetail[] pcsarray= new PCSDetail[]();
    List<PCS_Detail__c> pcsDetailList = [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];
    for(PCS_Detail__c p : pcsDetailList){
        PCSDetail pcs = new PCSDetail();    
            pcs.id= p.Name__r.Id;
            pcs.Name=p.Name__r.Name;
            pcs.Breed= p.Name__r.Breed__c;
            pcs.Species = p.Name__r.Species__c;
            pcs.Gender = p.Name__r.Gender__c;
            pcsarray.add(pcs);     
    }


        NewPetInformation.PackageServiceSoap obj = new NewPetInformation.PackageServiceSoap();
        obj.UpdateDatabase(pcsarray);
    }
    catch(System.AsyncException e) {
        System.debug('Exception' +e);    
    }
}

There might be syntax error but , I believe you got the idea.

  • What is pcsDetail in the above code. Did you miss out any line. Also could you tell me whether the web method which i wrote in .NET is correct or not. – Ean Mar 25 '16 at 11:21
  • I will try out your idea. Do i need to change the Web method or is it correct? – Ean Mar 25 '16 at 11:23
  • 1
    @Ean yes so basically the idea is in salesforce end you need create a class like your dot net model PCSDetail and pass that list into your weservice method and in your webservice you need to PackageServiceSoap.UpdateDatabase method should accept the List<PCSDetail> as parameter.. This way you can send the list of values to callout and you can get those values. – Ratan Paul Mar 25 '16 at 12:29
  • 1
    @Ean you can do that convert into JSON and then pass through web service.. and you can deserliaze that JSON in dot set side. check this post how to create JSON etc developer.salesforce.com/page/Getting_Started_with_Apex_JSON – Ratan Paul Mar 25 '16 at 12:58
  • 1
    @sfdcweb Sure. I will do that and get back if i face any issues. Thanks for your support. – Ean Mar 25 '16 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.