Updating a shopify synced model shortly after creating a record?

- I'm creating a shopify app that allows users to create and manage shopify sellingPlans, although my issue isn't exclusive to selling plans. - When clicking 'create' in the UI, the selling plan is created through the Shopify Admin API in a global action. - The selling plans sync back to gadget using built in webhooks. - The UI gives the option to add additional fields that don't exist on the native shopify selling plan fields (eg. Enabled / Disabled state) so I've added these additional fields on the data model inside gadget. My intention was to create the sellingPlan through the admin API, when the response returns, find the record in gadget and populate the custom field all in one UI action.
const shopifyResp = await shopify.graphql(...);
const gadgetSellingPlanResp = await api.shopifySellingPlan.findOne(shopifyResp.id);
const shopifyResp = await shopify.graphql(...);
const gadgetSellingPlanResp = await api.shopifySellingPlan.findOne(shopifyResp.id);
However just because the admin api returned a successful response, doesn't mean the webhook has fired and gadget has created the record. So after a successful response when I try to find the sellingPlan in gadget with the specific ID, its not there. So I'm wondering if theres a way around this please? I'm hoping I'm missing something obvious. Potential solutions - I've not touched background actions yet but not sure if thats the right approach. - I could move the custom field into a 2nd UI, which appears after the selling plan has been created, so a user creates a selling plan, and then as part of another UI step populates the custom field - I was thinking I could also push the sellingPlan id and the value for the custom fields into a temporary custom data model (eg: pending_custom_fields) and then in sellingPlan create onSuccess (assuming this fires when the webhook creates records), query the data model and check if any any records match the selling plan ids, populate the fields and delete the record from the temporary data model when complete, but this seems a massive long way around. Any help would be appreciated, thanks
5 Replies
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
Simon
Simon•11mo ago
Your 3rd option sounds best I guess - custom temporary model. Background actions will still not guarantee that the record has been created. J As you say, use the sellingPlan model's CREATE action, which fires once that record is created in gadget (via webhook or sync). In the onSuccess function there you can go to your "temporary" model and look for the respective data to merge it. A 2nd screen might still not guarantee that the webhook has fired. Probably rare, but if it fails, your temporary model would still do the job. Or last option: - Just have a linked model (belongsTo sellingPlan). There you keep all your custom options. It will automatically be linked the moment the sellingPlan is created
OllieEvans
OllieEvansOP•11mo ago
I've just tried the 3rd solution, however you can't call .update on the shopifySellingPlan inside gadget as its deprecated as there is no trigger to update the sellingPlan ... even though im not interested in updating the actual sellingPlan itself just the gadget field. So it looks like @Simon's 4th option might be the best solution, unless I hit some blockers on that too. I have just needed to add triggers: {api: true;} to the update action
Unknown User
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
OllieEvans
OllieEvansOP•11mo ago
Yeah sorry, I realised this after I replied! 🙈 Interesting, so I'm actually looking to add data to the sellingPlanGroupProductVariant model which isn't returned from the original API call unfortunately. After adding the { trigger: { api: true } } I've been able to update that record now, and will make sure access controls are in place thank you.

Did you find this page helpful?