# Problem connect a campaign price list to a discount using API

**URL:** <https://forum.litium.com/t/problem-connect-a-campaign-price-list-to-a-discount-using-api/2935>\
**Category:** Questions\
**Tags:** litium-8\
**Created:** [May 5, 2023, 9:25am UTC](https://forum.litium.com/t/problem-connect-a-campaign-price-list-to-a-discount-using-api/2935 "2023-05-05T09:25:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![henrik.ryden](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@henrik.ryden](https://forum.litium.com/u/henrik.ryden)\
**Post date:** [May 5, 2023, 9:25am UTC](https://forum.litium.com/t/problem-connect-a-campaign-price-list-to-a-discount-using-api/2935/1 "2023-05-05T09:25:46Z")

</div>

We are setting up discounts using the .net API, in Litium 8.7.2. We are using the price list service to create a campaign price list that we are adding the discounted products to.  
`
priceList = new CampaignPriceList();
priceList.Id = campaignImport.CampaignId;
priceList.CurrencySystemId = standardPriceList.CurrencySystemId;
priceList.Localizations.CurrentCulture.Name = campaignImport.Name;`

`
`

`_priceListService.Create(priceList);
`  
We then create a discount of the type `DiscountedProductPrice` where we pass along a `DicountedProdctPrice` object to the constructor, that has the price list id of the price list we just created.

`
var discountedProductPrice = new DiscountedProductPrice
{
    PriceListSystemIds = new List { priceList.SystemId }
};
var channel = _channelService.Get(_defaultChannelName);
var discount = new Discount(discountedProductPrice)
{
Id = campaignImport.CampaignId,
ChannelLinks = new List
{
new(channel.SystemId)
{
StartDateTimeUtc = campaignImport.StartDate,
EndDateTimeUtc = campaignImport.EndDate
}
}
};
discount.Localizations.CurrentCulture.Name = campaignImport.Name;
`

`_discountService.Create(discount);
`  
Both the discount and price list are created successfully. However, when you open the discount in the backoffice, the price list is not associated to the discount and we need to add it manually on the discount using the discount edit page under Sales → Dicsounts → [Discount name]…

If you do this step manually everything works. Should this not be done automaticlly when creating the pricelist and discount using the API and pass along the id of the price list to the constructor of the discount? Do we need to do an additional step to get the price list connected or is it some kind of timing problem where the price list does not exist yet or is it a bug?

Litium version: 8.7.2

---

<div class="post-metadata">

**Author:** ![Ericsj11](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/ericsj11/32/899_2.png) [@Ericsj11](https://forum.litium.com/u/Ericsj11)\
**Post date:** [May 5, 2023, 6:40pm UTC](https://forum.litium.com/t/problem-connect-a-campaign-price-list-to-a-discount-using-api/2935/2 "2023-05-05T18:40:48Z")

</div>

Yea it’s a “bug”, the Variants field in DiscountedProductPrice has to be set and can’t be null.  
So it should work if you change the variable discountedProductPrice to this:

```
    var discountedProductPrice = new DiscountedProductPrice 
    {
        PriceListSystemIds = new List<Guid> { priceList.SystemId },
        Variants = new Dictionary<Guid, IDictionary<Guid, decimal>>() 
    };

```

---

<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:** [June 2, 2023, 6:41pm UTC](https://forum.litium.com/t/problem-connect-a-campaign-price-list-to-a-discount-using-api/2935/3 "2023-06-02T18:41:33Z")

</div>

Closed on June 2, 2023, 6:41pm UTC

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