1

I am attempting to make a call from an Apex class that I created to the Google AdWords API via SOAP Request. I am new to SOAP so I am not sure exactly how to make the call.

I have generated 2 classes from a WSDL file provided by Google, and now I am just trying to utilize the methods that are inside the generated classes.

My questions are: Do I need a separate .XML file to define the SOAP request? Or can I just use the methods provided in the classes (below).

My Class, where I am trying to utilize the generated WSDL class

global class GoogleAdWordsLeadReportBatch implements Database.Batchable<sObject> {
private String searchQuery;

global GoogleAdWordsLeadReportBatch(String searchQuery){
    this.searchQuery = searchQuery;
}

global Database.QueryLocator start(Database.BatchableContext BC){
    return Database.getQueryLocator(searchQuery);
}

global void execute(Database.BatchableContext BC, List<Lead> leadList){
    //Make a callout to retrieve Authorization Token (OAuth), put this in this class, or make another class to handle this specifically (returning the token).
    String accessTokenUrl = 'https://accounts.google.com/o/oauth2/auth?client_id=your-client-id&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fadwords&redirect_uri=urn:ietf:wg:oauth:2.0:oob&access_type=offline&approval_prompt=auto';



    //Object to be used to make callout
    GoogleAdwordsConversionTracker.ConversionTrackerServiceInterfacePort conversionTrackerServiceInterfacePort = new GoogleAdwordsConversionTracker.ConversionTrackerServiceInterfacePort();

    //HEADERS
    conversionTrackerServiceInterfacePort.inputHttpHeaders_x = new Map<String, String>();
    conversionTrackerServiceInterfacePort.inputHttpHeaders_x.put('Authorization', '');
    conversionTrackerServiceInterfacePort.inputHttpHeaders_x.put('developerToken', '');
    conversionTrackerServiceInterfacePort.inputHttpHeaders_x.put('userAgent', '');
    conversionTrackerServiceInterfacePort.inputHttpHeaders_x.put('clientCustomerId', '');

    List<GoogleAdWordsConversionTracker.ConversionTrackerOperation> operationList = new List<GoogleAdWordsConversionTracker.ConversionTrackerOperation>();

    for(Lead lead : leadList){
        GoogleAdwordsConversionTracker.ConversionTracker conversionTracker = new GoogleAdwordsConversionTracker.ConversionTracker();
        conversionTracker.name = 'AdWords Name';
        conversionTracker.category = 'LEAD';
        //Anymore fields

        GoogleAdwordsConversionTracker.ConversionTrackerOperation conversionTrackerOperation = new GoogleAdwordsConversionTracker.ConversionTrackerOperation();
        conversionTrackerOperation.operand = conversionTracker;
        conversionTrackerOperation.operator = 'ADD';
        //Anymore fields

        operationList.add(conversionTrackerOperation);
    }


    conversionTrackerServiceInterfacePort.mutate(operationList);

}
global void finish(Database.BatchableContext BC){
    //Code to run after all batches finish
}

}

WSDL Generated Class (Trimmed Class because of 500 lines)

public class GoogleAdWordsConversionTracker {
    public class ConversionTracker {
        public Long id;
        public Long originalConversionTypeId;
        public String name;
        public String status;
        public String category;
        public Long conversionTypeOwnerCustomerId;
        public Integer viewthroughLookbackWindow;
        public Integer ctcLookbackWindow;
        public String countingType;
        public Double defaultRevenueValue;
        public String defaultRevenueCurrencyCode;
        public Boolean alwaysUseDefaultRevenueValue;
        public Boolean excludeFromBidding;
        public String mostRecentConversionDate;
        public String lastReceivedRequestTime;
        public String ConversionTracker_Type;
        private String[] id_type_info = new String[]{'id','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] originalConversionTypeId_type_info = new String[]{'originalConversionTypeId','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] name_type_info = new String[]{'name','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] status_type_info = new String[]{'status','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] category_type_info = new String[]{'category','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] conversionTypeOwnerCustomerId_type_info = new String[]{'conversionTypeOwnerCustomerId','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] viewthroughLookbackWindow_type_info = new String[]{'viewthroughLookbackWindow','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] ctcLookbackWindow_type_info = new String[]{'ctcLookbackWindow','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] countingType_type_info = new String[]{'countingType','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] defaultRevenueValue_type_info = new String[]{'defaultRevenueValue','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] defaultRevenueCurrencyCode_type_info = new String[]{'defaultRevenueCurrencyCode','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] alwaysUseDefaultRevenueValue_type_info = new String[]{'alwaysUseDefaultRevenueValue','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] excludeFromBidding_type_info = new String[]{'excludeFromBidding','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] mostRecentConversionDate_type_info = new String[]{'mostRecentConversionDate','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] lastReceivedRequestTime_type_info = new String[]{'lastReceivedRequestTime','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] ConversionTracker_Type_type_info = new String[]{'ConversionTracker.Type','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'https://adwords.google.com/api/adwords/cm/v201605','true','false'};
        private String[] field_order_type_info = new String[]{'id','originalConversionTypeId','name','status','category','conversionTypeOwnerCustomerId','viewthroughLookbackWindow','ctcLookbackWindow','countingType','defaultRevenueValue','defaultRevenueCurrencyCode','alwaysUseDefaultRevenueValue','excludeFromBidding','mostRecentConversionDate','lastReceivedRequestTime','ConversionTracker_Type'};
    }

