Skip to main content
Solved

How do I update a custom field in a different DAC to the one specified in FieldUpdated event?


Forum|alt.badge.img

Hi,

In the Projects screen, a client is wanting to have the description field from the Tasks tab to be copied across to the ‘Task Description’ custom field in the Revenue and Cost Budget tabs.

 

I have code set up from a previous request that copied over the Task ID column to these tabs, so I am just needing to add some additional logic to this code, but I am not sure how to achieve this.

 

The comments in the code starting with ‘ATTEMPT:’ show where I was adding in the logic to try and achieve this.

 

It publishes without any issues, but then when I try it out in the Projects screen, I get the error shown in the image below about an incorrect extension being requested.

 

What would you recommend for my next attempt?

 

Kind regards,

Andrew

 



protected void PMTask_UsrLineCtr_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
    {

      var row = (PMTask)e.Row;

      PMTaskExt rowExt = row.GetExtension<PMTaskExt>();

      // ATTEMPT: Grabbing the DAC extension for the PMRevenueBudget and PMCostBudget DACs
      PMBudgetExt budgetExt = cache.GetExtension<PMBudgetExt>(row);

      PMTask task = new PMTask();
      task.ProjectID = row.ProjectID;
      task.TaskID = row.TaskID;
      task.Description = row.Description;

      var selectCostBudget = new PXSelect<PMCostBudget,
        Where<PMCostBudget.projectID, Equal<Required<PMCostBudget.projectID>>,
        And<PMCostBudget.projectTaskID, Equal<Required<PMCostBudget.projectTaskID>>,
        And<PMCostBudget.type, Equal<GL.AccountType.expense>>>>>(this.Base);

      var selectRevenueBudget = new PXSelect<PMRevenueBudget,
        Where<PMRevenueBudget.projectID, Equal<Required<PMRevenueBudget.projectID>>,
        And<PMRevenueBudget.projectTaskID, Equal<Required<PMRevenueBudget.projectTaskID>>,
        And<PMRevenueBudget.type, Equal<GL.AccountType.income>>>>>(this.Base);

      bool costBudgetExists = false;
      // If task already exists in the Cost Budget column, just update the LineCntr column
      foreach (PMCostBudget budget in selectCostBudget.Select(task.ProjectID, task.TaskID))
      {      
          budget.LineCntr = rowExt.UsrLineCtr;
          costBudgetExists = true;
          selectCostBudget.Update(budget);
      }

      bool revBudgetExists = false;
      // If task already exists in the Cost Budget column, just update the LineCntr column
      foreach (PMRevenueBudget budget in selectRevenueBudget.Select(task.ProjectID, task.TaskID))
      {      
          budget.LineCntr = rowExt.UsrLineCtr;
          revBudgetExists = true;
          selectRevenueBudget.Update(budget);
      }


      if (!costBudgetExists) {
        // Add task to Cost Budget Tab if it doesn't exist
        PMCostBudget costBudget = new PMCostBudget();
        costBudget.LineCntr = rowExt.UsrLineCtr;
        costBudget.ProjectTaskID = task.TaskID;
        selectCostBudget.Insert(costBudget);
      }

      if (!revBudgetExists) {
        // Add task to Revenue Budget Tab if it doesn't exist
        PMRevenueBudget revBudget = new PMRevenueBudget();
        revBudget.LineCntr = rowExt.UsrLineCtr;
        revBudget.ProjectTaskID = task.TaskID;

        // ATTEMPT: This is how I was aiming to update the custom field with the Task description
        budgetExt.UsrDescriptionExt = task.Description;

        selectRevenueBudget.Insert(revBudget);
      }
    }

    #endregion
  }
}

 

 

Best answer by Zoltan Febert

Hi @AndrewA,

The error message means you try to get a PMBudgetExt type of extension using a PMTask row. It fails, because the base DAC of PMBudgetExt is PMBudget (I guess), not PMTask.

I added one more line to your code, you need to remove PMTaskExt rowExt = row.GetExtension<PMTaskExt>(); from your code.

      if (!revBudgetExists) {
        // Add task to Revenue Budget Tab if it doesn't exist
        PMRevenueBudget revBudget = new PMRevenueBudget();
        revBudget.LineCntr = rowExt.UsrLineCtr;
        revBudget.ProjectTaskID = task.TaskID;

        // Get DAC Extension
        var budgetExt = revBudget.GetExtension<PMBudgetExt>();

        // ATTEMPT: This is how I was aiming to update the custom field with the Task description
        budgetExt.UsrDescriptionExt = task.Description;

        selectRevenueBudget.Insert(revBudget);
      }

 

View original
Did this topic help you find an answer to your question?

2 replies

Zoltan Febert
Jr Varsity I
Forum|alt.badge.img+3
  • Jr Varsity I
  • 175 replies
  • Answer
  • October 12, 2023

Hi @AndrewA,

The error message means you try to get a PMBudgetExt type of extension using a PMTask row. It fails, because the base DAC of PMBudgetExt is PMBudget (I guess), not PMTask.

I added one more line to your code, you need to remove PMTaskExt rowExt = row.GetExtension<PMTaskExt>(); from your code.

      if (!revBudgetExists) {
        // Add task to Revenue Budget Tab if it doesn't exist
        PMRevenueBudget revBudget = new PMRevenueBudget();
        revBudget.LineCntr = rowExt.UsrLineCtr;
        revBudget.ProjectTaskID = task.TaskID;

        // Get DAC Extension
        var budgetExt = revBudget.GetExtension<PMBudgetExt>();

        // ATTEMPT: This is how I was aiming to update the custom field with the Task description
        budgetExt.UsrDescriptionExt = task.Description;

        selectRevenueBudget.Insert(revBudget);
      }

 


Forum|alt.badge.img
  • Author
  • Varsity I
  • 80 replies
  • October 12, 2023

Thanks so much for that Zoltan!


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings