Having Issue on Field Calculator With Python in ArcMap - arcgis

I have tried all of below snippets to use Python Parser in Field Calculator and update the values of a field called type based on a filed called MamerMN but in all of them I am getting Syntax error in Geoprocessing result window!
if !MamerMN! <= 0.151560:
return 1
and
if (!MamerMN! <= 0.151560):
return 1
and
if (MamerMN <= 0.151560):
return 1
and
def(MamerMN)
if MamerMN <= 0.151560:
return 1
and
def(MamerMN)
if (MamerMN <= 0.151560):
return 1
Can you please let me know what I am doing wrong?

You are writing your functions wrong. In the Field Calculator, make sure you check 'Show Codeblock' so the Pre-Logic Script Code box appears. Inside the Pre-Logic box, write your function-
def calcValue(mamerMN):
if mamerMN <= 0.151560:
return 1
and then in the box under, where it says the field_name =
you should write the name of the function and the field value you are passing to it surrounded by exclamations. So if your function above is named calcValue you would write
calcValue(!mamerMN!)

Without seeing a graphic of what your Field Calculator looks like, I suspect #csterling is probably right. However, an alternative is to just Select by Attribute where "mamerMN" <= 0.151560, then Field Calculate your selected features to 1 the regular way without necessitating a codeblock.

Related

Odoo force a field higher than x

In sales order, there is a field discount %. Is it possible to ensure the users only input value lower than x and it will not accept any value higher than x.
You can achieve this with creating a function using #api.onchange('discount') decorator on python code to ensure discount value is not higher than x, if higher then set x to discount field and also possible to show warning popup from that function.
But if python code change is not prefered, you can also achieve this with Automated action where you create a new rule on sale.order.line, set trigger On form modification, action Execute python code and add the following code
if record.discount > 10:
record.discount = 10
Where 10 is the value of x. This will ensure discount is always less than x.
Op1: If you want to ensure that behaviour you can add an sql constraint to the database, this will work fine if your X value never change, something like this:
_sql_constraints = [
('discount_less_than_X', 'CHECK(discount < X)', 'The discount value should be less than X'),
]
This constraint will trigger in all cases(create, write, import, SQL insert).
Replace X with the value desired.
Op2: Use decorator api.constrains, get X value from somewherelse and apply the restriccion, something like this:
#api.constrains('discount')
def _check_discount(self):
# Create a key:value parameter in `ir.config_parameter` table before.
disscount_max_value = self.env['ir.config_parameter'].sudo().get_param('disscount_max_value')
for rec in self:
if rec.disscount > int(disscount_max_value):
raise ValidationError('The discount value should be less than {}'.format(disscount_max_value))
I hope this answer can be helful for you.

Looking for a way to get a detailed description of ZTERM

I am currently trying to program a function module which should, in theory, output a custom table like T052, but with an additional field Z_TEXTLONG, which explains the details of the chosen ZTERM, akin to the text in FI_F4_ZTERM's popup. Here's what I tried:
LOOP AT T_ZBEDS ASSIGNING FIELD-SYMBOL(<line>).
CALL FUNCTION 'FI_F4_ZTERM'
EXPORTING
I_KOART = 'K'
I_ZTERM = <line>-zterm
I_XSHOW = ''
I_ZTYPE = ''
I_NO_POPUP = 'X'
IMPORTING
E_ZTERM = v_text
EXCEPTIONS
NOTHING_FOUND = 1
OTHERS = 2.
WRITE v_text TO <line>-Z_TEXTLONG.
From what I gathered, this does not work due to FI_F4_ZTERM writing the list it returns into E_ZTERM, not a single value, which would be what I need. I am a bit lost as to what I should do next. I tried looking into how exactly FI_F4_ZTERM generates these texts or where it calls them from, but I was not successful. Currently, I am trying to maybe get this text from V_T052, but that does not work either. I would be thankful for any suggestions.
Try function FI_TEXT_ZTERM, it has single import parameter I_T052
why not just call CALL FUNCTION 'FI_F4_ZTERM'
with I_NO_POPUP = 'X'
importing
ET_ZTERM = lt_zterm.
you get the list of payment terms and their texts.
Then in the loop read it from the table of payment terms.
LOOP AT T_ZBEDS ASSIGNING FIELD-SYMBOL(<line>).
read table lt_zterm assigning <term>
with key ZTERM = <line>-zterm.
ENDLOOP.

Can't Access a global view table with SQL by a selected field code converted from a dropdown selection

I am using a SQL notebook in Databricks/Spark community edition
%python
education_DF = sqlContext.sql('select * from global_temp.population_Globaltmp_view where `Education` = "2YD"')
display(education_DF)
The above code cell works fine and retrieves the desired rows from the view.
However the following code cell gives an error.
I am supbstituting the hard-coded value "2YD" in the WHERE clause, with a variable call education_choice that contains the same value.
It seems that I am not using variables correctly in SQL. How would I make this work?
%python
education_DF = sqlContext.sql('select * from global_temp.population_Globaltmp_view where `Education` = education_choice')
display(education_DF)
(This is the error)
org.apache.spark.sql.AnalysisException: cannot resolve '`education_choice`' given input columns: [global_temp.population_globaltmp_view.Salary, global_temp.population_globaltmp_view.Address, global_temp.population_globaltmp_view.Race, global_temp.population_globaltmp_view.MiddleI, global_temp.population_globaltmp_view.Education, global_temp.population_globaltmp_view.HairColor, global_temp.population_globaltmp_view.Age, global_temp.population_globaltmp_view.FullName, global_temp.population_globaltmp_view.City, global_temp.population_globaltmp_view.FirstName, global_temp.population_globaltmp_view.State, global_temp.population_globaltmp_view.LastName, global_temp.population_globaltmp_view.Height, global_temp.population_globaltmp_view.Fertility, global_temp.population_globaltmp_view.Employment, global_temp.population_globaltmp_view.Zip, global_temp.population_globaltmp_view.Weight, global_temp.population_globaltmp_view.Gender]; line 1 pos 72;
I tried the injection method suggested, but got a slightly different error this time. I included a screen shot of the relevant code cells. It seems that the education_choice "2YD" isn't being recognized as a literal but a field or something like that.
Shouldn't 2YD have quotes around it like "2YD"
If I hard code the WHERE clause like this: WHERE Education = "2YD" the queries works fine.
The image bellow is small but if you right click on it and "open in new tap, it is very readable.
*EDIT
You use the variable education_choice as a hardcoded string. Instead, you should inject the value of education_choice inside the SQL expression string like that.
*edit - wrap the variable with quoets.
%python
education_DF = sqlContext.sql('select * from global_temp.population_Globaltmp_view where `Education` = "{}"'.format(education_choice))
display(education_DF)

error in google datastudio on using custom query

I am using a simple sql block of statements to execute and return a set of results in big query, This is working fine in big query and getting the results,I need to export this data to data studio, so in data studio i use bigquery as connector and select the project and custom query and in that I paste the contents below:
Declare metricType String;
SET metricType="compute.googleapis.com/instance/cpu/utilization";
BEGIN
IF (metricType="compute.googleapis.com/instance/cpu/usage_time")
THEN
SELECT m.value as InstanceName,metric.type as metricType,point.value.double_value as usagetime,point.interval.start_time as StartTime,point.interval.end_time as EndTime,h.value as instance_id FROM `myproject.metric_export.sd_metrics_export_fin`, unnest(resource.labels) as h,unnest(metric.labels) as m where metric.type='compute.googleapis.com/instance/cpu/usage_time' and h.key="project_id";
ELSE IF (metricType="compute.googleapis.com/instance/cpu/utilization")
THEN
SELECT m.value as InstanceName,metric.type as metricType,point.value.double_value as utilizationrate,point.interval.start_time as
StartTime,point.interval.end_time as EndTime,h.value as instance_id FROM `myproject-.metric_export.sd_metrics_export_fin`,unnest(resource.labels) as h,unnest(metric.labels) as m where metric.type='compute.googleapis.com/instance/cpu/utilization' and h.key="project_id";
END IF;
END IF;
END;
but after click "ADD" button i get the below error:
I am not sure what is this error about? I have not used any stored procedure and I am just pasting it as custom query.
Also If I try to save the results of the BigQuery into a view from the Bigquery console results pane, it gives me the error,
Syntax error: Unexpected keyword DECLARE at [1:1]
I am extremely new to datastudio and also to bigquery. Kindly Help thanks
First, I would like to make some considerations about your query. You are using Scripting in order to declare and create a loop within your query. However, since you declare and set the metricsType in the beginning of the query, it will never enter in the first IF. This happens because the value is set and it is not looping through anything.
I would suggest you to use CASE WHEN instead, as below:
SELECT m.value as InstanceName,metric.type as metricType,
CASE WHEN metric.type = #parameter THEN point.value.double_value ELSE 0 END AS usagetime,
CASE WHEN metric.type = #parameter THEN point.value.double_value ELSE 0 END AS utilizationrate,
point.interval.start_time as StartTime,point.interval.end_time as EndTime,h.value as instance_id
FROM `myproject.metric_export.sd_metrics_export_fin`, unnest(resource.labels) as h,unnest(metric.labels) as m
WHERE metric.type=#parameter and h.key="project_id";
Notice that I am using the concept of Parameterized queries. Also, for this reason this query won't work in the console. In addition, pay attention that whem you set the #parameter to "compute.googleapis.com/instance/cpu/utilization", it will have a non-null column with the usagetime and a null column named utilizationrate.
Secondly, in order to add a new data source in DataStudio, you can follow this tutorial from the documentation. After, selecting New Report, click on the BigQuery Connector > Custom Query> Write your Project id, you need to click in ADD PARAMETER (below the query editor). In the above query, I would add:
Name: parameter
Display name: parameter
Data type: text
Default value: leave it in blank
Check the box Allow "parameter" to be modified in reports. This means you will be able to use this parameter as a filter and modify its value within the reports.
Following all the steps above, the data source will be added smoothly.
Lastly, I must point that if your query ran in the Console you are able to save it as a view by clicking on Save view, such as described here.

Zoho Creator making a custom function for a report

Trying to wrap my head around zoho creator, its not as simple as they make it out to be for building apps… I have an inventory database, and i have four fields that I call to fill a field called Inventory Number (Inv_Num1) –
First Name (First_Name)
Last Name (Last_Name)
Year (Year)
Number (Number)
I have a Custom Function script that I call through a Custom Action in the form report. What I am trying to do is upload a CSV file with 900 entries. Of course, not all of those have those values (first/last/number) so I need to bulk edit all of them. However when I do the bulk edit, the Inv_Num1 field is not updated with the new values. I use the custom action to populate the Inv_Num1 field with the values of the other 4 fields.
Heres is my script:
void onetime.UpdateInv()
{
for each Inventory_Record in Management
{
FN = Inventory_Record.First_Name.subString(0,1);
LN = Inventory_Record.Last_Name.subString(0,1);
YR = Inventory_Record.Year.subString(2,4);
NO = Inventory_Record.Number;
outputstr = FN + LN + YR + NO;
Inventory_Record.Inv_Num1 = outputstr;
}
}
I get this error back when I try to run this function
Error.
Error in executing UpdateInv workflow.
Error in executing For Each Record task.
Error in executing Set Variable task. Unable to update template variable FN.
Error evaluating STRING expression :
Even though there is a First Name for example, it still thinks there is none. This only happens on the fields I changed with Bulk Edit. If I do each one by hand, then the custom action works—but of course then the Inv_Num1 is already updated through my edit on success functions and makes the whole thing moot.
this may be one year late, you might have found the solution but just to highlight, the error u were facing was just due to the null value in first name.
you just have put a null check on each field and u r good to go.
you can generate the inv_number on the time of bulk uploading also by adding null check in the same code on and placing the code on Add> On Submt.( just the part inside the loop )
the Better option would be using a formula field, you just have to put this formula in that formula field and you'll get your inventory_number autogenerated , you can rename the formula_field to Inv Number or whaterver u want.
Since you are using substring directly in year Field, I am assuming the
year field as string.else you would have to user Year.tostring().substring(2,4) & instead of if(Year=="","",...) you have to put if(Year==null , null,...);
so here's the formula
if(First_Name=="","",First_Name.subString(0,1))+if(Last_Name =="","",Last_Name.subString(0,1)) + if(Year=="","",Year.subString(2,4)+Number
Let me know ur response if u implement this.
Without knowing the datatype its difficult to fix, but making the assumption that your Inventory_Record.number is a numeric data item you are adding a string to a number:
The "+" is used for string Concatenation - Joiner but it also adds two numbers together so think "a" + "b" = "ab" for strings but for numbers 1 + 2 = 3.
All good, but when you do "a" + 2 the system doesn't know whether to add or concatenate so gives an error.