Great Plains Find Sales Orders - microsoft-dynamics

I have created sales orders in Great Plains, however, I cannot find them in the right place in the system. Although my code executes without any errors, I do not find this transaction under Sales > All Sales Transactions. Instead, I see them under Sales > Sales Documents.
Is it in a pending state?
public void CreateOrder()
{
CompanyKey companyKey;
Context context;
SalesOrder salesOrder;
SalesDocumentTypeKey salesOrderType;
CustomerKey customerKey;
BatchKey batchKey;
SalesOrderLine salesOrderLine;
ItemKey orderedItem;
Quantity orderedAmount;
Policy salesOrderCreatePolicy;
// Create a context with which to call the service
context = new Context();
// Specify which company to use (sample company)
companyKey = new CompanyKey();
companyKey.Id = (-1);
// Set up the context object
context.OrganizationKey = (OrganizationKey)companyKey;
// Create a sales order object
salesOrder = new SalesOrder();
// Create a sales document type key for the sales order
salesOrderType = new SalesDocumentTypeKey();
salesOrderType.Type = SalesDocumentType.Order;
// Populate the document type key of the sales order object
salesOrder.DocumentTypeKey = salesOrderType;
// Create a customer key
customerKey = new CustomerKey();
customerKey.Id = "JONESJ008";
// Set the customer key property of the sales order object
salesOrder.CustomerKey = customerKey;
// Create a batch key
batchKey = new BatchKey();
batchKey.Id = "SALES ORDERS";
// Set the batch key property of the sales order object
salesOrder.BatchKey = batchKey;
// Create a sales order line to specify the ordered item
salesOrderLine = new SalesOrderLine();
// Create an item key
orderedItem = new ItemKey();
orderedItem.Id = "32X IDE";
// Set the item key property of the sales order line object
salesOrderLine.ItemKey = orderedItem;
// Create a sales order quantity object
orderedAmount = new Quantity();
orderedAmount.Value = 4;
// Set the quantity of the sales order line object
salesOrderLine.Quantity = orderedAmount;
// Create an array of sales order lines
// Initialize the array with sales order line object
SalesOrderLine[] orders = { salesOrderLine };
// Add the sales order line array to the sales order
salesOrder.Lines = orders;
// Get the create policy for the sales order object
salesOrderCreatePolicy = wsDynamicsGP.GetPolicyByOperation("CreateSalesOrder", context);
// Create the sales order
wsDynamicsGP.CreateSalesOrder(salesOrder, context, salesOrderCreatePolicy);
}
I have looked in Sales > All Sales Transactions. I can use the API to pull this order out. I am using the sample company file, Fabrikam, Inc. Here's how I set up my context:
public GPOrders()
{
wsDynamicsGP = new DynamicsGP();
// Be sure the default credentials are used
wsDynamicsGP.UseDefaultCredentials = true;
// Create a context with which to call the web service
context = new Context();
// Specify which company to use (sample company)
companyKey = new CompanyKey();
companyKey.Id = (-1);
// Set up the context object
context.OrganizationKey = (OrganizationKey)companyKey;
context.CultureName = "en-US";
}

I found them under Sales > Sales Documents. The transactions never posted, so they weren't under Sales > All Sales Transactions.

Related

update_create is creating new feilds instead of updating total

payments = list((expense
.annotate(month=Month('startDate'))
.values('month')
.annotate(total=Sum('cost'))
.order_by('month')))
for i in payments:
paymentMonths = (i["month"])
paymentTotal= (i["total"])
obj, created = Payment.objects.update_or_create(
author=curruser,
date=paymentMonths,
total = paymentTotal,
defaults={"total": paymentTotal},
)
totalcost = Payment.objects.filter(author = curruser.id)
Apparently, it should update the (date = 12) total but it is making new ones with the updated value, it might be because the totaldate is diff or maybe I'm wrong it is confusing
I believe you just need to omit total = paymentTotal from your arguments.
obj, created = Payment.objects.update_or_create(
author=curruser,
date=paymentMonths,
defaults={"total": paymentTotal},
)
This is because it is querying the DB to see if a Payment with that total already exists, and if it doesn't, it's creating a new one. Instead, you want to query Payments with curruser as the author and with date as paymentMonths, while only updating total with the new paymentTotal

