Copying data from Customer to Sales Order

Throwing in the towel on this one for now.

I’m trying to copy data from Customer.Comment to OrderHed.Character10 on a BPM update from OrderHed.Update. I want it to fire off once, so I can probably use if Charater10 is not null then fire off. etc.

I’ve tried several snippets I found on this site, but I’m in some loops and my client just crashes.

I’m aware of other methods such as update table by query with configured mapping - which works, but when I change or edit the data on character10 in Orderhed. it will overwrite with the data from customer.comment. I added a few conditions and it still would overwrite. That’s why I’m here. Looking for a BPM C# Method.

One would think this would work:

using (var txScope = IceContext.CreateDefaultTransactionScope())
{

 int custNum = ds.OrderHed.FirstOrDefault().CustNum;   
    var OrderHeds = Db.OrderHed.Where(x => x.CustNum == custNum).FirstOrDefault();
    var spec = Db.Customer.Where(x => x.CustNum == custNum).FirstOrDefault().Comment.ToString();


  
{
  OrderHeds.OrderComment = spec.ToString();
}

Db.Validate();
txScope.Complete();
}

I would implement it using a Method Directive on SalesOrder.Update as you suggest. Your preference was C# code, so give this a try:

var ordHed = ds.OrderHed.Where(o => o.RowMod == "U").FirstOrDefault();
if(ordHed != null)
{
    string vCustComment = Db.Customer.Where(c => c.Company == CompanyID && c.CustNum == ordHed.CustNum).Select(c => c.Comment).FirstOrDefault();
    if(vCustComment != null && !String.IsNullOrEmpty(vCustComment))
    {
        if(String.IsNullOrEmpty(ordHed.OrderComment))
        {
            ordHed.OrderComment = vCustComment;           
        }
    }
}

So we’re grabbing the updated OrderHed row from the dataset, doing a lookup to the customer to get the comment, then doing a check to see if the OrderHed.OrderComment field is empty and if so update it.

The nice thing about doing it this way is that because we’re pre-process, Epicor will do the database update for us!

BTW - in your original code you were looking up the Order to update based only on CustNum, so it would have probably have updated the earliest order for that customer (depending on the default index)! Also, always join using Company first in BAQs and custom code - all the indexes are built this way so it’s quicker, if you’re multi company then not having that will return data other than what you expected as multiple customers will have CustNum = 1.

1 Like

I see.

This looks promising. I’ve tried multiple iterations of this method but I’ve been unsuccessful, the code I pasted one just one of the failed attempts. :smiley:

I’ve changed OrderComment to Character10 and I get the following errors.:

System.Drawing.Bitmap CS1061 ‘OrderHedRow’ does not contain a definition for ‘Character10’ and no accessible extension method ‘Character10’ accepting a first argument of type ‘OrderHedRow’ could be found (are you missing a using directive or an assembly reference?)

Thanks Mark

@kyle.l Some places the system can’t see UD fields. You need to set them like below.

I do this kind of copying post processing on SalesOrder.ChangeCustomer, so it only fires on a customer change

ordHed.SetUDField<System.String>("Character10",vCustComment);
1 Like

I see, so that’s why character10 wasn’t being updated but when I tested other codes I was able to update.

I’ve tried SalesOrder.ChangeCustomer and other SalesOrder methods but I’m unable to update the fields. So far only SalesOrder.update seems to work, unfortunately because its a setfield, it’s always setting the field despite my conditions or the IF statements in the code. It overwrites what I edited in characater10 in Sales order entry.

I took what @markdamen wrote and put one of my UD in it and made it for post processing ChangeCustomer and this code works and does not get overwritten. Comment or remove the ShortChar09 and uncomment Character10 and this is ready. when you start a new order or change customer it will fire.

/* Get customer Comment */


var ordHed = ds.OrderHed.FirstOrDefault();

if(ordHed != null)
{
    string vCustComment = Db.Customer.Where(c => c.Company == CompanyID && c.CustNum == ordHed.CustNum).Select(c => c.Comment).FirstOrDefault();
    if(vCustComment != null && !String.IsNullOrEmpty(vCustComment))
    {
    
        var ch10 = ordHed.UDField<System.String>("ShortChar09");
        //var ch10 = ordHed.UDField<System.String>("Character10");
        if(String.IsNullOrEmpty(ch10))
        {
            //ordHed.OrderComment = vCustComment;    
            
            //ordHed.SetUDField<System.String>("Character10",vCustComment);
            ordHed.SetUDField<System.String>("ShortChar09", vCustComment);
        }
    }
}

2 Likes

:smiling_face_with_tear: :smiling_face_with_tear: :smiling_face_with_tear: :smiling_face_with_tear:

It’s beautiful. I will show my unborn child what greatness looks like.

Thanks Mark, Thanks Greg.

RawPayn.