Auto Check "Make Direct"

I have a BPM that I thought was working, but appears to be causing some glitches.
The intent is if there are certain parameters in the Part Master met, that when the line in a sales order is created, it would automatically check “Make Direct” as override since it’s not the default.
I have an In-Trans Data Directive on the OrderRel table that checks if a release was added and that the parameters are met and marks the box appropriately.
However, for those releases, the Planning Workbench is suggesting a new job for double the line quantity. If you uncheck “Make”, save, then recheck it, the suggestions fix themselves. If I disable the BPM, the suggestions are fine.
I tried using the business objects (ChangeMake and MasterUpdate based on trace) in a post method directive, but got quite a few errors since the release is created at a weird time.

Any suggestions on how to get this working?

Yea, if you are going to do this in the back door, there is one other table that needs updated as well… you need to also update the StockTrans field in the PartDTL table, which is the data that everything REALLY uses. Here is a snipit of code that does it.

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

                foreach (var partDtl in Db.PartDtl.With(LockHint.UpdLock).Where(x =>
                        x.Company == CompanyID &&
                        x.OrderNum == ttORel.OrderNum &&
                        x.OrderLine == ttORel.OrderLine &&
                        x.OrderRelNum == ttORel.OrderRelNum)) {

                    partDtl.StockTrans = false;
                }
                Db.Validate();
                txScope.Complete();
}
2 Likes

Hmm, no luck trying that.
The quantity in PartDtl looks fine, but PartSug is still double.

Would it totally botch things if I just set the proper suggested quantity in the PartSug table?

*Edit: Tried just forcing the PartSug value. Doesn’t want to change.

*Edit Edit: Flipped the updates to a Standard Trans and it appears to be working. I will need to do some more testing to make sure that forcing the PartSug.SugQty doesn’t negatively affect something else down the line.

Seems okay.

@hmwillett I am running into a similar scenario. Did you end up updating both PartSug and PartDtl tables in the standard data directive? Would it be possible to share your code if you still have it? Thanks!

I do recall it working, but I left that company.
@Randy can you help the guy out?

1 Like

Here is the BPM:

Condition 0:
image

Condition 1:

var tt = ttOrderRel.FirstOrDefault(r=>r.Added());
callContextBpmData.Checkbox01 = false;

if( tt != null )
{
  int oNum = Convert.ToInt32(tt.OrderNum);
  int oLine = Convert.ToInt32(tt.OrderLine);
  string pNum = "";
  decimal qty = tt.OurReqQty;
  //bool overrideMake = Convert.ToBoolean(tt["Override_c"]);
  
  // Get the part number from the line
  pNum = (
    from ol in Db.OrderDtl.With(LockHint.NoLock)
    where ol.Company == Session.CompanyID &&
          ol.OrderNum == oNum &&
          ol.OrderLine == oLine
    select ol.PartNum).DefaultIfEmpty("").FirstOrDefault();
  
  // Get the part master variables
  var pInfo = (
    from p in Db.Part.With(LockHint.NoLock)
    where p.Company == Session.CompanyID &&
          p.PartNum == pNum
  select new {p.TrackLots, p.MakeToStock_c, p.NonStock, p.TypeCode}).FirstOrDefault();
  
  if( pInfo != null )
  {
    // Purchased Part
    if( pInfo.TypeCode == "P" )
    {  
      callContextBpmData.Checkbox01 = true;
      return false;
    }
    
    if( !pInfo.MakeToStock_c && !pInfo.NonStock && pInfo.TrackLots )
    { 
        tt.Make = true;
        tt.BuyToOrder = false;
    
        return true;
    }
  }
}
return false;

Condition 2:
image

@Randy @hmwillett Really appreciate your help! I have gotten to this point of setting the Make Direct flag in OrderRel from In-transaction Data Directive. The issue I was running into was - checking the Make Direct flag alone doesn’t seem to trigger the other table updates that SalesOrder.ChangeMake method does. Because of this, MRP doesn’t process the Order as Make Direct (even when the flag is checked). So, I was wondering if you were taking additional steps to make sure that the other tables were updated as well. If not, maybe I’ll just call the ChangeMake method. Thanks!

@Randy --that’s not the correct one. There should be one where it sets something in the PartWip table too.

Ah, the BPM wasn’t named anything with “Make” so got confused. Here are the In-Trans and Standard ones. You may be able to skip the code in the In-Trans one but this is how it is in our LIVE so I’m posting it as is.

Condition 1:

