CRExtensionHelper.UpdateServiceOrderHeader() - acumatica

got the following error after upgrading Acumatica from 2020r2 to 2022r2
After my research found out UpdateServiceOrderHeader() was removed from CRExtensionHelper.cs
now its SM_CRCaseMaint.cs and SM_SOOrderEntry.cs.
I implemented the following changes but got the following error;
full code:
public PXAction<CRCase> CreateServiceOrders;
[PXButton]
[PXUIField(DisplayName = "Create Service Orders", Visible = true, MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
public virtual void createServiceOrders()
{
CRCase crCaseRow = Base.Case.Current;
FSxCRCase fsxCRCaseRow = Base.Case.Cache.GetExtension<FSxCRCase>(crCaseRow);
if (crCaseRow.CustomerID == null && crCaseRow.ContactID == null)
{
throw new PXException("You can not create proceed." + Environment.NewLine + "The Business Account and Contact are NULL.");
}
if (crCaseRow.CustomerID == null)
{
throw new PXException("You can not create proceed." + Environment.NewLine + "The Business Account is NULL.");
}
if (crCaseRow.ContactID == null)
{
throw new PXException("You can not create proceed." + Environment.NewLine + "The ContactID is NULL.");
}
if (CreateServiceOrderFilter.AskExt() == WebDialogResult.OK)
{
fsxCRCaseRow.SDEnabled = true;
fsxCRCaseRow.BranchLocationID = CreateServiceOrderFilter.Current.BranchLocationID;
fsxCRCaseRow.SrvOrdType = CreateServiceOrderFilter.Current.SrvOrdType;
fsxCRCaseRow.AssignedEmpID = CreateServiceOrderFilter.Current.AssignedEmpID;
fsxCRCaseRow.ProblemID = CreateServiceOrderFilter.Current.ProblemID;
PXLongOperation.StartOperation(Base, delegate ()
{
CreateServiceOrderDocument(Base, crCaseRow, CreateServiceOrderFilter.Current);
});
}
}
private void CreateServiceOrderDocument(CRCaseMaint graphCRCaseMaint, CRCase crCaseRow, FSCreateServiceOrderOnCaseFilter fsCreateServiceOrderOnCaseFilterRow)
{
if (graphCRCaseMaint == null || crCaseRow == null || fsCreateServiceOrderOnCaseFilterRow == null)
{
return;
}
ServiceOrderEntry graphServiceOrderEntry = PXGraph.CreateInstance<ServiceOrderEntry>();
FSServiceOrder newServiceOrderRow = CRExtensionHelper.InitNewServiceOrder(CreateServiceOrderFilter.Current.SrvOrdType, ID.SourceType_ServiceOrder.CASE);
graphServiceOrderEntry.ServiceOrderRecords.Current = graphServiceOrderEntry.ServiceOrderRecords.Insert(newServiceOrderRow);
//CRExtensionHelper.UpdateServiceOrderHeader(
// graphServiceOrderEntry,
// Base.Case.Cache,
// crCaseRow,
// fsCreateServiceOrderOnCaseFilterRow,
// graphServiceOrderEntry.ServiceOrderRecords.Current,
// true);
SM_CRCaseMaint.UpdateServiceOrderHeader(
graphServiceOrderEntry,
Base.Case.Cache,
crCaseRow,
fsCreateServiceOrderOnCaseFilterRow,
graphServiceOrderEntry.ServiceOrderRecords.Current,
true);
//graphServiceOrderEntry.ServiceOrderRecords.Current.SourceID = crCaseRow.CaseCD;
graphServiceOrderEntry.ServiceOrderRecords.Current.SourceRefNbr = crCaseRow.CaseCD;
if (!Base.IsContractBasedAPI)
{
throw new PXRedirectRequiredException(graphServiceOrderEntry, null);
}
}
}
#endregion
}

Error CS0120 means that you need to instantiate the object before calling UpdateServiceOrderHeader(). I would try searching the source code for other instances of this function being called and replicate the setup.

Related

Invoice and Memos screen Email Invoice/Memo Action in Acumatica

