# Using ProductPriceModelBuilder in scheduled task

**URL:** <https://forum.litium.com/t/using-productpricemodelbuilder-in-scheduled-task/2773>\
**Category:** Questions\
**Tags:** accelerator, litium-8\
**Created:** [November 30, 2022, 1:18pm UTC](https://forum.litium.com/t/using-productpricemodelbuilder-in-scheduled-task/2773 "2022-11-30T13:18:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kdxperbol](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/kdxperbol/32/513_2.png) [@kdxperbol](https://forum.litium.com/u/kdxperbol)\
**Post date:** [November 30, 2022, 1:18pm UTC](https://forum.litium.com/t/using-productpricemodelbuilder-in-scheduled-task/2773/1 "2022-11-30T13:18:37Z")

</div>

We need to calculate the price (list & campaign) for a variant in a scheduled task, but the `ProductPriceModelBuilder` throws an exception because it “Cant fetch OrderCarrier from users session” (sic) which sounds like it needs a user context.

Is there a way to use the `ProductPriceModelBuilder` without a user context, e.g., from a scheduled task?

Or is there something other than the `ProductPriceModelBuilder` that can calculate the list & campaign price for a variant with a user context?

Litium version: 8.6.1

---

<div class="post-metadata">

**Author:** ![utku](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/utku/32/62_2.png) [@utku](https://forum.litium.com/u/utku)\
**Post date:** [December 5, 2022, 8:23am UTC](https://forum.litium.com/t/using-productpricemodelbuilder-in-scheduled-task/2773/2 "2022-12-05T08:23:41Z")

</div>

ProductPriceModelBuilder is using the user context. You have to use directly IPriceCaclulator and IDiscountPriceCalculator.

---

<div class="post-metadata">

**Author:** ![kdxperbol](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/kdxperbol/32/513_2.png) [@kdxperbol](https://forum.litium.com/u/kdxperbol)\
**Post date:** [December 5, 2022, 8:26am UTC](https://forum.litium.com/t/using-productpricemodelbuilder-in-scheduled-task/2773/3 "2022-12-05T08:26:44Z")

</div>

We have tried to use the `ICampaignPriceCalculator` (this is Litium 7), but it doesn’t seem to be able to calculate any campaign prices. The list of campaign prices it calculates is always empty.

We are using the accelerator `ICampaignPriceCalculator` implementation.

But it’s supposed to work?

---

<div class="post-metadata">

**Author:** ![utku](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/utku/32/62_2.png) [@utku](https://forum.litium.com/u/utku)\
**Post date:** [December 5, 2022, 8:54am UTC](https://forum.litium.com/t/using-productpricemodelbuilder-in-scheduled-task/2773/4 "2022-12-05T08:54:20Z")

</div>

Yes, we are using it when we are creating the price agent feeds via scheduled jobs. Be sure that your CampaignPriceCalculatorArgs is correct.

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [December 5, 2022, 9:53am UTC](https://forum.litium.com/t/using-productpricemodelbuilder-in-scheduled-task/2773/5 "2022-12-05T09:53:09Z")

</div>

Pass in the `OrderCarrier` in the `CampaignPriceCalculatorArgs.OrderCarrier`, if that is `null` the system tries to resolve the `OrderCarrier` from the user http session.

---

<div class="post-metadata">

**Author:** ![kdxperbol](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/kdxperbol/32/513_2.png) [@kdxperbol](https://forum.litium.com/u/kdxperbol)\
**Post date:** [December 5, 2022, 11:44am UTC](https://forum.litium.com/t/using-productpricemodelbuilder-in-scheduled-task/2773/6 "2022-12-05T11:44:44Z")

</div>

Not sure what “the `OrderCarrier`” refers to. But this is what we do currently:

```auto
_orderCarrier = orderFactory.Create((OrderCarrier)null, Solution.Instance.AnonymousToken);
// ...
var priceCalculatorArgs = new CampaignPriceCalculatorArgs() {
    VariantSystemId = variant.SystemId,
    CurrencySystemId = currency.SystemId,
    DateTimeUtc = DateTime.UtcNow,
    LanguageID = _languageService.Get(cultureInfo).SystemId,
    Quantity = 1M,
    SecurityToken = SecurityToken.AnonymousToken,
    ChannelId = channel.SystemId,
    OrderCarrier = _orderCarrier,
    CountrySystemId = channelToCountryLink.CountrySystemId,
    SourceId = "scheduled task"
};
var campaignPriceResults = _campaignPriceCalculator.GetCampaignPrices(priceCalculatorArgs);

```

However, **`campaignPriceResults` is always empty when we do this**.

We have double-checked that all the calculator arguments are correct, and that product pages on the site show campaign prices.

We have created campaigns that apply to all channels and all currencies just for testing, but regardless, there are never any campaign price results returned.

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [December 5, 2022, 2:13pm UTC](https://forum.litium.com/t/using-productpricemodelbuilder-in-scheduled-task/2773/7 "2022-12-05T14:13:32Z")

</div>

Have you checked that you do not have tier prices and that the campaign that you set is a product campaign without any extra conditions.

This is the code in the price agent factory for Litium 7 that fetching the product list and campaign prices in a scheduled task.

```csharp
            var priceCalculatorArgs = new PriceCalculatorArgs
            {
                CurrencySystemId = currencyId,
                DateTimeUtc = DateTimeOffset.UtcNow,
                WebSiteSystemId = webSite.SystemId,
                CountrySystemId = priceAgentItemArgs.CountryId,
                SourceId = "price agent"
            };

            var priceCalculatorItemArgs = new PriceCalculatorItemArgs
            {
                VariantSystemId = variant.SystemId,
                Quantity = 1
            };

            var campaignPriceCalculatorArgs = new CampaignPriceCalculatorArgs
            {
                VariantSystemId = variant.SystemId,
                CurrencySystemId = currencyId,
                DateTimeUtc = DateTime.UtcNow,
                LanguageID = _languageService.Get(culture).SystemId,
                Quantity = 1,
                SecurityToken = SecurityToken.AnonymousToken,
                ChannelId = channel.SystemId,
                OrderCarrier = _orderCarrier,
                CountrySystemId = _orderCarrier.CountryID,
                SourceId = "price agent"
            };

            if (!_priceCalculator.GetListPrices(priceCalculatorArgs, priceCalculatorItemArgs)
                .TryGetValue(priceCalculatorItemArgs.VariantSystemId, out var listPrice))
            {
                return priceAgentItemResult;
            }
            
            priceAgentItemResult.PriceWithVat = listPrice.ListPriceWithVat;
            priceAgentItemResult.VatPercentage = listPrice.VatPercentage;

            if (_campaignPriceCalculator.GetCampaignPrices(campaignPriceCalculatorArgs).TryGetValue(campaignPriceCalculatorArgs.VariantSystemId, out var campaignPrice))
            {
                priceAgentItemResult.PriceWithVat = campaignPrice.CampaignPriceWithVAT;
            }

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)\
**Post date:** [January 2, 2023, 2:14pm UTC](https://forum.litium.com/t/using-productpricemodelbuilder-in-scheduled-task/2773/8 "2023-01-02T14:14:03Z")

</div>

Closed on January 2, 2023, 2:14pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
