Trying to run a script to remove multiple Adgroups

24 views
Skip to first unread message

Dan Hai

unread,
Mar 7, 2017, 11:45:00 AM3/7/17
to AdWords API Forum
Not sure where I am messing up. Here is my code:
public static function runExample(AdWordsServices $adWordsServices,
AdWordsSession $session)
{



$adGroupId = array(3729748624, 37497486701);
$adGroupCount = count($adGroupId);

for ($x = 0; $x < $adGroupCount; $x++) {
$array = $adGroupId[$adGroupCount];
$adGroupService = $adWordsServices->get($session, AdGroupService::class);

$operations = [];
// Create ad group with REMOVED status.
$adGroup = new AdGroup();
$adGroup->setId($array);
$adGroup->setStatus(AdGroupStatus::REMOVED);

// Create ad group operation and add it to the list.
$operation = new AdGroupOperation();
$operation->setOperand($adGroup);
$operation->setOperator(Operator::SET);
$operations[] = $operation;

// Remove the ad group on the server.
$result = $adGroupService->mutate($operations);

$adGroup = $result->getValue()[0];
printf("Ad group with ID %d was removed.\n", $adGroup->getId());
}
}
I am getting an  [RequiredError.REQUIRED @ operations[0].operand.id] error.

Vishal Vinayak (Adwords API Team)

unread,
Mar 7, 2017, 3:19:20 PM3/7/17
to AdWords API Forum
Hi,

The code that you shared looks similar to the RemoveAdGroup example available in the PHP client library. However, there might be issues with the way you are accessing index of the $adGroupId array. Also, the count doesn't seem to increment in the loop. Could you please fix these issues and confirm if that works? If not, are you able to remove one ad group at a time if you run the aforementioned example? Lastly, could you please check if you are using the correct ad group IDs, since I'm not able to see these ad groups in the system. If you are still facing issues, could you please provide me with the SOAP XML request and response logs for the API call that returns this error?

Regards,
Vishal, AdWords API Team

Dan Hai

unread,
Mar 9, 2017, 3:08:53 PM3/9/17
to AdWords API Forum
Resolved, Here is the working code!!
public static function runExample(AdWordsServices $adWordsServices,
AdWordsSession $session)
{

    $adGroupId = array(39345934486, 40389896837, 44185215527);
    $adGroupCount = count($adGroupId);

for ($x = 0; $x < $adGroupCount; $x++) {
        $array = $adGroupId[$x];
        $adGroupService = $adWordsServices->get($session, AdGroupService::class);

$operations = [];
// Create ad group with REMOVED status.
$adGroup = new AdGroup();
$adGroup->setId($array);
$adGroup->setStatus(AdGroupStatus::REMOVED);
Reply all
Reply to author
Forward
0 new messages