Calculate Fields Using Python

3120
5
Jump to solution
09-09-2015 07:43 AM
RobertBradley1
New Contributor II

Hello,

I have some tables in our Enterprise GeoDatabase that are used in a Map Service for another application, Cityworks 2013. To link the table asset to other data created in Cityworks (Work orders, etc.), a particular field must be designated for linking. This field must be unique and unchanging for each asset row in the table, and also the application is being used to add new assets to said tables. When a new asset record is added by Cityworks it does not automatically calculate the field designated for linking in the correct method. It simply places an OBJECT ID in that field, but we need it to also have a combination of other attributes. When performing a mass upload of this tabular asset data into our GeoDatabase I employed Field Calculator to update these fields using a concatenation Python Expression: "%s-%s-%s" % ('CP', !Field1!, !Field2!)

I would like to automated this expression to update this field on a regular basis, but cannot figure out a way to only update new records that have been added by the application and not overwrite existing data. Any help would be greatly appreciated!

Robert

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Your expression would be:

CityworksID=
myFunction(!CityworksID!, !Field1!, !Field2!)

and the code block:

def myFunction(cw, f1, f2):
    if 'CP' not in cw:
        return cw # or some other default
    else:
        return 'CP-' + f1 + str(f2)

View solution in original post

0 Kudos
5 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Robert,

You could incorporate an IF statement to determine if the field is blank.  If it is, then you can run the calculation.  Ex:

screen1.PNG

0 Kudos
RobertBradley1
New Contributor II

Thanks Jake but in my statement 'CP' is not a field but just a text string I'm adding. Also I am only trying to update one field, called CityworksID not three. Basically I need it to look at that field, determine if it meets certain qualifications (i.e. maybe if it contains 'CP'), and if not update to a concatenated value using the string text 'CP' + !Field1! (an asset type)+ !Field2! (a number)

For example the final value would be CP-PUMP1-0001

Thanks for helping!!

0 Kudos
WesMiller
Regular Contributor III

I would use select by attribute to isolate the new records then run your field calculator.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Your expression would be:

CityworksID=
myFunction(!CityworksID!, !Field1!, !Field2!)

and the code block:

def myFunction(cw, f1, f2):
    if 'CP' not in cw:
        return cw # or some other default
    else:
        return 'CP-' + f1 + str(f2)
0 Kudos
RobertBradley1
New Contributor II

Thank you Darren!! I think this might just work. Didn't think about using myFunction

Going to test this on a test database right away.

0 Kudos