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
somasoma 

Apex code generation from WSDL error - variables using reserved word names

I created a simple webservice in C# for the purposes of processing some data to send emails. One of the parameters for a function of my webservice was called "from". I generated the WSDL for this webservice (and removed all SOAP 1.2 tags), then went to the sandbox to generate the Apex code from the WSDL. The code was generated, but there was an error (see the generated Apex class following this discussion). It took me quite a while to figure out what the problem was. Examining the Apex code revealed nothing, but then I read another post in this forum where someone noted that he/she was able to generate Apex code with reserved words as variable names, e.g. public String return; and this caused a problem. I suppose 'from' is one of those keywords in Apex, so I renamed the field in my code, rewrote the WSDL, then regenerated the Apex code from the WSDL and all was well. The error and the code that generated the error follows:


The following generated class(es) have compilation errors:

Error: TestingGeneration3

Error: unexpected token: String at 15:16

Code:
//Generated by wsdl2apex

public class TestingGeneration3 {
    public class Authenticate_element {
        public String username;
        public String password;
        private String[] username_type_info = new String[]{'username','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
        private String[] password_type_info = new String[]{'password','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://www.example.com/WebServices/','true'};
        private String[] field_order_type_info = new String[]{'username','password'};
    }
    public class Send_element {
        public String toEmails;
        public String bccEmails;
        public String from;   // THE ERROR IS ON THIS LINE
        public String subject;
        public String body;
        private String[] toEmails_type_info = new String[]{'toEmails','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
        private String[] bccEmails_type_info = new String[]{'bccEmails','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
        private String[] from_type_info = new String[]{'from','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
        private String[] subject_type_info = new String[]{'subject','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
        private String[] body_type_info = new String[]{'body','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://www.example.com/WebServices/','true'};
        private String[] field_order_type_info = new String[]{'toEmails','bccEmails','from','subject','body'};
    }
    public class ConfirmationEmailSoap {
        public String endpoint_x = 'http://localhost:38851/ConfirmationEmail.asmx';
        private String[] ns_map_type_info = new String[]{'http://www.example.com/WebServices/', 'TestingGeneration3'};
        public Boolean Send(String toEmails,String bccEmails,String from,String subject,String body) {
            TestingGeneration3.Send_element request_x = new TestingGeneration3.Send_element();
            TestingGeneration3.SendResponse_element response_x;
            request_x.toEmails = toEmails;
            request_x.bccEmails = bccEmails;
            request_x.from = from;
            request_x.subject = subject;
            request_x.body = body;
            Map<String, TestingGeneration3.SendResponse_element> response_map_x = new Map<String, TestingGeneration3.SendResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              'http://www.example.com/WebServices/Send',
              'http://www.example.com/WebServices/',
              'Send',
              'http://www.example.com/WebServices/',
              'SendResponse',
              'TestingGeneration3.SendResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.SendResult;
        }
        public String Authenticate(String username,String password) {
            TestingGeneration3.Authenticate_element request_x = new TestingGeneration3.Authenticate_element();
            TestingGeneration3.AuthenticateResponse_element response_x;
            request_x.username = username;
            request_x.password = password;
            Map<String, TestingGeneration3.AuthenticateResponse_element> response_map_x = new Map<String, TestingGeneration3.AuthenticateResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              'http://www.example.com/WebServices/Authenticate',
              'http://www.example.com/WebServices/',
              'Authenticate',
              'http://www.example.com/WebServices/',
              'AuthenticateResponse',
              'TestingGeneration3.AuthenticateResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.AuthenticateResult;
        }
    }
    public class AuthenticateResponse_element {
        public String AuthenticateResult;
        private String[] AuthenticateResult_type_info = new String[]{'AuthenticateResult','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://www.example.com/WebServices/','true'};
        private String[] field_order_type_info = new String[]{'AuthenticateResult'};
    }
    public class SendResponse_element {
        public Boolean SendResult;
        private String[] SendResult_type_info = new String[]{'SendResult','http://www.w3.org/2001/XMLSchema','boolean','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://www.example.com/WebServices/','true'};
        private String[] field_order_type_info = new String[]{'SendResult'};
    }
}

 

Message Edited by soma on 10-02-2007 12:47 AM

TehNrdTehNrd
I'm no a SOA pro but according to the code you still have a variable named "from" in the apex class.

"From" is a reserved keyword and can not be used as a variable anywhere in apex. Renaming it should fix the problem. Maybe "source" instead? A list of reserved keywords can be located on page 171 of the reference guide.

http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf
somasoma
The code I posted on here is the code with the error in it. I didn't post the revised code. This post was just for information purposes.
TehNrdTehNrd
Whoops...... it's still early for me.  :smileytongue:
brad4dbrad4d
I have recently suffered from the same difficulty.  Unfortunately for me I don't have the luxury of modifying the external WSDL to conform to the internal requirements of SF.com.  It would be very helpful if there were a way to specify that the outbound request should use a specific label for an element - a sort of override of the SF.com internal renaming of things.  I don't need anything other than the outbound XML to contain the reserved word.  If I could pass that around as a string to override the default tag for the element (which in this case is being concatenated with _x in accordance with SF nameing convention) then I could get what I need from the client and I could call this web service.