Update an individual budget for a specific campaign

484 views
Skip to first unread message

roberto....@nu3.de

unread,
Jan 8, 2016, 2:18:06 PM1/8/16
to AdWords API Forum
Dear All,

I am quite new with the AdWords API. I have the need to update the budget for a list of campaigns. I have to assign a new individual budget, removing the previous shared budget.

Up to now I tried to follow the instructions present here, https://developers.google.com/adwords/api/docs/guides/budgets, but without results.

In the row 70 of my code, i correctly see the new budget, but on the AdWords platform I cannot see the result.

I have to change the budget for more than 2000 campaigns, can someone tell me where and why my code does not work?

Many many thanks.

Roberto

PS: In the code i iterate trough all the campaigns, because at the moment I am not able to select a campaign by name...
________________________________________________________________________________________________________________________________________________________

package My_Test;

import java.rmi.RemoteException;

import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.axis.utils.v201509.SelectorBuilder;
import com.google.api.ads.adwords.axis.v201509.cm.ApiException;
import com.google.api.ads.adwords.axis.v201509.cm.Budget;
import com.google.api.ads.adwords.axis.v201509.cm.BudgetBudgetDeliveryMethod;
import com.google.api.ads.adwords.axis.v201509.cm.BudgetBudgetPeriod;
import com.google.api.ads.adwords.axis.v201509.cm.BudgetOperation;
import com.google.api.ads.adwords.axis.v201509.cm.BudgetServiceInterface;
import com.google.api.ads.adwords.axis.v201509.cm.Campaign;
import com.google.api.ads.adwords.axis.v201509.cm.CampaignOperation;
import com.google.api.ads.adwords.axis.v201509.cm.CampaignPage;
import com.google.api.ads.adwords.axis.v201509.cm.CampaignReturnValue;
import com.google.api.ads.adwords.axis.v201509.cm.CampaignServiceInterface;
import com.google.api.ads.adwords.axis.v201509.cm.Money;
import com.google.api.ads.adwords.axis.v201509.cm.Operator;
import com.google.api.ads.adwords.axis.v201509.cm.Selector;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.adwords.lib.selectorfields.v201509.cm.CampaignField;
import com.google.api.ads.common.lib.auth.OfflineCredentials;
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
import com.google.api.ads.common.lib.conf.ConfigurationLoadException;
import com.google.api.ads.common.lib.exception.OAuthException;
import com.google.api.ads.common.lib.exception.ValidationException;
import com.google.api.client.auth.oauth2.Credential;

public class Budget_Assignetor{
public static void main(String[] args) throws OAuthException, ValidationException, ConfigurationLoadException, ApiException, RemoteException {
Credential oAuth2Credential = new OfflineCredentials.Builder().forApi(Api.ADWORDS).fromFile().build().generateCredential();// Generate a refreshable OAuth2 credential.
   AdWordsServices adWordsServices = new AdWordsServices();
AdWordsSession session = new AdWordsSession.Builder().fromFile().withOAuth2Credential(oAuth2Credential).build();// Construct an AdWordsSession.
BudgetServiceInterface budgetService =  adWordsServices.get(session, BudgetServiceInterface.class);
Budget individualBudget = new Budget();
individualBudget.setName("New Budget 1000 euro per day");
Money budgetAmount = new Money();
budgetAmount.setMicroAmount(1000000000L);
individualBudget.setAmount(budgetAmount);
individualBudget.setIsExplicitlyShared(false);
individualBudget.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);
individualBudget.setPeriod(BudgetBudgetPeriod.DAILY);
BudgetOperation budgetOperation = new BudgetOperation();
budgetOperation.setOperand(individualBudget);
budgetOperation.setOperator(Operator.ADD); 
Long budgetId =  budgetService.mutate(new BudgetOperation[] {budgetOperation}).getValue(0).getBudgetId();
individualBudget.setBudgetId(budgetId);
int PAGE_SIZE = 100;
   CampaignServiceInterface campaignService = adWordsServices.get(session, CampaignServiceInterface.class);
   int offset = 0;
   // Create selector.
   SelectorBuilder builder = new SelectorBuilder();
   Selector selector = builder.fields(CampaignField.Id, CampaignField.Name).orderAscBy(CampaignField.Name).offset(offset).limit(PAGE_SIZE).build();
   CampaignPage page = null;
   
   do {
     page = campaignService.get(selector);
     if (page.getEntries() != null) {
       for (Campaign campaign : page.getEntries()) {
           if(campaign.getName().equals("Naturdiet | Headterm | BMM")) {
            campaign.setBudget(individualBudget);
            System.out.println(campaign.getName() + "   " + campaign.getBudget().getName());
               CampaignOperation operation = new CampaignOperation();
               operation.setOperand(campaign);
               operation.setOperator(Operator.SET);
               CampaignOperation[] operations = new CampaignOperation[] {operation};
               CampaignReturnValue result = campaignService.mutate(operations);
           }
       }
     }
     offset += PAGE_SIZE;
     selector = builder.increaseOffsetBy(PAGE_SIZE).build();
   } 
   while (offset < page.getTotalNumEntries());
}
}

Yin Niu

unread,
Jan 8, 2016, 3:00:45 PM1/8/16
to AdWords API Forum
Hi Roberto, 

When I ran your code, I got this error "BudgetError.CANNOT_USE_IMPLICITLY_SHARED_BUDGET_WITH_MULTIPLE_CAMPAIGNS @ operations[0].operand.budget.budgetId] ". This is because you set Budget attribute IsExplicitlyShared to false. The default value for IsExplicitlyShared is true. 

Yin, AdWords API Team. 
Reply all
Reply to author
Forward
0 new messages