+ Start a Discussion
Ron WildRon Wild 

Passing parameters to an Apex Web Service

I've seen examples demonstrating how to invoke an Apex Web Service from inside the Ajax toolkit, but these only show simple parameter types

result = sforce.apex.execute('MyClass' ,'methodX',{role:"User", firstname "Joe", lastname "Test");

But what how do you pass complex parameters to the Web Service???

For sake of example, lets say you have the following Apex Class Definitions:

global class MyClass {
    WebService static String methodX(String role, ContactX contact) {
        return (role + ': '+ contact.firstname + ' ' + contact.lastname);   
    }
}


and

global class ContactX {
    public String firstname;
    public String lastname;
}



How would you invoke methodX, passing in an instance of the ContactX class?   I tried the following approaches - but had no luck ...

    result = sforce.apex.execute('MyClass' ,'methodX',{role:"User", contact:{firstname:"Joe", lastname:"Test"} });

    // null values for firstname and lastname are returned


and

    var newcontact = new sforce.Xml("ContactX");
    newcontact.firstname = "Joe";
    newcontact.lastname = "Test":
    result = sforce.apex.execute('MyClass' ,'methodX',{role:"User", contact:newcontact });

    //  soapenv:Client ->  There is no public member called 'firstname' in the Apex class 'ContactX'

Any suggestions?

Thanks,




Best Answer chosen by Admin (Salesforce Developers) 
cheenathcheenath
You have to mark the field as webservice, so this:

global class ContactX {
    public String firstname;
    public String lastname;
}

should be:

global class ContactX {
    webservice String firstname;
    webservice String lastname;
}

Then your code below should work.

  var newcontact = new sforce.Xml("ContactX");
    newcontact.firstname = "Joe";
    newcontact.lastname = "Test":
    result = sforce.apex.execute('MyClass' ,'methodX',{role:"User", contact:newcontact });

HTHs,






All Answers

cheenathcheenath
You have to mark the field as webservice, so this:

global class ContactX {
    public String firstname;
    public String lastname;
}

should be:

global class ContactX {
    webservice String firstname;
    webservice String lastname;
}

Then your code below should work.

  var newcontact = new sforce.Xml("ContactX");
    newcontact.firstname = "Joe";
    newcontact.lastname = "Test":
    result = sforce.apex.execute('MyClass' ,'methodX',{role:"User", contact:newcontact });

HTHs,






This was selected as the best answer
Ron WildRon Wild
Thanks a bunch!  I was really hoping someone would answer that one.

By the way, I just noticed a bug in the Ajax toolkit that you might want to know about...

If you have a nested class with 'webservice' declaratives, the Ajax Toolkit Test page has trouble parsing the class.

global Outer {

    global class ContactX {
       webservice String firstname;
       webservice String lastname;
    }

webservice static function String fullname(ContactX contact) {
       return (contact.firstname + contact.lastname);
}

} // end class


I'll be happy to email you a screenshot if you need more information.

Regards,
cheenathcheenath
Ok, I can see that an inner class could be a problem.
I will fix this in the next release of Ajax Tools.

Thanks.
Ad_InfosysAd_Infosys

HI, I implemented the method to pass parameters from SControl to SFDC.

Still I am facing error. My web service is:

global class DTCTest1 {
global class StatusDetail{
webservice String Zip;
webservice String Occupancy;
webservice String Property;
webservice String Grade;
webservice String MaxLoan; }
WebService static String DTC(StatusDetail sd) {
 ZIP_Status_Mapping__c Zsp=[select Status__C from ZIP_Status_Mapping__c where Name =: sd.Zip];
String Cltv=[select Max_CLTV__c from Status_Details__c where ((Occupancy_Type__c =: sd.Occupancy) and (Property_Type__c =: sd.Property) and (Credit_Grade__c =: sd.Grade) and (Max_Loan_Line__c =: sd.MaxLoan)and (Market__C =: Zsp.Status__C))].Max_CLTV__c;
return Cltv;
}

My scontrol has following section.
 
var nr = new sforce.Xml("StatusDetail");
nr.zip = '{!TestScreen__c.Zip__c}';
nr.oc= '{!TestScreen__c.Occupancy__c}';
nr.pro= '{!TestScreen__c.Property__c}';
nr.gra= '{!TestScreen__c.Grade__c}';
nr.max= '{!TestScreen__c.Maxloan__c}';
var result = sforce.apex.execute('DTCTest1' ,'DTC',{sd:nr });
 
I am facin error in retrieving the result. Seems I am not able to send the parameters. Kindly help on this.
 
Thanks in Advance
Ron WildRon Wild
If your class is part of a package, you may need to include the namespace in your call...

var result = sforce.apex.execute('xyz/DTCTest1' ,'DTC',{sd:nr });

where 'xyz' is the namespace you created when you set up the package.

If that doesn't work, I suggest putting a System.debug('...') in your method and check the parameters passed in. (Output can be viewed in the Debug Log of your developer account after the call).




Ad_InfosysAd_Infosys

Thanks Dear,

How do I identify this. I have not included anything like this in the code.

 

cptcpt

Hi Ron ,

 

 i have tried before what example you said for passing the object as  a paramter but i am getting the null value execption

 

 

my flex code is :

 

 var SampleLoginParams:ObjectProxy = new ObjectProxy(simpleLoginObject);           
       
            
             var f_Info:Parameter = new Parameter("SampleLoginParams", SampleLoginParams,true);
            
             var params:Array = [f_Info];                 
            
             var tempCallback1:AsyncResponder = new AsyncResponder(userLoginResultHandler1,userLoginFaultHandler1);       
               
               apex.execute("SimpleLoginService","loginFields1",params,tempCallback1);  

 

and the global class is:

 

 

global class SampleLoginParams {
       
        webservice String password;
        webservice String userId;    

}

 

and the apex method is:

 

global class SimpleLoginService {
    
     WebService static String loginFields1(SampleLoginParams params) {

  //here the param itself null
          SitemmanagementYintegDomyexpensesComXsd.SimpleLoginFields sampleResponse;
          System.debug('the value of param'+params.userId);

 return 'Login info is '+(params==null?'':params.toString());

}

}

 

The error in log is null value exception...why apex not taking flex objext as a input here whats the problem ..

please help me

hattihatti

HI, I need your help, I have a global class and it has an inner class. There is a method in the main class which accepts List<inner class>. To create a single instance and pass it, we do it like this, var smp = new sforce.Xml("Innerclass");

Then,

var acc = sforce.apex.execute("myclass", "mymethod", {param:smp}). Please let me know how we can execute if we have to pass a list to the method. How do we create this list in javascript.

Mayte MartínezMayte Martínez
Hi Guys,

cheenath how can I test your code solution?

This is my code:
Button:
var pr = new sforce.Xml("enviarParametros"); 
pr.pageReference = "/apex/PDFpage?id="; 
var url = sforce.apex.execute('classX','method',{Id:"{!Quote.Id}", pageReference2:pr}); 

Class enviarParametros:
global class enviarParametros {
  webservice String pageReference;
    public void enviarParametros(){ }
}

Class classX:
global with sharing class classX{
webService static String method (Id Id, enviarParametros pageReference2){ 
...
 String pageReference3 = pageReference2.pageReference;
...

How can I test my classX?
This is my test class:
...
String pr = new sforce.Xml('enviarParametros');
pr.pageReference = '/apex/PDFpage?id='; 
enviarParametros enviarParametrosPrueba = new enviarParametros();
classX.method(QuoteTest.Id, enviarParametrosPrueba);
...

I got "Invalid type: sforce.Xml"