Set a UD field from an unrelated UD Table

,

Well I have spent an entire week reading every post even remotely related to my issue. I have tried a FKV to link the tables but the Key1 on my UD table is a unique number that doesn’t correspond to the table I am trying to link it to. I want to use a Method Directive that when a record is entered into UD08 the key01 is sent to the matching PartLot.PRRNumber_c field. I can’t get to the the PartLot table in the BPM. I have seen other examples but I don’t understand what the variables mean (and now I can’t even find the examples I had been trying to work off of).

The relationship between the tables:
from Ice.UD08 as UD08
inner join Erp.PartLot as PartLot on
UD08.Company = PartLot.Company
and UD08.Key2 = PartLot.PartNum
and UD08.Key3 = PartLot.LotNum

@MelissaC I had something similar to this and made a few tweaks to simulate yours. Change the UD field and UD table and put it pre-processing on update.

/* update PartLot */

Erp.Tables.PartLot PartLot;

foreach (var tt in ttUD12.Where(tt => tt.Updated() || tt.Added()))
{
  PartLot = (from pl in Db.PartLot.With(LockHint.UpdLock) where pl.Company == tt.Company && pl.PartNum == tt.Key2 && pl.LotNum == tt.Key3 select pl).FirstOrDefault();
  if(PartLot!=null)
  {
    PartLot.SetUDField<System.String>("TariffComment_c", tt.Key1);
    
    Db.Validate();
  }
}

2 Likes

That worked wonderfully. I was also able to modify it to set data from the part lot to the job header during material issuing. Now I need to update a checkbox on the job header (that was set during material issue) when I update a checkbox in UD08. I’m not sure why the doe isn’t working. I have a pre-processing on UP08.Update and the code check in the BPM says everything is ok. I am hoping it’s something small I’m not seeing :slight_smile:


/* update JobHead */

Erp.Tables.JobHead JobHead;

foreach (var tt in ttUD08.Where(tt => tt.Updated() || tt.Added()))
{
  JobHead = (from jh in Db.JobHead.With(LockHint.UpdLock) where jh.Company == tt.Company && jh.Character01 == tt.Key1 select jh).FirstOrDefault();
  
  if(JobHead!=null)
    
  {
   
    JobHead.SetUDField<System.Boolean>("CheckBox05", tt.CheckBox01);
    
    Db.Validate();
  }
}

@MelissaC I would add some log entries to track how far the routine is before it fails. These are going to be in the event log and if you have the server logging on they will be there.

Ice.Diagnostics.Log.WriteEntry("JobHead Found ");

The UD fields from E9 sometimes act like UDs and sometimes like natural fields.
You may need the tt.UDField<System.Boolean>(“CheckBox01”) form or take both of the UDFields off.

Well I figured out the problem… I have 2 jobs that need to be updated. One was changing but the one I was looking at was not. I’m assuming it is the FirstorDefault() that is only returning one. I’m not sure what to put there to get all the jobs.

You need to change it to a foreach.

After some more reading I have tried 2 different ways of doing the foreach loop. They both have the error "Use of unassigned local variable ‘JobHead’, in the second one I also get the error "The name ‘job’ does not exist in the current context. I’m not sure which way is the correct way of coming at it.

Try1:

Erp.Tables.JobHead JobHead;

foreach (var tt in ttUD08.Where(tt => tt.Updated() || tt.Added()))
{
  
var JobNum = (from jh in Db.JobHead where jh.Company == tt.Company && jh.Character01 == tt.Key1 select new {jh.PartNum}).ToArray());
  
  if(JobNum!=null)
    
  {
   foreach (var job in JobNum)
     {
     
    JobHead.SetUDField<System.Boolean>("CheckBox05", tt.CheckBox01);
    
    Db.Validate();
    }
  }
}

Try 2

Erp.Tables.JobHead JobHead;

foreach (var tt in ttUD08.Where(tt => tt.Updated() || tt.Added()))
{
  
foreach (var job in (from jh in Db.JobHead where jh.Company == tt.Company && jh.Character01 == tt.Key1 select new {jh.PartNum}));
  
  {
  if(job!=null)
    
  {
   
     
    JobHead.SetUDField<System.Boolean>("CheckBox05", tt.CheckBox01);
    
    Db.Validate();
  
  }
}
}

The first line that you have there

Erp.Tables.JobHead JobHead;

isn’t doing anything. You haven’t assigned anything to that table, or even created it. You just declared it. Just get rid of that line, and change

JobHead.SetUDField<System.Boolean>("CheckBox05", tt.CheckBox01);

to

job.SetUDField<System.Boolean>("CheckBox05", tt.CheckBox01);

Your variable in the loop is job, not JobHead.

Thanks for the help. I am getting the error “The name ‘job’ does not exist in the current context” I’m not sure if Try 1 or Try 2 has the foreach in the right place.

Try this.

foreach (var tt in ttUD08.Where(tt => tt.Updated() || tt.Added()))
{
  var JobHead = (from jh in Db.JobHead.With(LockHint.UpdLock) where jh.Company == tt.Company && jh.Character01 == tt.Key1 select jh).ToList();
  
  foreach (var x in JobHead)
  {
    if(x!=null)
    {
        x.SetUDField<System.Boolean>("CheckBox05", tt.CheckBox01); 
        Db.Validate();
    }
  }

}
1 Like

Thank you!! That worked :slight_smile: