Skip to main content
Answer

error "'Graph' is a variable but used like a type" when trying to dynamically get a graph full name

  • August 22, 2022
  • 3 replies
  • 138 views

aaghaei
Captain II
Forum|alt.badge.img+10

Hello Everyone,

 

I have a funtion that depending on what graph is calling it should do different stuff. I would like to pass the graph calling the function then compare it to some graphs and do what I need. Inside the function when I try to compare the input graph I get an error that "'Graph' is a variable but used like a type". Below is the code snippet. How can I make it work?

 

public static string TestFunction(PXGraph Graph)
{
if (Graph == null) return null;

if (typeof(Graph).FullName == typeof(APInvoiceEntry).FullName)
{
// Do stuff here
}

if (typeof(Graph).FullName == typeof(ChangeOrderEntry).FullName)
{
// Do stuff here
}

// and so on for some more graphs

return null;
}

I can get the graph FullName from the caller and pass to the function but I prefer not.

Best answer by Dioris Aguilar

@aaghaei Try the following code: 

public virtual void UpdateFSCacheInAPDoc(PXGraph graphHelper)
{
if (graphHelper is AppointmentEntry)
{
var appGraph = (AppointmentEntry)graphHelper;
...
}
else if (graphHelper is ServiceOrderEntry)
{
var soGraph = (ServiceOrderEntry)graphHelper;
...
}
}

It’s an actual method from SM_APInvoiceEntry extension.

3 replies

jinin
Pro I
Forum|alt.badge.img+11
  • Pro I
  • August 22, 2022

Hi @aaghaei 

If we use the ScreenUtils, will get all graph information based on the screenID.

We can get the screenID based on the accessinfo(Base.Accessinfo.ScreenID) 

 

Please try like below.

Add name space PX.API and PX.MetaData 


 if (ScreenUtils.ScreenInfo.TryGet("AR301000").GraphName == typeof(APInvoiceEntry).FullName)
            {
                // Do stuff here
            }

Hope this will help you :)


aaghaei
Captain II
Forum|alt.badge.img+10
  • Author
  • Captain II
  • August 22, 2022

Thanks @jinin. My goal is not to hard code variables otherwise I could just gt the graphname from the graph that calls the function and pass it as a parameter to function. Here you have hard coded the screenID. I want this to be dynamic based on whatever graph calls the function. Any other though?


Dioris Aguilar
Jr Varsity I
Forum|alt.badge.img+2
  • Jr Varsity I
  • Answer
  • August 22, 2022

@aaghaei Try the following code: 

public virtual void UpdateFSCacheInAPDoc(PXGraph graphHelper)
{
if (graphHelper is AppointmentEntry)
{
var appGraph = (AppointmentEntry)graphHelper;
...
}
else if (graphHelper is ServiceOrderEntry)
{
var soGraph = (ServiceOrderEntry)graphHelper;
...
}
}

It’s an actual method from SM_APInvoiceEntry extension.