uBAQ to UD table help

I’m working on a uBAQ that’s linked to OrderDtl. I need the ability to add new, update existing, and as a bonus delete rows in the UD table. Another challenge I haven’t even got too yet is that multiple UD rows can be related to one OrderDtl row.

I got the code working to add a new row but my code on updating isn’t working. The GetByID is returning the row I want to update. Is there a restriction from using the same BO for multiple calls?

Anyway here is the code.
:ghost:

var resultQuery = queryResultDataset.Results.Where(row => !string.IsNullOrEmpty(row.RowMod) && row.RowMod != "P");
      foreach (var ttResult in resultQuery)
      {  
          //Call the UD10SvcContract and assign it to udBO. udBO will automatically be disposed of after
          //exiting the using statement, so no need to do any cleanup 
          using (var udBO = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.UD10SvcContract>(Db))
          {
            //Get a new UD10 Tableset. Make sure to include using Ice.Tablesets; in the usings area
            UD10Tableset udTS = new UD10Tableset();
          
            // Get a new, empty UD10 Row
            udBO.GetaNewUD10(ref udTS);
          
            // Get the first row from the UD01DataSet, if it exists 
            var udRow = (from ud10 in udTS.UD10 select ud10).FirstOrDefault(); 
 
// Show MsgBox - DEBUG
string body = "1- UD10 RowMod: " + udRow.RowMod + " / ttResult.RowMod: " + ttResult.RowMod +" "+ ttResult.UD10_Key5.Length.ToString();
this.PublishInfoMessage(body, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"FirstVar","SecondVar");
 
            //Set new UD10 row 
            if(udRow != null && ttResult.UD10_Key5.Length == 0)
            {
                //generated new GUID
                var nguid = Guid.NewGuid().ToString();  
              
                //Update fields
                udRow.Company = Session.CompanyID;
                udRow.Key1 = "TextTag";  
                udRow.Key2 = ttResult.OrderDtl_OrderNum.ToString();
                udRow.Key3 = ttResult.OrderDtl_OrderLine.ToString();
                udRow.Key4 = "";
                udRow.Key5 = nguid; 
                udRow.Character01 = ttResult.UD10_Character01;     
                
                //Commit new record
                udBO.Update(ref udTS);
            }        
          }

          //update UD10 row  
          if(ttResult.UD10_Key5.Length > 0)
          {
            //new UD10SvcContract
            using (var udBO2 = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.UD10SvcContract>(Db))
            {    
              //New UD10Tableset 
              UD10Tableset udTS2 = new UD10Tableset();
  
              //Set vars and GetByID 
              string udkey1 = "TextTag";
              string udkey2 = ttResult.OrderDtl_OrderNum.ToString();
              string udkey3 = ttResult.OrderDtl_OrderLine.ToString();
              string udkey4 = "";
              string udkey5 = ttResult.UD10_Key5;   
              
              udBO2.GetByID(udkey1, udkey2, udkey3, udkey4, udkey5);  
              
              //Get the row form the dataset 
              var udRow2 = (from ud10 in udTS2.UD10 select ud10).FirstOrDefault();                        
              
string body2 = "2a ";
this.PublishInfoMessage(body2, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"FirstVar","SecondVar"); 
 
              if (udRow2 != null)
              {
string body2b = "2b " + udkey1 +" "+ udkey2+" "+ udkey3+" "+ udkey4+" "+ udkey5;
this.PublishInfoMessage(body2b, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"FirstVar","SecondVar"); 

                  udRow2.Character01 = ttResult.UD10_Character01;  
                  udBO2.Update(ref udTS2); 
              }
            }   
          }   
      }        

for the update don’t forget the rowmod = “U”;

1 Like

Thanks for the reminder the issue currently is that my GetByID appears to not work I’m getting an null error.

image

Wonder if that is the second time going through the loop. We were getting those errors while looping through a collection and calling a BO.

Hmm, possible I guess. I’m only seeing my debug messages once when I usually see them multiple times if it loops. I never see “2b” because of the null.

try this.

udTS2 = udBO2.GetByID(udkey1, udkey2, udkey3, udkey4, udkey5);

1 Like

Or maybe GetByID has a ref to udTS2…

I would do this by using UpdateExt then you don’t have to get the record, just set the keys and it will be added or updated and if you set a RowMod of D it will be deleted.

3 Likes

Thanks @gpayne, @timshuwy’s code does look nice and clean but there is some declarations missing form his example from how I’m reading the errors correclty.

CS0103 - The name ‘ds’ does not exist in the current context
CS0103 - The name ‘errorOccurred’ does not exist in the current context

Thanks, this is working with my initial posted code. Would like to support deletion too so going to beat on Tim’s code a bit before marking solution and posting code.

I have used UpdateEXT for deletion as well… I was told that it may not work in all instances, but it did in the UD Table I was working with. All you do is set teh RowMod to “D” in addition to all the indexes.

1 Like

Add this at the top EDIT UD10

UpdExtUD10Tableset ds = new UpdExtUD10Tableset();

and this just before the update

bool errorOccurred;
1 Like

