Skip to main content
Answer

How can I programmatically assign a Related Entity

  • February 2, 2022
  • 12 replies
  • 351 views

darylbowman
Captain II
Forum|alt.badge.img+15

I need to programmatically tie an email to a Case. The code I have makes some kind of association, because the email will display in the Case activities. However, the email ‘Related Entity’ field remains blank.

 

Here’s my code:

Base.CurrentMessage.SetValueExt<CRSMEmail.refNoteID>(email, taxCase.NoteID);
email = Base.CurrentMessage.Update(email);
Base.Actions.PressSave();

In the database, the field is being correctly set, but the UI does not reflect the change.

Best answer by darylbowman

I was actually.

 

 

Here’s my code:

Guid? noteID = PXNoteAttribute.GetNoteID<CRCase.noteID>(caseGraph.Case.Cache, taxCase);
caseGraph.Case.Update(taxCase);
caseGraph.Actions.PressSave();

// Reassign Appointment to Case instead, so the Case's 'Appointment Date' is correct
bAccountGraph.Activities.Current = bAccountGraph.Activities.Search<CRPMTimeActivity.noteID>(appointment.NoteID);
bAccountGraph.Activities.SetValueExt<CRPMTimeActivity.refNoteID>(appointment, taxCase.NoteID);
bAccountGraph.Activities.Update(appointment);
bAccountGraph.Actions.PressSave();

If I remember correctly, 

Guid? noteID = PXNoteAttribute.GetNoteID<CRCase.noteID>(caseGraph.Case.Cache, taxCase);

performs an insert on the Note table which allows the association to occur.

12 replies

Yuriy Zaletskyy
Jr Varsity I
Forum|alt.badge.img+3

Can you please add few more details:

  1. On which screen you want to have this connection?
  2. For which graph you want to create an extension?
  3. Which action/method you override/extend/append?

darylbowman
Captain II
Forum|alt.badge.img+15
  • Author
  • February 2, 2022

@yuriyzaletskyy69 - My more complete code looks like this:

 

public class CREmailActivityMaint_Extension : PXGraphExtension<CREmailActivityMaint>
{
public PXAction<CRSMEmail> AddToTaxCase;
[PXButton(CommitChanges = true, Category = "Actions")]
[PXUIField(DisplayName = "Add To Tax Case")]
protected virtual void addToTaxCase()
{
CRSMEmail email = Base.Message.Current;
if (email is null) return;

CRCase taxCase = BAccountWorkflow.BAccountWorkflow_BusinessAccountMaint_Extension.GetOrCreateCurrentTaxCase(bAccount.BAccountID);
if (taxCase != null)
{
Base.CurrentMessage.SetValueExt<CRSMEmail.refNoteID>(email, taxCase.NoteID);
email = Base.CurrentMessage.Update(email);
Base.Actions.PressSave();
}
}
}

I’ve tried using Base.CurrentMessage and Base.Message. Both assign the correct value. Neither populate the UI field correctly.


Yuriy Zaletskyy
Jr Varsity I
Forum|alt.badge.img+3

@Deetz can you add screenshots as well? What do you expect to see in UI field? Why you don’t like what you see in UI field? What should be there instead?


darylbowman
Captain II
Forum|alt.badge.img+15
  • Author
  • February 2, 2022

@yuriyzaletskyy69 

The right thing (achieve from UI): 

The wrong thing (achieved through code):

 


Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • February 2, 2022

Hi, @Deetz I just tried with your code but was unable to compile and get the error below. I’m missing something?

 

 

 


Yuriy Zaletskyy
Jr Varsity I
Forum|alt.badge.img+3

@Deetz few more questions, which build of Acumatica you use, and which exactly screenid you try to customize?


darylbowman
Captain II
Forum|alt.badge.img+15
  • Author
  • February 2, 2022

@yuriyzaletskyy69 - 21.204.0055 and CR.30.60.15

 

@Naveen B - It’s just a method that returns a Case to attach the Activity too. You can write a simple BQL select to get a Case.


Yuriy Zaletskyy
Jr Varsity I
Forum|alt.badge.img+3

@Deetz 

I’m looking on  CRSMEmail declaration, and see that it is a PXProjection : 

[PXProjection(typeof (Select2<CRActivity, InnerJoin<SMEmail, On<SMEmail.refNoteID, Equal<CRActivity.noteID>>>, Where<CRActivity.classID, Equal<CRActivityClass.email>, Or<CRActivity.classID, Equal<CRActivityClass.emailRouting>>>>), Persistent = true)]
 

  [Serializable]
  public class CRSMEmail : CRActivity

If to look on declaration of RefNoteID, we see following:

[PXDBGuid(false, BqlField = typeof (CRActivity.refNoteID))]

…………………...

[PXUIField(DisplayName = "References Nbr.")]

 public override Guid? RefNoteID { get; set; }

 

If to sum up, in order for your entity to work, it must satisfy following criteria:

  1. is should correspond to PXProjection requirements, which states that NoteID should have record in SMEmail, classID should be equal to email or class id equal to email routing
  2. It should live in table CRActivity

Without 1 and 2 satisfied, Acumatica will either crash, or behave in a strange way.

 


darylbowman
Captain II
Forum|alt.badge.img+15
  • Author
  • February 3, 2022

@yuriyzaletskyy69  I’m not sure I’m following. It sounds like those requirements would be for creating a new CRSMEmail, which is not what I’m doing. I’m simply trying to add the CRSMEmail to a Case via the RefNoteID. This works in the backend, as it shows up under the Case’s Activity tab, but the CRSMEmail ‘Related Entity’ field doesn’t give the entity description like it’s supposed to. This is a calculated field on CRActivity (PXFormula). I’m not sure how to accomplish  this.


Chris Hackett
Community Manager
Forum|alt.badge.img
  • Acumatica Community Manager
  • April 25, 2022

Hi @darylbowman were you ever able to resolve your issue? Thank you!


darylbowman
Captain II
Forum|alt.badge.img+15
  • Author
  • Answer
  • April 25, 2022

I was actually.

 

 

Here’s my code:

Guid? noteID = PXNoteAttribute.GetNoteID<CRCase.noteID>(caseGraph.Case.Cache, taxCase);
caseGraph.Case.Update(taxCase);
caseGraph.Actions.PressSave();

// Reassign Appointment to Case instead, so the Case's 'Appointment Date' is correct
bAccountGraph.Activities.Current = bAccountGraph.Activities.Search<CRPMTimeActivity.noteID>(appointment.NoteID);
bAccountGraph.Activities.SetValueExt<CRPMTimeActivity.refNoteID>(appointment, taxCase.NoteID);
bAccountGraph.Activities.Update(appointment);
bAccountGraph.Actions.PressSave();

If I remember correctly, 

Guid? noteID = PXNoteAttribute.GetNoteID<CRCase.noteID>(caseGraph.Case.Cache, taxCase);

performs an insert on the Note table which allows the association to occur.


Chris Hackett
Community Manager
Forum|alt.badge.img
  • Acumatica Community Manager
  • April 25, 2022

@darylbowman awesome! Thank you for sharing!