What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Save PriceRule does not return created object

Solved

Save PriceRule does not return created object

sancuscommerce
Shopify Partner
17 1 3
I am attempting to create a price rule using admin.rest.resource. the documentation states that the response will contain the created price rule. however, when I attempt to retrieve the response, it is returning undefined. 
 
 
const price_rule = new admin.rest.resources.PriceRule({session: session});
price_rule.title = "TEST";
price_rule.target_type = "line_item";
price_rule.target_selection = "all";
price_rule.allocation_method = "across";
price_rule.value_type = "fixed_amount";
price_rule.value = "-10.0";
price_rule.customer_selection = "all";
price_rule.starts_at = "2018-03-22T00:00:00-00:00";

const priceResponse = await price_rule.save({
update: true,
});
console.log('priceResponse:'+JSON.stringify(priceResponse));
 
I have also tried just logging the response without using a variable:
console.log('save:'+JSON.stringify(await price_rule.save({
update: true,
})));
 
it also returns undefined.
 
Both of these will create the price rule and I can retrieve it if i list all the price rules:
console.log('price rule list:'+JSON.stringify(await admin.rest.resources.PriceRule.all({
session: session,
})));
 
How do I get the id of the created price rule so I can use it in creating a discount code? I don't want to query the price rules every time i want to create a discount and loop over those trying to find my newly created price rule.
 
Thanks
Accepted Solution (1)

sancuscommerce
Shopify Partner
17 1 3

This is an accepted solution.

I found the solution. I don't need to assign the await to a new variable. the update:true will update the price_rule variable with the newly created id.

 

This works and returns an id i can pass into the create discount code:

const price_rule = new admin.rest.resources.PriceRule({session: session});
price_rule.title = "Price Rule Title";
price_rule.target_type = "line_item";
price_rule.target_selection = "all";
price_rule.allocation_method = "across";
price_rule.value_type = "fixed_amount";
price_rule.value = "-10.0";
price_rule.customer_selection = "all";
price_rule.starts_at = "2018-03-22T00:00:00-00:00";

await price_rule.save({
update: true,
});
console.log('price_rule:'+JSON.stringify(price_rule));

View solution in original post

Replies 2 (2)

sancuscommerce
Shopify Partner
17 1 3

This is an accepted solution.

I found the solution. I don't need to assign the await to a new variable. the update:true will update the price_rule variable with the newly created id.

 

This works and returns an id i can pass into the create discount code:

const price_rule = new admin.rest.resources.PriceRule({session: session});
price_rule.title = "Price Rule Title";
price_rule.target_type = "line_item";
price_rule.target_selection = "all";
price_rule.allocation_method = "across";
price_rule.value_type = "fixed_amount";
price_rule.value = "-10.0";
price_rule.customer_selection = "all";
price_rule.starts_at = "2018-03-22T00:00:00-00:00";

await price_rule.save({
update: true,
});
console.log('price_rule:'+JSON.stringify(price_rule));
Liam
Community Manager
3108 344 895

Glad you figured this out and thanks for coming back to post your solution!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog