Skip to main content
Answer

Error When Creating Opportunity through Customization

  • August 23, 2022
  • 2 replies
  • 105 views

Forum|alt.badge.img+1

Hello,

I am currently trying to add a button to Cases where I can create an opportunity. See below for graph initiation and insert. Note that this is proof of concept.

    #region Actions

public PXAction<PX.Objects.CR.CRCase> CreateOpportunity;

[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "Create Opportunity")]
protected void createOpportunity()
{
CRCase cCase = Base.Case.Current;

OpportunityMaint graph = PXGraph.CreateInstance<OpportunityMaint>();

CROpportunity opty = new CROpportunity
{
BAccountID = cCase.CustomerID,
ContactID = cCase.ContactID,
//Status = "N",
//ClassID = "SERVICE",
//StageID = "L",
Subject = cCase.Subject
};

graph.Opportunity.Insert(opty);

if (graph != null)
{
PXRedirectHelper.TryRedirect(graph, PXRedirectHelper.WindowMode.NewWindow);
}
}

#endregion

However, I’m getting an odd error when I try to execute. See below for snip.

Any help regarding this error would be greatly appreciated.

Best answer by Naveen Boga

Hi @rhooper91  Please use the below code, I have verified it from my end and it is working as expected.

 

 public class CRCaseMaintExt : PXGraphExtension<CRCaseMaint>
{
public PXAction<PX.Objects.CR.CRCase> CreateOpportunity;
[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "Create Opportunity")]
protected IEnumerable createOpportunity(PXAdapter adapter)
{

CRCase cCase = Base.Case.Current;
PXLongOperation.StartOperation(Base, delegate ()
{
OpportunityMaint graph = PXGraph.CreateInstance<OpportunityMaint>();
if (cCase != null)
{

CROpportunity objcROpportunity = new CROpportunity
{
Status = "L",
StageID = "P"
};

objcROpportunity.Subject = "Test Subject";
// objcROpportunity.BAccountID = cCase.CustomerID;
graph.Opportunity.Cache.Insert(objcROpportunity);
CRContact objCrContact = graph.Opportunity_Contact.Select();
objCrContact.Email = "Test@Test.com";
objCrContact.FirstName = "Naveen";
graph.Opportunity_Contact.Cache.Update(objCrContact);
graph.Actions.PressSave();

objcROpportunity.BAccountID = cCase.CustomerID;
graph.Opportunity.Cache.Update(objcROpportunity);
graph.Actions.PressSave();
PXRedirectHelper.TryRedirect(graph, PXRedirectHelper.WindowMode.Same);


}
});
return adapter.Get();
}
}

 

 

2 replies

Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • Answer
  • August 23, 2022

Hi @rhooper91  Please use the below code, I have verified it from my end and it is working as expected.

 

 public class CRCaseMaintExt : PXGraphExtension<CRCaseMaint>
{
public PXAction<PX.Objects.CR.CRCase> CreateOpportunity;
[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "Create Opportunity")]
protected IEnumerable createOpportunity(PXAdapter adapter)
{

CRCase cCase = Base.Case.Current;
PXLongOperation.StartOperation(Base, delegate ()
{
OpportunityMaint graph = PXGraph.CreateInstance<OpportunityMaint>();
if (cCase != null)
{

CROpportunity objcROpportunity = new CROpportunity
{
Status = "L",
StageID = "P"
};

objcROpportunity.Subject = "Test Subject";
// objcROpportunity.BAccountID = cCase.CustomerID;
graph.Opportunity.Cache.Insert(objcROpportunity);
CRContact objCrContact = graph.Opportunity_Contact.Select();
objCrContact.Email = "Test@Test.com";
objCrContact.FirstName = "Naveen";
graph.Opportunity_Contact.Cache.Update(objCrContact);
graph.Actions.PressSave();

objcROpportunity.BAccountID = cCase.CustomerID;
graph.Opportunity.Cache.Update(objcROpportunity);
graph.Actions.PressSave();
PXRedirectHelper.TryRedirect(graph, PXRedirectHelper.WindowMode.Same);


}
});
return adapter.Get();
}
}

 

 


Forum|alt.badge.img+1
  • Author
  • Varsity III
  • August 23, 2022

@Naveen Boga - that did it. I modified it a little bit, but I no longer get the error.

You are the best, I appreciate the time you spend into this and all my other questions!