I also have mine wrapped in a using at the very top.

using(var UD10Svc = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.UD10SvcContract>(Db, true))
{

} // last line
1 Like

Closer! What reference does UpdExtUD10Tableset need? I tried Ice.Tablesets but the error remains. Do I need to add another assembly than Ice.Contracts.BO.UD10 too?

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

That is something from your original code that is not defined now. If you want to post where is is now, I will throw it into a UD10 Ubaq and see if I can see the issue.

Sorry I should of posted it. The error is coming on: ds.udRow2.Add(udRow2);

      foreach (var ttResult in resultQuery)
      {  
 
          using (var udBO = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.UD10SvcContract>(Db))
          {
            UD10Tableset udTS = new UD10Tableset();

            udBO.GetaNewUD10(ref udTS);

            var udRow = (from ud10 in udTS.UD10 select ud10).FirstOrDefault(); 
 
            //New UD10 row 
            if(udRow != null && ttResult.UD10_Key5.Length == 0)
            {
                var nguid = Guid.NewGuid().ToString();  

                udRow.Company = Session.CompanyID;
                udRow.Key1 = "ARSLightRepCom";  
                udRow.Key2 = ttResult.OrderDtl_OrderNum.ToString();
                udRow.Key3 = ttResult.OrderDtl_OrderLine.ToString();
                udRow.Key4 = "";
                udRow.Key5 = nguid; 
                udRow.Character01 = ttResult.UD10_Character01;     
                
                udBO.Update(ref udTS);
            }        
          }

          //update UD10 row  
          if(ttResult.UD10_Key5.Length > 0)
          {
          
            using (var svc = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.UD10SvcContract>(Db))
            {    
              UpdExtUD10Tableset ds = new UpdExtUD10Tableset();
              
              bool errorOccurred;
              
              string udkey1 = "ARSLightRepCom";
              string udkey2 = ttResult.OrderDtl_OrderNum.ToString();
              string udkey3 = ttResult.OrderDtl_OrderLine.ToString();
              string udkey4 = "";
              string udkey5 = ttResult.UD10_Key5;   
              
              var udRow2 = new Ice.Tablesets.UD10Row {
                Company = Session.CompanyID, Key1 = udkey1, Key2 = udkey2, Key3 = udkey3, Key4 = udkey4, Key5 = udkey5,};
   
              udRow2.Character01 = ttResult.UD10_Character01; 
              //udRow2.SetUDField<System.String>("UDField1_c", "Some Value");
              //to delete the record uncomment this line: //UD12.RowMod = "D";
              
              ds.udRow2.Add(udRow2);
              
              BOUpdErrorTableset boUpdateErrors = svc.UpdateExt(ref ds, false, true, out errorOccurred);
            } 
          } 
      } 

I was about to start Friday drinking like Kevin since in my system UD10 was giving me a message that I had to fill out all of the fields and all I had done was change my UD03 to UD10 to match yours. I changed it to UD29 and it worked. Anyway, this routine does what I believe you want, but if I set RowMod to D it removed it from the Dataset, but not the Db.

I am not sure why you are setting Key5 to a guid when the record will get a unique SysRowID when it is made, but I did that also.

/* Create Update UD10 records */
using(var UD10Svc = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.UD10SvcContract>(Db, true))
{

UpdExtUD10Tableset UD10ts = new UpdExtUD10Tableset();

Ice.Diagnostics.Log.WriteEntry(" in Update  UD10");


foreach (var ttResult in queryResultDataset.Results.Where(row => !String.IsNullOrEmpty(row.RowMod)).Select(row => row) )
{


Ice.Diagnostics.Log.WriteEntry($"in create UD10 {ttResult.OrderDtl_OrderNum} - {ttResult.OrderDtl_OrderLine}"); /* order/line  */

                    
  
                    if(string.IsNullOrEmpty(ttResult.UD10_Key5))
                    {
                       ttResult.UD10_Key5 = Guid.NewGuid().ToString();
                    }
                    
                
                
                  var UD10 = new Ice.Tablesets.UD10Row
                  {
                    
                    Key1 = "ARSLightRepCom",
                    Key2 = ttResult.OrderDtl_OrderNum.ToString(),
                    Key3 = ttResult.OrderDtl_OrderLine.ToString(),
                    Key4 = "",
                    Key5 = ttResult.UD10_Key5,
                    Company = Session.CompanyID,
                    SysRowID = ttResult.UD10_SysRowID,
                  };
                    
                    
                    UD10.Character01 = ttResult.UD10_Character01; 
     
                    
                    UD10ts.UD10.Add(UD10);
    
     
        bool errorOccurred;
        BOUpdErrorTableset boUpdateErrors = UD10Svc.UpdateExt(ref UD10ts, false,true, out errorOccurred);
   

}
}
1 Like

Thanks! This UD table it being used for a customization that ties a OrderDtl to possibly many UD10 rows so I was using the Key5 guid to ensure no duplicates. If I don’t need it then even better.

Is there only one UD10 for each order/line? Or can a detail line have multiple UD10 records?

One OrderDtl line can have many UD10 lines.