var tt = ttOrderRel.FirstOrDefault(r=>r.Added() || r.Updated());
callContextBpmData.Checkbox01 = false;

if( tt != null )
{
  int oNum = Convert.ToInt32(tt.OrderNum);
  int oLine = Convert.ToInt32(tt.OrderLine);
  string pNum = "";
  decimal qty = tt.OurReqQty;
  bool overrideMake = Convert.ToBoolean(tt["Override_c"]);
  
  // Get the part number from the line
  pNum = (
    from ol in Db.OrderDtl.With(LockHint.NoLock)
    where ol.Company == Session.CompanyID &&
          ol.OrderNum == oNum &&
          ol.OrderLine == oLine
    select ol.PartNum).DefaultIfEmpty("").FirstOrDefault();
  

  using(var txScope = IceContext.CreateDefaultTransactionScope()) 
  {
  
    foreach (var partDtl in Db.PartDtl.With(LockHint.UpdLock).Where(x =>
      x.Company == CompanyID &&
      x.OrderNum == tt.OrderNum &&
      x.OrderLine == tt.OrderLine &&
      x.OrderRelNum == tt.OrderRelNum)) {
  
      partDtl.StockTrans = false;
    }
    Db.Validate();
    txScope.Complete();
  }
  
  using(var txScope = IceContext.CreateDefaultTransactionScope()) 
  {
  
    foreach (var partSug in Db.PartSug.With(LockHint.UpdLock).Where(x =>
      x.Company == CompanyID &&
      x.OrderNum == tt.OrderNum &&
      x.OrderLine == tt.OrderLine &&
      x.OrderRelNum == tt.OrderRelNum)) {
  
      partSug.SugQty = qty;
    }
    Db.Validate();
    txScope.Complete();
  }
  return true;

}
return false;

var tt = ttOrderRel.FirstOrDefault();

if( tt != null )
{
  int oNum = Convert.ToInt32(tt.OrderNum);
  int oLine = Convert.ToInt32(tt.OrderLine);
  int oRelNum = Convert.ToInt32(tt.OrderRelNum);
  string pNum = "";
  
  
  decimal qty = (
    from or in Db.OrderRel.With(LockHint.NoLock)
    where or.Company == Session.CompanyID &&
                        or.OrderNum == oNum &&
                        or.OrderLine == oLine &&
                        or.OrderRelNum == oRelNum
    select or.OurReqQty).DefaultIfEmpty(0).FirstOrDefault();
  
      
  using(var txScope = IceContext.CreateDefaultTransactionScope()) 
  {

    foreach (var partDtl in Db.PartDtl.With(LockHint.UpdLock).Where(x =>
      x.Company == CompanyID &&
      x.OrderNum == tt.OrderNum &&
      x.OrderLine == tt.OrderLine &&
      x.OrderRelNum == tt.OrderRelNum)) {

      partDtl.StockTrans = false;
    }
    Db.Validate();
    txScope.Complete();
  }
  
  using(var txScope = IceContext.CreateDefaultTransactionScope()) 
  {

    foreach (var partSug in Db.PartSug.With(LockHint.UpdLock).Where(x =>
      x.Company == CompanyID &&
      x.OrderNum == tt.OrderNum &&
      x.OrderLine == tt.OrderLine &&
      x.OrderRelNum == tt.OrderRelNum)) {

      partSug.SugQty = qty;
    }
    Db.Validate();
    txScope.Complete();
  }
}

2 Likes

@hmwillett @Randy This is AWESOME! Thanks for sharing the code. You guys ROCK!

1 Like

Thank @hmwillett it was all her work.

Hi Randy - I am trying to following your BPM and it is not working. One question I have is the below statement is Override_C a UD field that you have on the OrderRel table?
//bool overrideMake = Convert.ToBoolean(tt[“Override_c”]);

Yes–it is.

Anytime you see an “_c” on a field, it’s user defined.

Thanks - I assumed it was but figured I would ask as you know what they say about assuming :wink:
I am fairly new to BPM’s and the ones I have done have not used any custom code so this is a first.

1 Like

@hmwillett beat me to it but yeah all “_c” fields are UDs.

I did get this to work…I ended up only using the In-Transaction directive. I used the Condition 0 from the Standard Directive in this post, then we always want the "Make Direct’ set on our Order Release Lines so I used a “Set Field” and then an “Execute Custom Code” using the code from the Standard Directive in this post. Still need to have our users test but the testing I did it works great.

I truly appreciate those who shared the code for this as I would not know how to write this code…I am really good at copying others and tweaking if needed :slight_smile:

2 Likes