locked
CRM SDK web service salesorderdetail RRS feed

  • Question

  • Hello everyone. I am trying to add a sales order to CRM using the SDK web service. Adding one sales order detail is no problem, but how do you add more than one detail item? I have a web form for the user to select multiple items and each item needs to end up in the CRM SalesOrderDetail view. 

    Here is the code I am using now which only adds one detail record:

    salesorder.customerid.Value = new Guid(acctID);
    salesorder.customerid.type = "account";
    salesorder.pricelevelid = new Lookup(); 
    salesorder.pricelevelid.Value = new Guid(GUID_HERE);


    TargetCompoundSalesOrder target = new TargetCompoundSalesOrder();
    target.SalesOrder = salesorder;
    salesorderdetail salesorderdetail = new salesorderdetail();

    foreach (myitem.cartItem element in items)
     {
             salesorderdetail.description = "Web Order Detail";
             salesorderdetail.owninguser = new UniqueIdentifier();
             salesorderdetail.owninguser.Value = new Guid(GUID_HERE);
             salesorderdetail.productid = new Lookup();
             salesorderdetail.productid.Value = new Guid(productIDGUID);  
             salesorderdetail.productid.type = EntityName.product.ToString();
             salesorderdetail.quantity = new CrmDecimal();
             salesorderdetail.quantity.Value = Convert.ToInt32(element.Quantity);
             salesorderdetail.ispriceoverridden = new CrmBoolean();
             salesorderdetail.ispriceoverridden.Value = false;
             salesorderdetail.isproductoverridden = new CrmBoolean();
             salesorderdetail.isproductoverridden.Value = false;
             salesorderdetail.uomid = new Lookup();
             salesorderdetail.uomid.Value = new Guid(GUID_HERE);

             target.SalesOrderDetails = new salesorderdetail[1] { salesorderdetail };

     }

    CompoundCreateRequest create = new CompoundCreateRequest();
    create.Target = target;
    CompoundCreateResponse created = (CompoundCreateResponse)service.Execute(create);


    Thanks for taking a look at this!

    Thursday, November 19, 2009 6:05 AM

Answers

  • Hi,

    Try something like this, look at the commented lines of code.

       TargetCompoundSalesOrder target = new TargetCompoundSalesOrder();
                target.SalesOrder = salesorder;
    
                //int i = 0;
                //salesorderdetail[] mySalesOrderDetails = new salesorderdetail[totalItem];
               
              
                foreach (myitem.cartItem element in items)
                {
    
                    salesorderdetail salesorderdetail = new salesorderdetail();
                    salesorderdetail.description = "Web Order Detail";
                    salesorderdetail.owninguser = new UniqueIdentifier();
                    salesorderdetail.owninguser.Value = new Guid(GUID_HERE);
                    salesorderdetail.productid = new Lookup();
                    salesorderdetail.productid.Value = new Guid(productIDGUID);
                    salesorderdetail.productid.type = EntityName.product.ToString();
                    salesorderdetail.quantity = new CrmDecimal();
                    salesorderdetail.quantity.Value = Convert.ToInt32(element.Quantity);
                    salesorderdetail.ispriceoverridden = new CrmBoolean();
                    salesorderdetail.ispriceoverridden.Value = false;
                    salesorderdetail.isproductoverridden = new CrmBoolean();
                    salesorderdetail.isproductoverridden.Value = false;
                    salesorderdetail.uomid = new Lookup();
                    salesorderdetail.uomid.Value = new Guid(GUID_HERE);
    
                    //mySalesOrderDetails[i] = salesorderdetail;
                    //i++;              
    
                }
    
                //target.SalesOrderDetails = mySalesOrderDetails;
    
                CompoundCreateRequest create = new CompoundCreateRequest();
                create.Target = target;
                CompoundCreateResponse created = (CompoundCreateResponse)service.Execute(create);
    Regards,
    Nishant Rana

    http://nishantrana.wordpress.com
    Thursday, November 19, 2009 6:44 AM

All replies

  • Hi,

    Try something like this, look at the commented lines of code.

       TargetCompoundSalesOrder target = new TargetCompoundSalesOrder();
                target.SalesOrder = salesorder;
    
                //int i = 0;
                //salesorderdetail[] mySalesOrderDetails = new salesorderdetail[totalItem];
               
              
                foreach (myitem.cartItem element in items)
                {
    
                    salesorderdetail salesorderdetail = new salesorderdetail();
                    salesorderdetail.description = "Web Order Detail";
                    salesorderdetail.owninguser = new UniqueIdentifier();
                    salesorderdetail.owninguser.Value = new Guid(GUID_HERE);
                    salesorderdetail.productid = new Lookup();
                    salesorderdetail.productid.Value = new Guid(productIDGUID);
                    salesorderdetail.productid.type = EntityName.product.ToString();
                    salesorderdetail.quantity = new CrmDecimal();
                    salesorderdetail.quantity.Value = Convert.ToInt32(element.Quantity);
                    salesorderdetail.ispriceoverridden = new CrmBoolean();
                    salesorderdetail.ispriceoverridden.Value = false;
                    salesorderdetail.isproductoverridden = new CrmBoolean();
                    salesorderdetail.isproductoverridden.Value = false;
                    salesorderdetail.uomid = new Lookup();
                    salesorderdetail.uomid.Value = new Guid(GUID_HERE);
    
                    //mySalesOrderDetails[i] = salesorderdetail;
                    //i++;              
    
                }
    
                //target.SalesOrderDetails = mySalesOrderDetails;
    
                CompoundCreateRequest create = new CompoundCreateRequest();
                create.Target = target;
                CompoundCreateResponse created = (CompoundCreateResponse)service.Execute(create);
    Regards,
    Nishant Rana

    http://nishantrana.wordpress.com
    Thursday, November 19, 2009 6:44 AM
  • I tried your suggestion, but I get this error:
    "The name 'totalItem' does not exist in the current context"

    What is totalItem?


    Thanks for your help with this!

    Thursday, November 19, 2009 4:36 PM
  • I tried your suggestion, but I get this error:
    "The name 'totalItem' does not exist in the current context"

    What is totalItem?


    Thanks for your help with this!


    To get the total item count from my arraylist, I did this:

    ArrayList totalItems = (ArrayList)Session["mysession"];
    salesorderdetail[] mySalesOrderDetails = new salesorderdetail[totalItems.Capacity];

    It is outside of my loop so that is what threw me when you had "totalItem" in there. I am assuming what I am showing above is ok?  The code works great either way so thank you very much for your help!!!

    Thursday, November 19, 2009 4:50 PM
  • Hi,

    The totalItem field i had used was just to specify what the  size of the array should be.
    It was used for explanation purpose only. It wasn't an actual variable. 
    I should have put that information in comment :)


    Regards,
    Nishant Rana


    http://nishantrana.wordpress.com
    Friday, November 20, 2009 4:52 AM