How to change UnitPrice via Dynamics GP API

I would like to change UnitPrice when create an order via Dynamics GP API,
So I run next code:
// Create a sales order object
var salesOrder = new SalesOrder();
// Create a sales document type key for the sales order
var salesOrderType = new SalesDocumentTypeKey();
salesOrderType.Type = SalesDocumentType.Order;
// Populate the document type key of the sales order object
salesOrder.DocumentTypeKey = salesOrderType;
// Create a customer key
var customerKey = new CustomerKey();
customerKey.Id = customerSummary[0].Key.Id;
// Set the customer key property of the sales order object
salesOrder.CustomerKey = customerKey;
// Create a batch key
var batchKey = new BatchKey();
batchKey.Id = "01-INBOUND";
// Set the batch key property of the sales order object
salesOrder.BatchKey = batchKey;
// Create a sales order line to specify the ordered item
var salesOrderLine = new SalesOrderLine();
// Create an item key
var orderedItem = new ItemKey();
orderedItem.Id = "AA1111";
// Set the item key property of the sales order line object
salesOrderLine.ItemKey = orderedItem;
// Create a sales order quantity object
var orderedAmount = new Quantity();
orderedAmount.Value = 1;
// Set the quantity of the sales order line object
salesOrderLine.Quantity = orderedAmount;
// THIS DOESN'T WORK
salesOrderLine.UnitPrice = new MoneyAmount { Value = 1.11m, Currency = "USD" };
// Discount works ok
// salesOrderLine.Discount = new MoneyPercentChoice { Item = new MoneyAmount { Value = 1.25m } };
// Create an array of sales order lines
// Initialize the array with sales order line object
SalesOrderLine[] orders = { salesOrderLine };
// Add the sales order line array to the sales order
salesOrder.Lines = orders;
// Get the create policy for the sales order object
var salesOrderCreatePolicy = dynamicsGP.GetPolicyByOperation("CreateSalesOrder", context);
// Create the sales order
dynamicsGP.CreateSalesOrder(salesOrder, context, salesOrderCreatePolicy);
But line to change UnitPrice doesn't work.
salesOrderLine.UnitPrice = new MoneyAmount { Value = 1.11m, Currency = "USD" };
What I'm doing wrong? Appreciate any help.
Thank you!
Found solution:
In Dynamics GP Web Service Policy Configuration for Update Sales
Invoice Policy, you need to update the “Calculate Unit Price Behavior”
to “Do Not Calculate”.
Also, make Sure that you have selected the right Company and assigned
proper roles for the policy. The role should not be Default.
Source: https://www.cloudfronts.com/dynamics-gp-sales-invoice-unit-price-always-0/

System.ListException: Before Insert or Upsert list must not have two identically equal elements

I am receiving this error upon inserting new records executed by a trigger:
"System.ListException: Before Insert or Upsert list must not have two identically equal elements"
Below is trigger:
trigger createCETAccount on Account (before update) {
//trigger runs after the update of an Account record
//create a set of all the unique Account IDs
Set<id> AIds = new Set<id>();
//for each instance of the trigger being fired if the Contact field is not blank
//and the Contact Exact Target Send field is not blank and has changed from what it was previously - add that record's ID to the MTIds Set
for (Integer i = 0; i < Trigger.new.size(); i++)
{
if(Trigger.new[i].Contact_Exact_Target_Send__c != Trigger.old[i].Contact_Exact_Target_Send__c && Trigger.new[i].Contact_Exact_Target_Send__c != null)
{
AIds.add(Trigger.new[i].Id);
}
}
//query for all the Account records for the unique AIds in the records
List <Account> aList = new List<Account> ([Select Id, Contact_Exact_Target_Send__c from Account where Id in :AIds]);
//List all the Contacts associated with the Account
List <Contact> ContactList = new List <Contact> ([Select Id, AccountId, isPrimary__c from Contact where AccountId in :AIds ORDER BY isPrimary__c ASC, createdDate DESC]);
//Create a list of ContactExactTarget records
List <ContactExactTarget__c> CETList = new List<ContactExactTarget__c> ([Select ID, Account__c from ContactExactTarget__c where Account__c in :AIds]);
List <ContactExactTarget__c> CETList2 = new List<ContactExactTarget__c>;
//iterate over the list of Account records being processed in the trigger
for(Account acc: aList){
//create a new ContactExactTarget send record for each instance
ContactExactTarget__c objCET = new ContactExactTarget__c();
objCET.ET_Code__c = acc.Contact_Exact_Target_Send__c;
objCET.Account__c = acc.Id;
//add the record to the CETList defined above
CETList.add(objCET);
}
//add the related Contact to the CET record
for(Contact con : ContactList) {
for (ContactExactTarget__c cet : CETList){
if (con.AccountId == cet.Account__c)
cet.Contact__c = con.Id;
CETList2.add(cet);
}
}
//upsert the list to insert the new records
upsert CETList2;
}

What is the preferred method of updating Money fields in Dynamics CRM 2013 via Web Services?

I have an entity in CRM that has some Money fields on them. At the time of the creation of the entity there is no value for those fields, so the entity is created and saved. However if I then have values and go to update them (either through the UI or Web Services) I basically get an error stating "The currency cannot be null".
In the UI:
I then get a red circle with an X through it if I go to update the value.
In the code (web service call):
var crmService = new CrmServiceReference.IFAAContext(new Uri(crmWebServicesUrl));
crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
var crmAccount = crmService.AccountSet.Where(a => a.AccountId == accountId).FirstOrDefault();
crmAccount.myitems_MoneyValueItem.Value = 10.0m;
crmService.UpdateObject(crmAccount);
crmService.SaveChanges()
I then get the "The currency cannot be null" on the .SaveChanges() call.
In the code (second attempt):
var crmService = new CrmServiceReference.IFAAContext(new Uri(crmWebServicesUrl));
crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
var crmAccount = crmService.AccountSet.Where(a => a.AccountId == accountId).FirstOrDefault();
var crmCurrency = crmService.TransactionCurrencySet.Where(c => c.ISOCurrencyCode == "AUD").First();
crmService.SetLink(crmAccount, "TransactionCurrency_Account", crmCurrency);
//crmService.SaveChanges() /* tried with this uncommented as well to try and "set currency" before setting value */
crmAccount.myitems_MoneyValueItem.Value = 10.0m;
crmService.UpdateObject(crmAccount);
crmService.SaveChanges()
So this attempt fails in the same way, however if I run it a second time (without the currency and SetLink) so that the currency was saved before then it does work - hence the atempt to do a second .SaveChanges() but really need it to sort of work the first time through.
In the code (third attempt):
var crmService = new CrmServiceReference.IFAAContext(new Uri(crmWebServicesUrl));
crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
var crmAccount = crmService.AccountSet.Where(a => a.AccountId == accountId).FirstOrDefault();
crmAccount.athos_MoneyValueItem= new CrmServiceReference.Money() { Value = 10.0m };
crmService.UpdateObject(crmAccount);
crmService.SaveChanges()
This doesn't appear to work either
When you create a new record containing a Money field (or updating an existing one where no Money fields are filled before) , you need to specify the currency, the right field to set is TransactionCurrencyId (logical name transactioncurrencyid), it's a lookup (so inside the code is an EntityReference) to the currency entity.
assuming you have the Guid of your currency stored inside the currencyId variable:
var crmAccount = crmService.AccountSet.Where(a => a.AccountId == accountId).FirstOrDefault();
crmAccount.TransactionCurrencyId = new EntityReference(TransactionCurrency.LogicalName, currencyId);
crmAccount.myitems_MoneyValueItem = new Money(10.0m); //better to update the money field with this syntax
crmService.UpdateObject(crmAccount);
crmService.SaveChanges()
CRM 2013 expects a Money object for a currency field.
crmAccount.myitems_MoneyValueItem = new Money(10.0m);
You shouldn't have to explicitly declare a currency unless your CRM organization doesn't have a default currency set (and since you need to set this when creating an organization, it should always be set).
It looks like if I do a lookup on currency (shame as an extra data call) and then use that to set the TransactionCurrencyId it then "works" ... seems like I shouldn't have to do this as I do have a default set for the organisation though. But this does appear to be working in that it results in being able to update those fields.
var crmCurrency = crmService.TransactionCurrencySet.Where(c => c.ISOCurrencyCode == currencyCode).First();
var crmService = new CrmServiceReference.IFAAContext(new Uri(crmWebServicesUrl));
crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
var crmAccount = crmService.AccountSet.Where(a => a.AccountId == accountId).FirstOrDefault();
crmAccount.TransactionCurrencyId = new CrmServiceReference.EntityReference() { Id = crmCurrency.TransactionCurrencyId, LogicalName = "transactioncurrency", Name = crmCurrency.CurrencyName };
crmAccount.myitems_MoneyValueItem.Value = 10.0m;
crmService.UpdateObject(crmAccount);
crmService.SaveChanges();
i am doing the same thing as you but unable to set the money field value during service.create.
I have already put in
entity.Attributes["transactioncurrencyid"] = currencyGuid;
entity.Attributes["new_moneyfield"] = new Money(123.45M);
but in the end, the record is created with the transactioncurrency field populated, while the money field is blank.
Its a custom workflow on CRM online 2016 by the way..

List of instances with minimal date of their group

I'm working on a Java project, using Hibernate to administrate data on a SQL database.
I try to fetch a list of instances from the Database, that have a minimal timestamp of the group they share. The group is modeled by a container.
Here is a minimal model sketch:
#Entity
#Table(name = "object")
public class Object implements Serializable{
#Id
#GeneratedValue(strategy = GenerationType.Auto)
long obj_id;
#Column(name = "time_stamp", nullable = false)
Date timestamp;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "container_id", nullable = false)
Container con;
}
#Entity
#Table(name = "container")
public class Container{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
long con_id;
#OneToMany(mappedBy = "container")
List<object> obj_list;
}
So there are some objects with a timestamp and containers that group these objects.
For example, there are two containers, con_a and con_b:
Container con_a:
con_id = 1
obj_list = {obj_a, obj_b}
Container con_b:
con_id = 2
obj_list = {obj_c}
And three objects, obj_a, obj_b, obj_c:
Object obj_a
obj_id = 1
timestamp = 10
con = con_a
Object obj_b
obj_id = 2
timestamp = 20
con = con_a
Object obj_c
obj_id = 3
timestamp = 30
con = con_b
The desired List in this example would look like:
List<Object> = {obj_a, obj_c}
I seem to move in a circle, as I do not even know where to "start" the query:
Criteria crit = session.createCriteria(Container.class). ...
or
Criteria crit = session.createCriteria(Object.class). ...
It seems both possible for me, but i just have no idea how to go on from any of those 2 possibilities.
Update [2014.07.11, 14:19]:
I tried and started the query with the Object class and used a Subquery:
Session session = getSession();
Transaction transaction = session.beginTransaction();
DetachedCriteria IdListOfGroupMinimum = DetachedCriteria.forClass(Object.class, "obj")
IdListOfGroupMinimum.createAlias("con.id", "containerId")
.setProjection(
.Projections.projectionList()
.add(Projections.property("obj.id"))
.add(Projections.min("obj.timestamp"))
.add(Projections.groupProperty("containerId")))
.setProjection(Projection.property("obj.id"));
Criteria objects = session.createCriteria(object.class, "obj")
objects.add(Subqueries.in("obj.id", IdListOfGroupMinimum));
List<Object> = objects.list();
But I received the following error:
javax.servlet.ServletException: org.hibernate.QueryException: not an association: id
I tried to do this:
SELECT * from Object
WHERE id IN (
SELECT obj.id
FROM Object obj
INNER JOIN (
SELECT obj.containerID, MIN(obj.timestamp) AS minimum
FROM Object obj
GROUP BY obj.containerID) subquery
ON obj.containerID = subquery.containerID
WHERE obj.timestamp = subquery.minimum
)
I found a solution for my problem which is probably not the most elegant one, but it works.
Mainly I used the SQL-Query that I already posted above:
Session session = getSession();
Transaction transaction = session.beginTransaction();
//This query fetches the IDs of the smallest objects in each group with
//regard to the timestamp
Query q = session.createSQLQuery(
"SELECT obj.id FROM Object obj "
+ "INNER JOIN ( "
+ "SELECT obj.containerID, MIN(obj.timestamp) AS minimum "
+ "FROM Object obj "
+ "GROUP BY obj.containerID) subquery "
+ "ON obj.containerID = subquery.containerID "
+ "WHERE obj.timestamp = subquery.minimum "
);
//This tells Hibernate that the result are values of type Long
q.addScalar("id", LongType.INSTANCE)
//Creates a list of the found IDs
#SuppressWarnings("unchecked")
List<Long> ids = q.list();
//Fetches all object with those IDs...
Criteria smallestOfEachGroup = session.createCriteria(Object.class)
.add(Restrictions.in("id", ids);
//...and saves them in a list.
#SuppressWarnings("unchecked")
List<Object> desiredList = smallestOfEachGroup.list()
try{
transaction.commit();
} catch(HibernateException e) {
transaction.rollback();
}
As all my sketches are not the real code, so there might be still naming errors.
Anyway, I hope this helps someone.
I still would be pleased by any more elegant solution.
Update [2014.07.20, 18:50]:
I found a solution that uses Hibernate Criteria exclusively :)
Session session = getSession();
Transaction transaction = session.beginTransaction();
//This subquery fetches the minimal timestamp of a container.
DetachedCriteria minOfGroup = DetachedCriteria.forClass(Object.class);
minOfGroup.add(Restrictions.eqProperty("con.con_id", "outerObject.con.con_id")
.setProjection(Projections.min("timestamp"));
//This subquery fetches the IDs of all Objects, whose timestamp is minimal
//in their container.
DetachedCriteria groupwiseMin = DetachedCriteria.forClass(Object.class, "outerObject");
groupwiseMin.add(Subqueries.propertyEq("timestamp", minOfGroup));
.setProjections(Projections.id())
//This subquery fetches all Objects whose IDs are fetched by the groupwiseMin
//query
Criteria groupwiseMinObjects = session.createCriteria(Object.class);
groupwiseMinObjects.add(Subqueries.propertyIn("obj_id", groupwiseMin));
List<Object> desiredObjects = groupwiseMinObjects.list();
try{
transaction.commit();
} catch(HibernateException e) {
transaction.rollback();
}
I think you can make this query even shorter, if you remove the groupwiseMinObjects query above replace the groupwiseMin query by:
Criteria anotherGroupWiseMinObjects = session.createCriteria(Object.class, "outerObject");
anotherGroupwiseMinObjects.add(Subqueries.propertyEq("timestamp", minOfGroup));
But I did not test that.
In my original project I use several subqueries that converge in a single query.
That means after some subqueries, there is a final query like:
Criteria finalQuery = session.createCriteria(Object.class);
finalQuery.add(Subqueries. (...) )
(...)
.add(Subqueries. (...) );