We have created some customized reports and these will open up based on Sales Order type in details tab. Same way we want to send report based on order type when we use Email Invoice/Memo action from Actions menu.
We tried to override the code but still we are seeing default report is sent in email. How can I fix this? My code is below:
[PXOverride]
public PXAction<ARInvoice> sendARInvoiceMemo;
[PXUIField(DisplayName = "Send AR Invoice/Memo", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
[PXLookupButton]
public IEnumerable SendARInvoiceMemo(PXAdapter adapter, String reportID)
{
PXReportRequiredException ex = null;
foreach (ARInvoice doc in adapter.Get<ARInvoice>())
{
var parameters = new Dictionary<string, string>();
ARTran TranData = PXSelectReadonly<ARTran, Where<ARTran.tranType, Equal<Required<ARTran.tranType>>,
And<ARTran.refNbr, Equal<Required<ARTran.refNbr>>>>>.Select(Base, doc.DocType, doc.RefNbr);
if (TranData != null)
{
if (TranData.SOOrderType == "WS" || TranData.SOOrderType == "WO" || TranData.SOOrderType == "TS" || TranData.SOOrderType == "IM")
{
if (reportID == null) reportID = "KR501011";
Dictionary<string, string> mailParams = new Dictionary<string, string>();
if (reportID == "KR501011")
{
mailParams["DocType"] = doc.DocType;
mailParams["RefNbr"] = doc.RefNbr;
if (!ReportNotificationGenerator.Send(reportID, mailParams).Any())
{
throw new PXException(ErrorMessages.MailSendFailed);
}
}
Base.Clear();
Base.Document.Current = Base.Document.Search<ARInvoice.refNbr>(doc.RefNbr, doc.DocType);
}
if (TranData.SOOrderType == "RS" || TranData.SOOrderType == "RO" || TranData.SOOrderType == "PS" || TranData.SOOrderType == "QT")
{
if (reportID == null) reportID = "KR501012";
Dictionary<string, string> mailParams = new Dictionary<string, string>();
if (reportID == "KR501012")
{
mailParams["DocType"] = doc.DocType;
mailParams["RefNbr"] = doc.RefNbr;
if (!ReportNotificationGenerator.Send(reportID, mailParams).Any())
{
throw new PXException(ErrorMessages.MailSendFailed);
}
}
Base.Clear();
Base.Document.Current = Base.Document.Search<ARInvoice.refNbr>(doc.RefNbr, doc.DocType);
}
}
}
if (ex != null) throw ex;
return adapter.Get();
}
Note, no version was specified so my work was done against 2020r2.
SendARInvoiceMemo isn't called by the "Email Invoice/Memo" action. Rather, Notification is the delegate for that action.
Note that notification isn't actually calling a report ID, rather it relies on notification CD and your mailing settings to determine what report to run. I modified the code to change to a new notificationCD I created called INVOICEALT. This is configured to my alternative Report ID.
[PXOverride()]
[PXUIField(DisplayName = "Notifications", Visible = false)]
[PXButton(ImageKey = PX.Web.UI.Sprite.Main.DataEntryF)]
public virtual IEnumerable Notification(PXAdapter adapter,
[PXString]
string notificationCD)
{
foreach (ARInvoice doc in adapter.Get().RowCast<ARInvoice>())
{
Base.Document.Current = doc;
Dictionary<string, string> parameters = new Dictionary<string, string>
{
["DocType"] = doc.DocType,
["RefNbr"] = doc.RefNbr
};
using (var ts = new PXTransactionScope())
{
if (ProjectDefaultAttribute.IsProject(Base, doc.ProjectID) && Base.Activity.IsProjectSourceActive(doc.ProjectID, notificationCD))
{
Base.Activity.SendNotification(PMNotificationSource.Project, notificationCD, doc.BranchID, parameters);
}
else
{
//Base.Activity.SendNotification(ARNotificationSource.Customer, notificationCD, doc.BranchID, parameters); //This is what was there
//Inserted switch based on Sales Order Type >>
ARTran TranData = PXSelectReadonly<ARTran, Where<ARTran.tranType, Equal<Required<ARTran.tranType>>,
And<ARTran.refNbr, Equal<Required<ARTran.refNbr>>>>>.Select(Base, doc.DocType, doc.RefNbr);
switch (TranData.SOOrderType)
{
case "IN":
Base.Activity.SendNotification(ARNotificationSource.Customer, "INVOICEALT", doc.BranchID, parameters);
break;
default:
Base.Activity.SendNotification(ARNotificationSource.Customer, notificationCD, doc.BranchID, parameters);
break;
}
//<< Inserted switch based on Sales Order Type
}
Base.Save.Press();
ts.Complete();
}
yield return doc;
}
}
As an alternative to the above, if you're just talking about users running the action, you could hide the Email Invoice/Memo action and put a new action in place.
public PXAction<ARInvoice> sendARInvoiceMemoAlt;
[PXUIField(DisplayName = "Alt Email Invoice/Memo", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
[PXLookupButton]
public IEnumerable SendARInvoiceMemoAlt(PXAdapter adapter, String reportID)
{
PXReportRequiredException ex = null;
foreach (ARInvoice doc in adapter.Get<ARInvoice>())
{
var parameters = new Dictionary<string, string>();
ARTran TranData = PXSelectReadonly<ARTran, Where<ARTran.tranType, Equal<Required<ARTran.tranType>>,
And<ARTran.refNbr, Equal<Required<ARTran.refNbr>>>>>.Select(Base, doc.DocType, doc.RefNbr);
if (TranData != null)
{
if (TranData.SOOrderType == "SO")// || TranData.SOOrderType == "WS" || TranData.SOOrderType == "WO" || TranData.SOOrderType == "TS" || TranData.SOOrderType == "IM")
{
if (reportID == null) reportID = "AR641000";
Dictionary<string, string> mailParams = new Dictionary<string, string>();
if (reportID == "AR641000")
{
mailParams["DocType"] = doc.DocType;
mailParams["RefNbr"] = doc.RefNbr;
if (!ReportNotificationGenerator.Send(reportID, mailParams).Any())
{
throw new PXException(ErrorMessages.MailSendFailed);
}
}
Base.Clear();
Base.Document.Current = Base.Document.Search<ARInvoice.refNbr>(doc.RefNbr, doc.DocType);
}
if (TranData.SOOrderType == "IN")// || TranData.SOOrderType == "RS" || TranData.SOOrderType == "RO" || TranData.SOOrderType == "PS" || TranData.SOOrderType == "QT")
{
if (reportID == null) reportID = "AR641001";
Dictionary<string, string> mailParams = new Dictionary<string, string>();
if (reportID == "AR641001")
{
mailParams["DocType"] = doc.DocType;
mailParams["RefNbr"] = doc.RefNbr;
if (!ReportNotificationGenerator.Send(reportID, mailParams).Any())
{
throw new PXException(ErrorMessages.MailSendFailed);
}
}
Base.Clear();
Base.Document.Current = Base.Document.Search<ARInvoice.refNbr>(doc.RefNbr, doc.DocType);
}
}
}
if (ex != null) throw ex;
return adapter.Get();
}
public override void Initialize()
{
base.Initialize();
Base.ActionsMenuItem.AddMenuAction(sendARInvoiceMemoAlt);
}

Automapper: map an object inside a Profile using a parameter

I have the following issue using ASP.NET Core 2.2 and Automapper 9.0.0:
I have a Entity that I map to a dto, this works fine. Inside that entity are a few entities as well. These cannot be flattened as they are also needed by our client. There is however one property within those nested entities that need additional mapping.
within the Profile class of the overlapping Entity i do this:
public class OperationalToPalletResultProfile : Profile
{
public OperationalToPalletResultProfile()
{
string lang = null;
CreateMap<TblDatOperationalHUTrace, PalletResult>()
.ForMember(dest => dest.TransCode, act => act.MapFrom((src, _, transactionCode, ctx) =>
ctx.Mapper.Map<TblLstCode, TransactionCodeResult>(src.TransCode)));
}
}
This effectively maps the TblLstCode to TransactionCodeResult correctly. However the "DisplayDesc" property of the TransactionCodeResult remains null...
Note: the "lang" property gets set with the "ProjectTo" method:
var values = await query
.Take(5000)
.ProjectTo<PalletResult>(mapper.ConfigurationProvider, new { lang = language })
.ToListAsync()
.ConfigureAwait(false);
I have tried:
1- Using "AfterMap" in Profile class (result: DisplayDesc = null)
CreateMap<TblDatOperationalHUTrace, PalletResult>()
.ForMember(dest => dest.TransCode, act => act.MapFrom((src, _, transactionCode, ctx) =>
ctx.Mapper.Map<TblLstCode, TransactionCodeResult>(src.TransCode, opt =>
opt.AfterMap((source, destination) =>
destination.DisplayDesc = $"[{destination.ShortName}] {destination.DescToLanguageDesc(destination, lang)}"))))
Note: DescToLanguageDesc is a function that finds a property in the entity with reflection (this works and is not part of the problem)
2- Creating an IMappingAction to use in the Profile of the SubEntity (result: DisplayDesc = null)
public class TransactionCodeTranslation : IMappingAction<TblLstCode, TransactionCodeResult>
{
private readonly IHttpContextAccessor httpContextAccessor;
public TransactionCodeTranslation(IHttpContextAccessor httpContextAccessor)
{
this.httpContextAccessor = httpContextAccessor;
}
public void Process(TblLstCode source, TransactionCodeResult destination, ResolutionContext context)
{
//Get language from httpContext - this works correctly
destination.DisplayDesc = $"[{source.ShortName}] {source.DescToLanguageDesc(source, language)}";
}
}
public class TransactionCodeProfile : Profile
{
public TransactionCodeProfile()
{
string language = string.Empty;
CreateMap<TblLstCode, TransactionCodeResult>()
.AfterMap<TransactionCodeTranslation>();
}
}
This doesn't work. HOWEVER if I use option 2 directly:
var tc = await codeRepo.TableNoTracking.FirstOrDefaultAsync(x => x.ShortName == "[something]").ConfigureAwait(false);
var transactionCode = mapper.Map<TblLstCode, TransactionCodeResult>(tc);
Then it works correctly! But that would mean i would have to loop my result and map every object in the result again...
Is there a way to do it like option 1?
Thank you!
Edit 1:
Per Lucian Bargaoanu's request i added a BuildExecutionPlan:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<TblDatOperationalHUTrace, PalletResult>()
.ForMember(dest => dest.TransCode, act => act.MapFrom((src, _, transactionCode, ctx) =>
ctx.Mapper.Map<TblLstCode, TransactionCodeResult>(src.TransCode, opt =>
opt.AfterMap((source, destination) =>
destination.DisplayDesc = $"[{destination.ShortName}] {destination.DescToLanguageDesc(destination, language)}"))));
cfg.CreateMap<TblLstCode, TransactionCodeResult>()
.AfterMap<TransactionCodeTranslation>();
});
var expression = config.BuildExecutionPlan(typeof(TblDatOperationalHUTrace), typeof(PalletResult));
var expression2 = config.BuildExecutionPlan(typeof(TblLstCode), typeof(TransactionCodeResult));
Result of expression:
(src, dest, ctxt) =>
{
PalletResult typeMapDestination;
return (src == null)
? null
: {
typeMapDestination = dest ?? new PalletResult();
try
{
var resolvedValue = mappingFunction.Invoke(
src,
typeMapDestination,
typeMapDestination.TransCode,
ctxt);
var propertyValue = (resolvedValue == null) ? null : resolvedValue;
typeMapDestination.TransCode = propertyValue;
}
catch (Exception ex)
{
throw new AutoMapperMappingException(
"Error mapping types.",
ex,
AutoMapper.TypePair,
TypeMap,
PropertyMap);
return null;
}
return typeMapDestination;
};
}
Result Expression2:
(src, dest, ctxt) =>
{
TransactionCodeResult typeMapDestination;
return (src == null)
? null
: {
typeMapDestination = dest ?? new TransactionCodeResult();
try
{
var resolvedValue = ((src == null) || false) ? default(int) : src.Id;
typeMapDestination.Id = resolvedValue;
}
catch (Exception ex)
{
throw new AutoMapperMappingException(
"Error mapping types.",
ex,
AutoMapper.TypePair,
TypeMap,
PropertyMap);
return default(int);
}
try
{
var resolvedValue = ((src == null) || false) ? null : src.ShortName;
var propertyValue = (resolvedValue == null) ? null : resolvedValue;
typeMapDestination.ShortName = propertyValue;
}
catch (Exception ex)
{
throw new AutoMapperMappingException(
"Error mapping types.",
ex,
AutoMapper.TypePair,
TypeMap,
PropertyMap);
return null;
}
try
{
var resolvedValue = ((src == null) || false) ? default(int) : src.CodeTypeId;
typeMapDestination.CodeTypeId = resolvedValue;
}
catch (Exception ex)
{
throw new AutoMapperMappingException(
"Error mapping types.",
ex,
AutoMapper.TypePair,
TypeMap,
PropertyMap);
return default(int);
}
try
{
var resolvedValue = ((src == null) || false) ? null : src.DescLC;
var propertyValue = (resolvedValue == null) ? null : resolvedValue;
typeMapDestination.DescLC = propertyValue;
}
catch (Exception ex)
{
throw new AutoMapperMappingException(
"Error mapping types.",
ex,
AutoMapper.TypePair,
TypeMap,
PropertyMap);
return null;
}
try
{
var resolvedValue = ((src == null) || false) ? null : src.DescEN;
var propertyValue = (resolvedValue == null) ? null : resolvedValue;
typeMapDestination.DescEN = propertyValue;
}
catch (Exception ex)
{
throw new AutoMapperMappingException(
"Error mapping types.",
ex,
AutoMapper.TypePair,
TypeMap,
PropertyMap);
return null;
}
try
{
var resolvedValue = ((src == null) || false) ? null : src.DescFR;
var propertyValue = (resolvedValue == null) ? null : resolvedValue;
typeMapDestination.DescFR = propertyValue;
}
catch (Exception ex)
{
throw new AutoMapperMappingException(
"Error mapping types.",
ex,
AutoMapper.TypePair,
TypeMap,
PropertyMap);
return null;
}
try
{
var resolvedValue = ((src == null) || false) ? null : src.DescGE;
var propertyValue = (resolvedValue == null) ? null : resolvedValue;
typeMapDestination.DescGE = propertyValue;
}
catch (Exception ex)
{
throw new AutoMapperMappingException(
"Error mapping types.",
ex,
AutoMapper.TypePair,
TypeMap,
PropertyMap);
return null;
}
try
{
var resolvedValue = ((src == null) || false) ? null : src.DescNL;
var propertyValue = (resolvedValue == null) ? null : resolvedValue;
typeMapDestination.DescNL = propertyValue;
}
catch (Exception ex)
{
throw new AutoMapperMappingException(
"Error mapping types.",
ex,
AutoMapper.TypePair,
TypeMap,
PropertyMap);
return null;
}
afterFunction.Invoke(src, typeMapDestination, ctxt);
return typeMapDestination;
};
}
EDIT 2:
Ok, I actually found the solution. I was overcomplicating things for no reason...
I changed OperationalToPalletResultProfile to:
public class OperationalToPalletResultProfile : Profile
{
public OperationalToPalletResultProfile()
{
CreateMap<TblDatOperationalHUTrace, PalletResult>();
}
}
The TransactionCodeProfile remained the same:
public class TransactionCodeProfile : Profile
{
public TransactionCodeProfile()
{
CreateMap<TblLstCode, TransactionCodeResult>()
.AfterMap<TransactionCodeTranslation>();
}
}
And then where i use my mapping i changed this:
//results as TblDatOperationalHUTrace
var queryresult = await query
.Take(5000)
//.ProjectTo<PalletResult>(mapper.ConfigurationProvider) Don't do the mapping here anymore
.ToListAsync()
.ConfigureAwait(false);
//Map results to PalletResult
var values = mapper.Map<List<TblDatOperationalHUTrace>, List<PalletResult>>(queryresult); //Do the mapping here
Ok i actually found the solution myself:
I changed OperationalToPalletResultProfile to:
public class OperationalToPalletResultProfile : Profile
{
public OperationalToPalletResultProfile()
{
CreateMap<TblDatOperationalHUTrace, PalletResult>();
}
}
The TransactionCodeProfile remained the same:
public class TransactionCodeProfile : Profile
{
public TransactionCodeProfile()
{
CreateMap<TblLstCode, TransactionCodeResult>()
.AfterMap<TransactionCodeTranslation>();
}
}
And then where i use my mapping i changed this:
//results as TblDatOperationalHUTrace
var queryresult = await query
.Take(5000)
//.ProjectTo<PalletResult>(mapper.ConfigurationProvider) Don't do the mapping here anymore
.ToListAsync()
.ConfigureAwait(false);
//Map results to PalletResult
var values = mapper.Map<List<TblDatOperationalHUTrace>, List<PalletResult>>(queryresult); //Do the mapping here
And then it works like it's supposed to.

Sending notification to requester when PO is created in Acumatica

I need to be able to send an email to the original requester when a PO is created from a Requisition in Acumatica 6.1.
Per Acumatica, the Notification screen cannot handle this functionality, so I have this code to extend the POOrder Entry graph, which sends an email to the Customer's contact email from the Requisition when a PO is created (along with the RQRequisitionEntryExt trigger):
public class POOrderEntryExt : PXGraphExtension<POOrderEntry>
{
private bool sendEmailNotification = false;
public bool SendEmailNotification
{
get
{
return sendEmailNotification;
}
set
{
sendEmailNotification = value;
}
}
[PXOverride]
public void Persist(Action del)
{
using (var ts = new PXTransactionScope())
{
if (del != null)
{
del();
}
if (SendEmailNotification)
{
bool sent = false;
string sError = "Failed to send E-mail.";
try
{
Notification rowNotification = PXSelect<Notification,
Where<Notification.name, Equal<Required<Notification.name>>>>
.Select(Base, "PurchaseOrderNotification");
if (rowNotification == null)
throw new PXException("Notification Template was not found.");
var order = Base.Document.Current;
var requisition = (RQRequisition)PXSelect<RQRequisition,
Where<RQRequisition.reqNbr, Equal<Current<POOrder.rQReqNbr>>>>
.SelectSingleBound(Base, new object[] { order });
if (requisition.CustomerID != null)
{
var customer = (BAccountR)PXSelectorAttribute.Select<RQRequisition.customerID>(
Base.Caches[typeof(RQRequisition)], requisition);
if (customer != null)
{
var defCustContact = (Contact)PXSelectorAttribute.Select<BAccountR.defContactID>(
Base.Caches[typeof(BAccountR)], customer);
if (String.IsNullOrEmpty(defCustContact.EMail))
throw new PXException("E-mail is not specified for Customer Contact.");
var sender = TemplateNotificationGenerator.Create(order,
rowNotification.NotificationID.Value);
sender.RefNoteID = order.NoteID;
sender.MailAccountId = rowNotification.NFrom.HasValue ?
rowNotification.NFrom.Value :
PX.Data.EP.MailAccountManager.DefaultMailAccountID;
sender.To = defCustContact.EMail;
sent |= sender.Send().Any();
}
}
}
catch (Exception Err)
{
sent = false;
sError = Err.Message;
}
if (!sent)
throw new PXException(sError);
}
ts.Complete();
}
}
}
And this to modify RQRequisitionEntry:
public class RQRequisitionEntryExt : PXGraphExtension<RQRequisitionEntry>
{
public PXAction<RQRequisition> createPOOrder;
[PXButton(ImageKey = PX.Web.UI.Sprite.Main.DataEntry)]
[PXUIField(DisplayName = Messages.CreateOrders)]
public IEnumerable CreatePOOrder(PXAdapter adapter)
{
PXGraph.InstanceCreated.AddHandler<POOrderEntry>((graph) =>
{
graph.GetExtension<POOrderEntryExt>().SendEmailNotification = true;
});
return Base.createPOOrder.Press(adapter);
}
}
In order to send an email to the Requester's (Employee) contact email from the Request, I modified the POOrderEntryExt to pull the information from the Request object and the Employee's Contact email (I left the RQRequisitionEntryExt the same and in place):
public class POOrderEntryExt : PXGraphExtension<POOrderEntry>
{
private bool sendEmailNotification = false;
public bool SendEmailNotification
{
get
{
return sendEmailNotification;
}
set
{
sendEmailNotification = value;
}
}
[PXOverride]
public void Persist(Action del)
{
using (var ts = new PXTransactionScope())
{
if (del != null)
{
del();
}
if (SendEmailNotification)
{
bool sent = false;
string sError = "Failed to send E-mail.";
try
{
Notification rowNotification = PXSelect<Notification,
Where<Notification.name, Equal<Required<Notification.name>>>>
.Select(Base, "PurchaseOrderNotification");
if (rowNotification == null)
throw new PXException("Notification Template was not found.");
var order = Base.Document.Current;
var requisition = (RQRequisition)PXSelect<RQRequisition,
Where<RQRequisition.reqNbr, Equal<Current<POOrder.rQReqNbr>>>>
.SelectSingleBound(Base, new object[] { order });
var request = (RQRequest)PXSelectJoin<RQRequest,
InnerJoin<RQRequisitionContent,
On<RQRequisitionContent.orderNbr, Equal<RQRequest.orderNbr>>>,
Where<RQRequisitionContent.reqNbr, Equal<POOrder.rQReqNbr>>>
.SelectSingleBound(Base, new object[] { order });
if (request.EmployeeID != null)
{
var employee = (BAccountR)PXSelectorAttribute.Select<RQRequest.employeeID>(
Base.Caches[typeof(RQRequest)], request);
if (employee != null)
{
var defEmpContact = (Contact)PXSelectorAttribute.Select<BAccountR.defContactID>(
Base.Caches[typeof(BAccountR)], employee);
if (String.IsNullOrEmpty(defEmpContact.EMail))
throw new PXException("E-mail is not specified for Employee Contact.");
var sender = TemplateNotificationGenerator.Create(order,
rowNotification.NotificationID.Value);
sender.RefNoteID = order.NoteID;
sender.MailAccountId = rowNotification.NFrom.HasValue ?
rowNotification.NFrom.Value :
PX.Data.EP.MailAccountManager.DefaultMailAccountID;
sender.To = defEmpContact.EMail;
sent |= sender.Send().Any();
}
else
throw new PXException("Customer not found.");
}
else
throw new PXException("Request not found.");
}
catch (Exception Err)
{
sent = false;
sError = Err.Message;
}
if (!sent)
throw new PXException(sError);
}
ts.Complete();
}
}
}
I can get the original code to send an email in my development environment, but my modified code only returns the outer "Failed to send E-mail" error.
Can anyone help point me in the right direction to get my modifications to work?
Because in Acumatica there is one-to-many relationship between RQRequisition and RQRequest, I believe the better approach is to loop through all requests linked to the current requisition and compose a list of requester's emails. After that we can go ahead and send emails to all requesters as part of the Create Orders operation:
public class POOrderEntryExt : PXGraphExtension<POOrderEntry>
{
private bool sendEmailNotification = false;
public bool SendEmailNotification
{
get
{
return sendEmailNotification;
}
set
{
sendEmailNotification = value;
}
}
[PXOverride]
public void Persist(Action del)
{
using (var ts = new PXTransactionScope())
{
if (del != null)
{
del();
}
if (SendEmailNotification)
{
bool sent = false;
string sError = "Failed to send E-mail.";
try
{
Notification rowNotification = PXSelect<Notification,
Where<Notification.name, Equal<Required<Notification.name>>>>
.Select(Base, "PONotification");
if (rowNotification == null)
throw new PXException("Notification Template was not found.");
var order = Base.Document.Current;
var emails = new List<string>();
var requests = PXSelectJoinGroupBy<RQRequest,
InnerJoin<RQRequisitionContent,
On<RQRequest.orderNbr,
Equal<RQRequisitionContent.orderNbr>>>,
Where<RQRequisitionContent.reqNbr,
Equal<Required<RQRequisition.reqNbr>>>,
Aggregate<GroupBy<RQRequest.orderNbr>>>
.Select(Base, order.RQReqNbr);
foreach (RQRequest request in requests)
{
if (request.EmployeeID != null)
{
var requestCache = Base.Caches[typeof(RQRequest)];
requestCache.Current = request;
var emplOrCust = (BAccountR)PXSelectorAttribute
.Select<RQRequest.employeeID>(requestCache, request);
if (emplOrCust != null)
{
var defEmpContact = (Contact)PXSelectorAttribute
.Select<BAccountR.defContactID>(
Base.Caches[typeof(BAccountR)], emplOrCust);
if (!String.IsNullOrEmpty(defEmpContact.EMail) &&
!emails.Contains(defEmpContact.EMail))
{
emails.Add(defEmpContact.EMail);
}
}
}
}
foreach (string email in emails)
{
var sender = TemplateNotificationGenerator.Create(order,
rowNotification.NotificationID.Value);
sender.RefNoteID = order.NoteID;
sender.MailAccountId = rowNotification.NFrom.HasValue ?
rowNotification.NFrom.Value :
PX.Data.EP.MailAccountManager.DefaultMailAccountID;
sender.To = email;
sent |= sender.Send().Any();
}
}
catch (Exception Err)
{
sent = false;
sError = Err.Message;
}
if (!sent)
throw new PXException(sError);
}
ts.Complete();
}
}
}

Error while adding entity object to DBContext object called from Parallel.Foreach loop

Error :
If the event originated on another computer, the display information had to be saved with the event.
The following information was included with the event:
Object reference not set to an instance of an object. at
System.Data.Objects.ObjectStateManager.DetectConflicts(IList1
entries) at System.Data.Objects.ObjectStateManager.DetectChanges()
at System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean
force) at
System.Data.Entity.Internal.Linq.InternalSet1.ActOnSet(Action action,
EntityState newState, Object entity, String methodName) at
System.Data.Entity.Internal.Linq.InternalSet1.Add(Object entity)
at System.Data.Entity.DbSet1.Add(TEntity entity) at
ESHealthCheckService.BusinessFacade.BusinessOperationsLayer.AddErrorToDbObject(Exception
ex, Server serverObj, Service windowsServiceObj)
the message resource is present but the message is not found in the string/message table
public void CheckForServerHealth()
{
businessLayerObj.SetStartTimeWindowsService();
List<ServerMonitor> serverMonitorList = new List<ServerMonitor>();
serverList = businessLayerObj.GetServerList();
Parallel.ForEach(
serverList,
() => new List<ServerMonitor>(),
(server, loop, localState) =>
{
localState.Add(serverStatus(server, new ServerMonitor()));
return localState;
},
localState =>
{
lock (serverMonitorList)
{
foreach (ServerMonitor serverMonitor in localState)
{
serverMonitorList.Add(serverMonitor);
}
}
});
businessLayerObj.SaveServerHealth(serverMonitorList);
}
public ServerMonitor serverStatus(Server serverObj, ServerMonitor serverMonitorObj)
{
if (new Ping().Send(serverObj.ServerName, 30).Status == IPStatus.Success)
{
serverMonitorObj.Status = true;
try
{
PerformanceCounter cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total", serverObj.ServerName);
serverMonitorObj.CPUUtlilization = (cpu.NextValue());
}
catch (Exception ex)
{
businessLayerObj.AddErrorObjectToStaticList(ex, serverObj);
}
serverMonitorObj.ServerID = serverObj.ServerID;
try
{
string[] diskArray = serverObj.DriveMonitor.ToString().Split(':');
if (diskArray != null && diskArray.Contains("NA"))
{
serverMonitorObj.DiskSpace = "NA";
}
else
{
serverMonitorObj.DiskSpace = ReadFreeSpaceOnNetworkDrives(serverObj.ServerName, diskArray);
}
}
catch (Exception ex)
{
businessLayerObj.AddErrorObjectToStaticList(ex, serverObj);
}
serverMonitorObj.CreatedDateTime = DateTime.Now;
}
else
{
serverMonitorObj.Status = false;
serverMonitorObj.ServerID = serverObj.ServerID;
//return serverMonitorObj;
}
return serverMonitorObj;
}
public void AddErrorObjectToStaticList(Exception ex, Server serverObj = null, Service windowsServiceObj = null)
{
EShelathLoging esLogger = new EShelathLoging();
esLogger.CreateDatetime = DateTime.Now;
if (ex.InnerException != null)
{
esLogger.Message = (windowsServiceObj == null ? ex.InnerException.Message : ("Service Name : " + windowsServiceObj.ServiceName + "-->" + ex.InnerException.Message));
//esLogger.Message = "Service Name : " + windowsServiceObj.ServiceName + "-->" + ex.InnerException.Message;
esLogger.StackTrace = (ex.InnerException.StackTrace == null ? "" : ex.InnerException.StackTrace);
}
else
{
esLogger.Message = (windowsServiceObj == null ? ex.Message : ("Service Name : " + windowsServiceObj.ServiceName + "-->" + ex.Message));
//esLogger.Message = "Service Name : " + windowsServiceObj.ServiceName + "-->" + ex.Message;
esLogger.StackTrace = ex.StackTrace;
}
if (serverObj != null)
{
esLogger.ServerName = serverObj.ServerName;
}
try
{
lock (lockObject)
{
esHealthCheckLoggingList.Add(esLogger);
}
}
catch (Exception exe)
{
string logEntry = "Application";
if (EventLog.SourceExists(logEntry) == false)
{
EventLog.CreateEventSource(logEntry, "Windows and IIS health check Log");
}
EventLog eventLog = new EventLog();
eventLog.Source = logEntry;
eventLog.WriteEntry(exe.Message + " " + exe.StackTrace, EventLogEntryType.Error);
}
}
And then the below function is called to add objects from static list to the db object.
public void AddErrorToDbObject()
{
try
{
foreach (EShelathLoging eslogObject in esHealthCheckLoggingList)
{
lock (lockObject)
{
dbObject.EShelathLogings.Add(eslogObject);
}
}
}
catch (DbEntityValidationException exp)
{
string logEntry = "Application";
if (EventLog.SourceExists(logEntry) == false)
{
EventLog.CreateEventSource(logEntry, "Windows and IIS health check Log");
}
EventLog eventLog = new EventLog();
eventLog.Source = logEntry;
eventLog.WriteEntry(exp.Message + " " + exp.StackTrace, EventLogEntryType.Error);
}
catch (Exception exe)
{
string logEntry = "Application";
if (EventLog.SourceExists(logEntry) == false)
{
EventLog.CreateEventSource(logEntry, "Windows and IIS health check Log");
}
EventLog eventLog = new EventLog();
eventLog.Source = logEntry;
eventLog.WriteEntry(exe.Message + " " + exe.StackTrace, EventLogEntryType.Error);
}`enter code here`
}
DbSet<T> is not thread-safe, so you can't use it from multiple threads at the same time. It seems you're trying to fix that by using a lock, but you're doing that incorrectly. For this to work, all threads have to share a single lock object. Having separate lock object for each thread, like you do now, won't do anything.
Please note that I received the same exception with the application I was working on, and determined that the best way to resolve the issue was to add an AsyncLock, because of what #svick mentioned about how DbSet is not threadsafe. Thank you, #svick!
I'm guessing that your DbContext is inside your businessLayerObj, so here is what I recommend, using Stephen Cleary's excellent Nito.AsyncEx (see https://www.nuget.org/packages/Nito.AsyncEx/):
using Nito.AsyncEx;
// ...
private readonly AsyncLock _dbContextMutex = new AsyncLock();
public void CheckForServerHealth()
{
using (await _dbContextMutex.LockAsync().ConfigureAwait(false))
{
await MyDbContextOperations(businessLayerObj).ConfigureAwait(false);
}
}
private async Task MyDbContextOperations(BusinessLayerClass businessLayerObj)
{
await Task.Run(() =>
{
// operations with businessLayerObj/dbcontext here...
});
}

System.UnauthorizedAccessException was unhandled by user code, when deleting user alerts?

I am getting System.UnauthorizedAccessException was unhandled by user code when deleting user alerts by programming. It is working good in my QA farm. But not working in DEV farm.
I added application pool account to farm administation group and database users group as dbowner. Still getting same error.
protected void ChkBx41_CheckedChanged(object sender, EventArgs e)
{
SPUser user = SPContext.Current.Web.CurrentUser;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using(SPSite site=new SPSite(url.Text)){
using (SPWeb eweb = site.OpenWeb())
{
SPUser juser = null;
eweb.AllowUnsafeUpdates = true;
try
{
juser = eweb.AssociatedMemberGroup.Users[user.LoginName];
}
catch (Exception)
{
}
if (ChkBx41.Checked)
{
if (juser == null)
{
eweb.AssociatedMemberGroup.AddUser(eweb.EnsureUser(user.LoginName));
SPUtility.SendEmail(eweb, true, true, user.Email, "Welcome to " + eweb.Title, "Hello " + "<br><br>" + "Welcome to the " + eweb.Title + " ");
createalert(SPAlertFrequency.Daily, eweb, eweb.EnsureUser(user.LoginName),true);
}
RBList4.SelectedValue = "Daily";
RBList4.Enabled = true;
}
else
{
if (juser != null)
{
eweb.AssociatedMemberGroup.RemoveUser(juser);
removealert(eweb, juser);
RBList4.SelectedValue = null;
RBList4.Enabled = false;
}
}
eweb.AllowUnsafeUpdates = false;
}
}
});
}
public void removealert(SPWeb rweb, SPUser ruser)
{
bool oldCatchAccessDeniedException = rweb.Site.CatchAccessDeniedException;
try
{
SPUser cuser = rweb.EnsureUser(ruser.LoginName);
List<Guid> altid = new List<Guid>();
foreach (SPAlert alt in cuser.Alerts)
{
try
{
if (alt.AlertType == SPAlertType.List)
{
altid.Add(alt.ID);
}
}
catch (Exception) { }
}
rweb.Site.CatchAccessDeniedException = false;
foreach (Guid delid in altid)
{
cuser.Alerts.Delete(delid);
}
}
catch (UnauthorizedAccessException)
{
}
finally
{
rweb.Site.CatchAccessDeniedException = oldCatchAccessDeniedException;
}
}
Running from web part, try elevating?
public void removealert(SPWeb rweb, SPUser ruser) {
SPSecurity.RunWithElevatedPrivileges(delegate() {
using(SPSite csite = new SPSite(rweb.Site.Id)) {
using(SPWeb cweb = csite.OpenWeb(rweb.Id)) {
SPUser cuser = cweb.EnsureUser(ruser.LoginName);
List<Guid> altid = new List<Guid>();
foreach (SPAlert alt in cuser.Alerts) {
try {
if (alt.AlertType == SPAlertType.List) {
altid.Add(alt.ID);
}
} catch (Exception) {
}
}
foreach (Guid delid in altid) {
cuser.Alerts.Delete(delid);
}
}
}
});
}
I also would be interested to know where SPWeb rweb comes from. I hope not from SPContext, as you should not Dispose that.
I would recommend also having your SPWebs in a using block, in the same method block for easy reading.

Resources