    public virtual class Operation {
        public String operator;
        public String Operation_Type;
        private String[] operator_type_info = new String[]{'operator','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] Operation_Type_type_info = new String[]{'Operation.Type','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'https://adwords.google.com/api/adwords/cm/v201605','true','false'};
        private String[] field_order_type_info = new String[]{'operator','Operation_Type'};
    }


    public class ConversionTrackerOperation extends Operation{
        public GoogleAdWordsConversionTracker.ConversionTracker operand;
        private String[] operand_type_info = new String[]{'operand','https://adwords.google.com/api/adwords/cm/v201605',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'https://adwords.google.com/api/adwords/cm/v201605','true','false'};
        private String[] field_order_type_info = new String[]{'operand'};
    }


 public class ConversionTrackerServiceInterfacePort {
        public String endpoint_x = 'https://adwords.google.com/api/adwords/cm/v201605/ConversionTrackerService';
        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;
        public GoogleAdWordsConversionTracker.SoapHeader RequestHeader;
        public GoogleAdWordsConversionTracker.SoapResponseHeader ResponseHeader;
        private String RequestHeader_hns = 'RequestHeader=https://adwords.google.com/api/adwords/cm/v201605';
        private String ResponseHeader_hns = 'ResponseHeader=https://adwords.google.com/api/adwords/cm/v201605';
        private String[] ns_map_type_info = new String[]{'https://adwords.google.com/api/adwords/cm/v201605', 'GoogleAdWordsConversionTracker'};
        public GoogleAdWordsConversionTracker.ConversionTrackerReturnValue mutate(GoogleAdWordsConversionTracker.ConversionTrackerOperation[] operations) {
            GoogleAdWordsConversionTracker.mutate_element request_x = new GoogleAdWordsConversionTracker.mutate_element();
            request_x.operations = operations;
            GoogleAdWordsConversionTracker.mutateResponse_element response_x;
            Map<String, GoogleAdWordsConversionTracker.mutateResponse_element> response_map_x = new Map<String, GoogleAdWordsConversionTracker.mutateResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'https://adwords.google.com/api/adwords/cm/v201605',
              'mutate',
              'https://adwords.google.com/api/adwords/cm/v201605',
              'mutateResponse',
              'GoogleAdWordsConversionTracker.mutateResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.rval;
        }
        public GoogleAdWordsConversionTracker.ConversionTrackerPage get(GoogleAdWordsConversionTracker.Selector serviceSelector) {
            GoogleAdWordsConversionTracker.get_element request_x = new GoogleAdWordsConversionTracker.get_element();
            request_x.serviceSelector = serviceSelector;
            GoogleAdWordsConversionTracker.getResponse_element response_x;
            Map<String, GoogleAdWordsConversionTracker.getResponse_element> response_map_x = new Map<String, GoogleAdWordsConversionTracker.getResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'https://adwords.google.com/api/adwords/cm/v201605',
              'get',
              'https://adwords.google.com/api/adwords/cm/v201605',
              'getResponse',
              'GoogleAdWordsConversionTracker.getResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.rval;
        }
        public GoogleAdWordsConversionTracker.ConversionTrackerPage query(String query) {
            GoogleAdWordsConversionTracker.query_element request_x = new GoogleAdWordsConversionTracker.query_element();
            request_x.query = query;
            GoogleAdWordsConversionTracker.queryResponse_element response_x;
            Map<String, GoogleAdWordsConversionTracker.queryResponse_element> response_map_x = new Map<String, GoogleAdWordsConversionTracker.queryResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'https://adwords.google.com/api/adwords/cm/v201605',
              'query',
              'https://adwords.google.com/api/adwords/cm/v201605',
              'queryResponse',
              'GoogleAdWordsConversionTracker.queryResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.rval;
        }
    }
}

1 Answer 1

0

The classes generated in GoogleAdWordsConversionTracker will be used in conjunction with the WebServiceCallout.invoke method to create the required SOAP XML requests and parse the responses. That's what all those *_type_info members are for. They tell Apex how to convert the classes into XML.

I notice that you are making the callouts from a batch. You will need to add Database.AllowsCallouts to the class definition to allow the callouts to proceed. Also, don't forget to a a Remote Site Setting for the URL you are making the callouts to.

2
  • I see, thanks for the response. So the WebServiceCallout.invoke will go to this URL "adwords.google.com/api/adwords/cm/v201605" for example for the definition of the XML? I tried going to adwords.google.com/api/adwords/cm/v201605, but it doesn't show anything. Any ideas why?
    – Coova
    Commented Jun 23, 2016 at 13:51
  • @ItzHoudini I don't follow why you need to make a request to https://adwords.google.com/api/adwords/cm/v201605. The endpoing that the XML will be sent to is https://adwords.google.com/api/adwords/cm/v201605/ConversionTrackerService. The text you mention is a namespace. It can be confusing when the namespace looks like a URL. Try adwords.google.com/api/adwords/cm/v201605/… Commented Jun 24, 2016 at 0:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .