การใช้รหัสชั่วคราว
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ฟีเจอร์ที่ทรงพลังของ BatchJobService
คือ
รองรับการใช้รหัสชั่วคราว
คุณทำได้โดยการระบุ resource_name
ของทรัพยากรใหม่ให้ใช้รหัสติดลบ เช่น สมมติว่าคุณสร้างแคมเปญและระบุชื่อทรัพยากรเป็น customers/<YOUR_CUSTOMER_ID>/campaigns/-1
เมื่อสร้าง
กลุ่มโฆษณาในการดำเนินการในภายหลัง คุณจะอ้างอิงกลุ่มโฆษณานั้นตามชื่อทรัพยากร
และ -1
ที่ระบุไว้ได้ โดยระบบจะแทนที่ด้วยรหัสจริงของแคมเปญที่สร้างขึ้น
โดยอัตโนมัติ
สิ่งที่ควรคำนึงถึงเมื่อใช้ชื่อทรัพยากรชั่วคราวมีดังนี้
- ชื่อทรัพยากรชั่วคราวจะใช้ได้ก็ต่อเมื่อมีการกำหนดไว้ใน
ทรัพยากรแล้ว ในตัวอย่างต่อไปนี้ การดำเนินการกลุ่มโฆษณาจะต้อง
ปรากฏหลังการดำเนินการแคมเปญในรายการการดำเนินการ
- ระบบจะไม่จดจำชื่อทรัพยากรชั่วคราวในงานหรือคำขอเปลี่ยนแปลง
หากต้องการอ้างอิงทรัพยากรที่สร้างขึ้นในงานหรือคำขอเปลี่ยนแปลงก่อนหน้า ให้ใช้ชื่อทรัพยากรจริง
- สำหรับคำขอเดียวหรือคำขอเปลี่ยนแปลง ชื่อทรัพยากรชั่วคราวแต่ละชื่อต้องใช้
ตัวเลขลบที่ไม่ซ้ำกัน แม้ว่าจะเป็นทรัพยากรประเภทต่างๆ ก็ตาม หากมีการนำรหัสชั่วคราวมาใช้ซ้ำในงานเดียวหรือคำขอเปลี่ยนแปลง ระบบจะแสดงข้อผิดพลาด
ตัวอย่าง
สมมติว่าคุณต้องการเพิ่มแคมเปญ กลุ่มโฆษณา และโฆษณาในคำขอ API เดียว คุณจะสร้างโครงสร้างสำหรับคำขอในลักษณะเดียวกับ
ต่อไปนี้
mutate_operations: [
{
campaign_operation: {
create: {
resource_name: "customers/<YOUR_CUSTOMER_ID>/campaigns/-1",
...
}
}
},
{
ad_group_operation: {
create: {
resource_name: "customers/<YOUR_CUSTOMER_ID>/adGroups/-2",
campaign: "customers/<YOUR_CUSTOMER_ID>/campaigns/-1"
...
}
}
},
{
ad_group_ad_operation: {
create: {
ad_group: "customers/<YOUR_CUSTOMER_ID>/adGroups/-2"
...
}
}
},
]
ระบบจะใช้รหัสชั่วคราวใหม่สำหรับกลุ่มโฆษณา เนื่องจากเราไม่สามารถนำ -1
ที่ใช้กับแคมเปญมาใช้ซ้ำได้ นอกจากนี้ เรายังอ้างอิงกลุ่มโฆษณานี้เมื่อสร้างโฆษณากลุ่มโฆษณาด้วย
กลุ่มโฆษณาอ้างอิงชื่อทรัพยากรที่เราสร้างขึ้นสำหรับ
แคมเปญในการดำเนินการก่อนหน้านี้ในคำขอ ส่วน resource_name
ใน
ad_group_ad_operation
ไม่จำเป็นเนื่องจากไม่มีการดำเนินการเพิ่มเติม
ที่อ้างอิงถึง
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-27 UTC
[null,null,["อัปเดตล่าสุด 2025-08-27 UTC"],[[["\u003cp\u003e\u003ccode\u003eBatchJobService\u003c/code\u003e allows the use of temporary IDs (negative numbers) for new resources within a single job, enabling you to reference them in subsequent operations within the same request.\u003c/p\u003e\n"],["\u003cp\u003eTemporary resource names must be unique within a single job or mutate request and are not remembered across different jobs or requests; use actual resource names for referencing resources created earlier.\u003c/p\u003e\n"],["\u003cp\u003eResources with temporary IDs must be defined before being referenced by other operations within the same request, ensuring the order of operations reflects dependencies.\u003c/p\u003e\n"]]],[],null,["# Using temporary IDs\n\nA powerful feature of [`BatchJobService`](/google-ads/api/reference/rpc/v21/BatchJobService) is that\nit supports the use of temporary IDs.\n\nYou can do this by specifying the new resource's `resource_name` to use a\nnegative ID. For example, suppose you create a campaign and specify its resource name\nas `customers/\u003cYOUR_CUSTOMER_ID\u003e/campaigns/-1`. When you create the\nad group in a later operation, you can then reference it by that resource name\nand the `-1` you specified will be replaced by the actual ID of the created\ncampaign automatically.\n\nHere are some things to keep in mind when using temporary resource names:\n\n- A temporary resource name can only be used after it's been defined in a resource. In the following example, the ad group operation would have to appear after the campaign operation in the list of operations.\n- Temporary resource names are not remembered across jobs or mutate requests. To reference a resource created in a previous job or mutate request, use its actual resource name.\n- For a single job or mutate request, each temporary resource name must use a unique negative number, even if they are from different resource types. If a temporary ID is reused in a single job or mutate request, then an error is returned.\n\n### Example\n\nSuppose you want to add a campaign, an ad group, and an ad in a single API\nrequest. You would create a structure for your request analogous to the\nfollowing: \n\n mutate_operations: [\n {\n campaign_operation: {\n create: {\n resource_name: \"customers/\u003cYOUR_CUSTOMER_ID\u003e/campaigns/-1\",\n ...\n }\n }\n },\n {\n ad_group_operation: {\n create: {\n resource_name: \"customers/\u003cYOUR_CUSTOMER_ID\u003e/adGroups/-2\",\n campaign: \"customers/\u003cYOUR_CUSTOMER_ID\u003e/campaigns/-1\"\n ...\n }\n }\n },\n {\n ad_group_ad_operation: {\n create: {\n ad_group: \"customers/\u003cYOUR_CUSTOMER_ID\u003e/adGroups/-2\"\n ...\n }\n }\n },\n ]\n\nA new temporary ID is used for the ad group, since we can't reuse the `-1` that\nwe used for the campaign. We also reference this ad group when creating an ad\ngroup ad. The ad group itself references the resource name we established for\nthe campaign in an earlier operation in the request, while `resource_name` in\n`ad_group_ad_operation` is not necessary since no further operation is\nreferencing it."]]