Adding sales order details to sales order in dynamics crm - c#

I am working to create a sales order with a single product added to the sales order detail and attach that to the sales order.
It is throwing me an error and I am wondering if there is a proper way to performing this action?
Thanks!
public void Create(CrmContextCore _crmContext, Guid productId, UserEntityModel currentuser)
{
var detail = new Entity("salesorderdetail");
{
detail["productid"] = new EntityReference("product", productId);
}
var salesorder = new Entity("salesorder");
{
salesorder["accountid"] = new EntityReference("account", currentuser.AccountId);
salesorder["contactid"] = new EntityReference("contact", currentuser.ContactId );
salesorder["emailaddress"] = currentuser.Email;
salesorder["name"] = "DealerPO123";
salesorder["salesorderdetail"] = detail;
}
_crmContext.ServiceContext.AddObject(salesorder);
_crmContext.ServiceContext.SaveChanges();
}

Sample: Set negative prices in opportunities, quotes, and sales orders.
// Create the sales order.
SalesOrder order = new SalesOrder()
{
Name = "Faux Order",
DateFulfilled = new DateTime(2010, 8, 1),
PriceLevelId = new EntityReference(PriceLevel.EntityLogicalName,
_priceListId),
CustomerId = new EntityReference(Account.EntityLogicalName,
_accountId),
FreightAmount = new Money(20.0M)
};
_orderId = _serviceProxy.Create(order);
order.Id = _orderId;
// Add the product to the order with the price overriden with a
// negative value.
SalesOrderDetail orderDetail = new SalesOrderDetail()
{
ProductId = new EntityReference(Product.EntityLogicalName,
_product1Id),
Quantity = 4,
SalesOrderId = order.ToEntityReference(),
IsPriceOverridden = true,
PricePerUnit = new Money(-40.0M),
UoMId = new EntityReference(UoM.EntityLogicalName,
_defaultUnitId)
};
_orderDetailId = _serviceProxy.Create(orderDetail);

Related

Invoicer PDF gives null but Membership does not give PDF null

My problem as it is right now. It is that I must have made such that a customer can buy an item that is only paid once. Thus assigned Invoice id and PDf to the database.
As it is right now I only get hold of Invoice id while PDF is null.
I've read a little more about this.
Invoice Id return with null after change using Stripe
var options = new ProductCreateOptions
{
Name = "Starter Setup",
};
var service = new ProductService();
var product = service.Create(options);
var optionsA = new PriceCreateOptions
{
Product = product.Id,
UnitAmount = 2000,
Currency = "usd",
};
var serviceA = new PriceService();
var price = serviceA.Create(optionsA);
var optionsB = new CustomerCreateOptions
{
Email = model.Mail,
Name = model.FuldName,
Source = token
};
var serviceB = new CustomerService();
var customer = serviceB.Create(optionsB);
var optionsC = new InvoiceItemCreateOptions
{
Customer = customer.Id,
Price = price.Id,
};
var serviceC = new InvoiceItemService();
var invoiceItem = serviceC.Create(optionsC);
var invoiceId = invoiceItem.Id;
var serviceE = new InvoiceService();
var f = serviceE.Get(invoiceId);
var pdf = f.InvoicePdf;// This here gives zero.
If I do it this way, I'll get this out of it. I get the Invoice ID that I want here but I get nothing on the invoice that shows that it is zero.
{
"id": "ii_1IR4UtFnB7TvDVRrzPwWo8ZW",
"object": "invoiceitem",
"amount": 2000,
"currency": "usd",
"customer": "cus_J3Aqpyt4PwqCcN",
"date": 1614815575,
"description": "Starter Setup",
"discountable": true,
"discounts": [
],
"invoice": null,
"livemode": false,
"metadata": {
},
....
}
With this, my thinking is whether I will in a way be able to make such that I make a membership which then stops immediately but that it says in the invoice that the purchase is only of a single item and not several months.
The way I have done it in relation to membership I have done like this.
var createCustomer = new CustomerCreateOptions
{
Source = token,
Name = model.FuldName,
Email = model.Mail
};
var addService = new CustomerService();
var customer = addService.Create(createCustomer);
var optionsProduct = new ProductCreateOptions
{
Name = $"Single buy - {DateTime.Now} - Kursus Id : {id}",
Type = "service",
};
var serviceProduct = new ProductService();
Product product = serviceProduct.Create(optionsProduct);
var optionsPlan = new PlanCreateOptions
{
Currency = "dkk",
Interval = Helpers.Stripe.interval,
Nickname =
$"Single buy - {DateTime.Now} - Kursus Id : {id}",
Amount = amount,
Product = product.Id,
IntervalCount = 1
};
var servicePlan = new PlanService();
Plan plan = servicePlan.Create(optionsPlan);
var items = new List<SubscriptionItemOptions>()
{
new SubscriptionItemOptions()
{
Plan = plan.Id,
Quantity = 1
},
};
var createSubscruptionA = new SubscriptionCreateOptions
{
Customer = customer.Id,
Items = items,
OffSession = true,
};
var addserviceA = new SubscriptionService();
Subscription subscription = addserviceA.Create(createSubscruptionA);
var invoiceId = subscription.LatestInvoiceId;
var service = new InvoiceService();
var pdf = service.Get(invoiceId).InvoicePdf;
That which I would like to achieve by this. It is that I can get hold of PDF and Invoice id as I will use it for my system in the future etc.
EDIT
var optionsB = new CustomerCreateOptions
{
Email = model.Mail,
Name = model.FuldName,
Source = token
};
var serviceB = new CustomerService();
var customer = serviceB.Create(optionsB);
var optionsC = new InvoiceItemCreateOptions
{
Customer = customer.Id,
Price = price.Id,
};
var serviceC = new InvoiceItemService();
var invoiceItem = serviceC.Create(optionsC);
var invoiceId = invoiceItem.Id;
var invoiceOptions = new InvoiceCreateOptions
{
Customer = customer.Id,
AutoAdvance = true,
};
var invoiceService = new InvoiceService();
var invoice = invoiceService.Create(invoiceOptions);
For one-off Invoices you need to create Invoice Items for the Customer (as you've done), but importantly you then need to create an Invoice that will contain those Items.
This line is not correct for what you're trying to accomplish:
var invoiceId = invoiceItem.Id;
Instead, you need to create the invoice as shown in the docs linked above:
var invoiceOptions = new InvoiceCreateOptions
{
Customer = "cus_123",
AutoAdvance = true,
};
var invoiceService = new InvoiceService();
var invoice = invoiceService.Create(invoiceOptions);
The Invoice object will have an invoice_pdf URL (docs) after you finalize it.
var service = new InvoiceService();
service.FinalizeInvoice(
"in_123"
);

API to stripe make 500

That's when i need to use the Stripe API so when i need it, it will go wrong and make mistakes in the Stripe area as you can see here.
i have : v15.6.1 on Stripe.net
Where it goes wrong is here:
planservice.Create(new StripePlanCreateOptions()
to here:
PlanId = abn.PriceValueUnikId };
all the value I get by json eg userid, pric and pricId there is content in them.
[HttpPost]
public IActionResult Post([FromBody] JObject token)
{
var api = Settings.ConstName.StrinpAPIKeyTest;
StripeConfiguration.SetApiKey(api);
var chargeService = new StripeChargeService();
chargeService.ExpandBalanceTransaction = true;
chargeService.ExpandCustomer = true;
chargeService.ExpandInvoice = true;
//StripeCharge stripeCharge = chargeService.Get(api);
var customerSerive = new StripeCustomerService(api);
var subservice = new StripeSubscriptionService(api);
var planservice = new StripePlanService(api);
var pricId = (int)token.GetValue("pricid");
var pric = (int)token.GetValue("pric");
var userid = (int) Userid();
var abn = _dbContext.PriceValue.FirstOrDefault(i => i.PriceValueId == pricId || i.Price == pric);
//Finder information omkring pakken til den enkelte pakke.
var currentUser = _dbContext.Users.FirstOrDefault(i => i.UserId == userid);
if (currentUser != null)
{
if (abn != null)
{
var orderid = Settings.ValueWordsAndNumbers.OrdreValue();//Orderid
var planType = $"OrderId: {orderid} - Pris: {abn.Price} - Mdr: {abn.Months} UserId: {userid}";
planservice.Create(new StripePlanCreateOptions()//error from here
{
Amount = int.Parse(abn.Price.ToString()) * 100,
Nickname = planType,
Currency = "dkk",
Interval = "month",
IntervalCount = abn.Months,
Id = abn.PriceValueUnikId
});
var newCustomer = new StripeCustomerCreateOptions
{
SourceToken = token["id"].ToString(),
Email = token["email"].ToString(),
PlanId = abn.PriceValueUnikId,
};//error to here
var stripeCustomer = customerSerive.Create(newCustomer);
}
}
var planOptions = new StripePlanCreateOptions() {
Product = new StripePlanProductCreateOptions() {
Name = "planType"
},
Amount = int.Parse(abn.Price.ToString()) * 100,
Nickname = planType,
Currency = "dkk",
Interval = "month",
IntervalCount = abn.Months,
};
var planService = new StripePlanService();
StripePlan plan = planService.Create(planOptions);
API version to 2018-02-06 and add support for Product & Plan API
Now Product is REQUIRED.
you need past ID product or dictionary containing fields used to create a service product.
var planOptions = new StripePlanCreateOptions() {
ProductId ="Product Plan id",
Amount = int.Parse(abn.Price.ToString()) * 100,
Nickname = planType,
Currency = "dkk",
Interval = "month",
IntervalCount = abn.Months,
};

Entity Framework 6 Many to many

I'm trying to learn to use Entity Framework and am working through the Entity Framework Recipes 6 Book (not even going to try and hide that).
Working on 2-4:
3 tables:
Order: OrderID, OrderDate, (Nav Properties OrderItems)
OrderItem: OrderId, SKU, Count (Nav Properties, Item, Order)
Item: SKU, Description, Price (Nav Properties, OrderItems)
using (var context = new EFRecipesContext()) {
var order = new Order { OrderId = 1, OrderDate = new DateTime(2010, 1, 18) };
var item = new Item { SKU = 1729, Description = "Backpack", Price = 29.97M };
var oi = new OrderItem { Order = order, Item = item, Count = 1 };
item = new Item { SKU = 2929, Description = "Water Filter", Price = 13.97M };
oi = new OrderItem { Order = order, Item = item, Count = 3 };
item = new Item { SKU = 1847, Description = "Camp Stove", Price = 43.99M };
oi = new OrderItem { Order = order, Item = item, Count = 1 };
context.Orders.Add(order);
context.SaveChanges();
}
The only thing that gets added to the database is the order and nothing else, none of the items, etc... I had a problem with the previous example and had to add the "other" items individually but I thought that point was that you could just do the one "Add" and it would add all the objects to the Database?
Thanks in advance!
UPDATEOk I made the following changes based on the suggestions below and now it's working
using (var context = new EFRecipesContext()) {
var order = new Order { OrderId = 1, OrderDate = new DateTime(2010, 1, 18) };
var item = new Item { SKU = 1729, Description = "Backpack", Price = 29.97M };
var oi = new OrderItem { Order = order, Item = item, Count = 1 };
order.OrderItems.Add(oi); // suggestion from Stackoverflow.
item = new Item { SKU = 2929, Description = "Water Filter", Price = 13.97M };
oi = new OrderItem { Order = order, Item = item, Count = 3 };
order.OrderItems.Add(oi); // suggestion from Stackoverflow.
item = new Item { SKU = 1847, Description = "Camp Stove", Price = 43.99M };
oi = new OrderItem { Order = order, Item = item, Count = 1 };
order.OrderItems.Add(oi); // suggestion from Stackoverflow.
context.Orders.Add(order);
context.SaveChanges();
}
At least now I know what to change in their code going forward to get it working. Thanks!!
You never actually add the OrderItems to the DbContext collection.
There is also a problem with your code, which means you are overwriting your items and oi variable values multiple times.
The below code makes some assumptions, since you haven't supplied more of your code, but should be easy to adapt.
using (var context = new EFRecipesContext()) {
var order = new Order { OrderId = 1, OrderDate = new DateTime(2010, 1, 18) };
context.Orders.Add(order);
var item1 = new Item { SKU = 1729, Description = "Backpack", Price = 29.97M };
var oi1 = new OrderItem { Order = order, Item = item, Count = 1 };
context.OrderItems.Add(oi1);
var item2 = new Item { SKU = 2929, Description = "Water Filter", Price = 13.97M };
var oi2 = new OrderItem { Order = order, Item = item, Count = 3 };
context.OrderItems.Add(oi2);
var item3 = new Item { SKU = 1847, Description = "Camp Stove", Price = 43.99M };
var oi3 = new OrderItem { Order = order, Item = item, Count = 1 };
context.OrderItems.Add(oi3);
context.SaveChanges();
}
Unless you are using the variables again, you could do the definition of the objects inline like this aswell, which is slightly less repetitive:
context.OrderItems.Add(new OrderItem {
Order = order,
Item = new Item {
SKU = 1729,
Description = "Backpack",
Price = 29.97M
},
Count = 1
});
This is not really about many to many, but about the way how EF discovers related data.
You need to explicitely add the entry to the database, that is actually referencing the related data. In your case, this is oi, so if you add it
context.OrderItems.Add(oi);
context.SaveChanges();
the related io.Order and io.Item will be considered by EF. But EF has no way of knowing about this relation when you only provide order without providing navigation information from order to io.
Another way of solving the issue (instead of adding io explicitely) would be to update order.OrderItems before saving:
oi = new OrderItem { Order = order, Item = item, Count = 1 };
order.OrderItems.Add(oi);
context.Orders.Add(order);
context.SaveChanges();
You must add an entry to the database to be invoked on appropriate data. In this case, it's the OI.

vTiger .net C# sales order fails - MANDATORY_FIELDS_MISSING “message”:"

Throwing an exception when I trying to create sales order in VTiger version 6.15.7.6
Sending the following entity
for Sales order creation
VTigerListItem[] arrListItem = new VTigerListItem[1];
arrListItem[0] = new VTigerListItem() { productid = "25x405", listprice = "468.25", quantity = "1" };
VTigerSalesOrder[] arrListItem1 = new VTigerSalesOrder[1];
arrListItem1[0] = new VTigerSalesOrder()
{
start_period = "2015-01-01",
end_period = "2015-01-01",
subject = "Testing",
contact_id = "4x680",
sostatus = SoStatus.Created,
account_id = "3x679",
assigned_user_id = "19x11",
bill_street = "test bill street",
invoicestatus = Invoicestatus.Created,
productid = "25x405",
cf_1121 = "RDMS",
createdtime = "2015-01-01",
modifiedtime = "2015-01-01",
LineItems = arrListItem
};
var desc = vtigerApi.Create(arrListItem1[0]);
Please check the Contactid , Accountid and Productid.
Module ids are given wrong.
Contacts start with 12X,
Accounts start with 11X,
Products start with 14X,
assign_user_id if it is assigning to User then 19X, if it is assigning to Group then 20X. Also change contact_id to contactid,account_id to accountid.

null Amount value salesorderdetail on crm 2011-Silverlight

I'm trying to insert data on SalesOrderDetail entity, everything works fine, except for the Amount field which remains null. I don't get any error message.
Here is an example of my code :
private void beginCreateSalesOrderDetail()
{
SalesOrderDetail orderDetail = new SalesOrderDetail();
orderDetail.SalesOrderId = new EntityReference()
{
Id = id,
LogicalName = "salesorder"
};
orderDetail.Quantity = element.QuantityOnHand;
orderDetail.ProductId = new EntityReference()
{
Id = element.ProductId,
LogicalName = "product"
};
orderDetail.UoMId = new EntityReference()
{
Id = new Guid("8DDD2AFB-73CF-E111-8140-00155D55B216"),
LogicalName = "uom"
};
orderDetail.TransactionCurrencyId = new EntityReference()
{
Id = new Guid("77D695B5-ACB4-E111-97BC-00155D55B216"),
LogicalName = "transactioncurrency"
};
Money Taxe = new Money();
Money Amount = new Money();
Taxe.Value = Convert.ToDecimal(element.totalCharges);
Amount.Value = Convert.ToDecimal(InvoiceTotal);
orderDetail.Tax = Taxe;
orderDetail.BaseAmount = Amount;
orderDetail.PricePerUnit = element.Price;
orderDetail.Description = element.PDesc;
_context.AddToSalesOrderDetailSet(orderDetail);
_context.BeginSaveChanges(EndCreateSalesOrderDetail, orderDetail);
}
private void EndCreateSalesOrderDetail(IAsyncResult result)
{
try
{
_context.EndSaveChanges(result);
}
catch (Exception ex)
{
}
}
Please note that only the amount which remains null
I get the solution.
Actually, there is a restrictions in crm 2011 to calcul the amounts of products,
We have to create a Price List, and associate each product in the list.
Thank you.

Categories

Resources