Feature Class does not get updated with a Geoprocessing Service

Discussion created by ionarawilson on Sep 26, 2013
Latest reply on Sep 26, 2013 by ionarawilson
When I run a python script, the feature class in a SDE feature class gets updated, but when I run a geoprocessing service created from the same script, the same feature class does not get updated. All the fields are blank. Does anybody know why?

#Import modules
import os, sys, arcpy, traceback, arcgisscripting

#Set Overwrite Option
arcpy.env.overwriteOutput = True
#arcpy.env.workspace = "d:/ArcGISData/SARS/Temp"

gp = arcgisscripting.create(10.1)
#del row
#del rows

#Sets parameters (attributes)
Status = gp.GetParameterAsText(0)
Office =  gp.GetParameterAsText(1)
Forester = gp.GetParameterAsText(2)
DateStart = gp.GetParameterAsText(3)
PlanLength = gp.GetParameter(4)
PlanEQIP = gp.GetParameter(5)
RecipientLast = gp.GetParameterAsText(6)
RecipientFirst = gp.GetParameterAsText(7)
MailAddress = gp.GetParameterAsText(8)
City = gp.GetParameterAsText(9)
State = gp.GetParameterAsText(10)
ZipCode = gp.GetParameterAsText(11)
Phone = gp.GetParameterAsText(12)
Email = gp.GetParameterAsText(13)
Underserved = gp.GetParameter(14)
Stewardship = gp.GetParameterAsText(15)

# This is to send the data input to the database
with arcpy.da.UpdateCursor(Stewardship, ("Status", "Office", "Forester", "DateStart","PlanLength", "PlanEQIP", "RecipientLast", "RecipientFirst",
     "MailAddress", "City" , "State", "ZipCode", "Phone", "Email", "Underserved")) as rows:
        # row comes back as a tuple in the order specified here, so Office is row[0], Forester is row[1]
        for row in rows:
            row[0] = Status
            row[1] = Office
            row[2] = Forester
            row[3] = DateStart
            row[4] = PlanLength
            row[5] = PlanEQIP
            row[6] = RecipientLast
            row[7] = RecipientFirst
            row[8] = MailAddress
            row[9] = City
            row[10] = State
            row[11] = ZipCode
            row[12] = Phone
            row[13] = Email
            row[14] = Underserved

# This is to save only the year to the FFY field from the DateStart field
with arcpy.da.UpdateCursor(Stewardship, ("FFY")) as rows:
        # row comes back as a tuple in the order specified here, so FFY is row[0]
       # To strip the date start depending on the lenght of the date

        Datestarstr1 = str(DateStart)

        if len(Datestarstr1) == 9:
            # if for example 6/28/2013 or 10/4/2013
                yearonly = DateStart[5:9]

        # if for example 6/8/2013
        if len(Datestarstr1) == 8:
                yearonly = DateStart[4:8]
        if len(Datestarstr1) > 9:
             # if for example 10/10/2013
                yearonly = DateStart[6:10]
        for row in rows:
            row[0] = yearonly


Counties = "d:/ArcGISData/SARS/Python_10April2013/SARS.gdb/Counties"

arcpy.MakeFeatureLayer_management(Counties, "Countieslayer")

arcpy.MakeFeatureLayer_management(Stewardship, "Stewardshiplayer")

outstewardshipcentroids = "d:/ArcGISData/SARS/Temp/outstewardshipcentroids.shp"

    # Create centroid
arcpy.FeatureToPoint_management("Stewardshiplayer",  outstewardshipcentroids,"INSIDE")

arcpy.MakeFeatureLayer_management(outstewardshipcentroids, "Stewardshipcentroidslayer")

arcpy.SelectLayerByLocation_management("Countieslayer", "CONTAINS", "Stewardshipcentroidslayer", "", "NEW_SELECTION")

    # Takes the first row of the cursor (there is only one object in the county centroid).
    # The first index is the first row, the next [0] index is the first attribute, which there is only one (FIPS_TXT)
countycode = tuple(arcpy.da.SearchCursor("Countieslayer", "FIPS_TXT"))[0][0]
urows = arcpy.da.UpdateCursor("Stewardshiplayer", "County")
for urow in urows:
    urow[0] = countycode

del urows, urow

# To add 1 to the year if month starts in October

import time
from datetime import datetime, date
#sim_date_counter = 1

with arcpy.da.UpdateCursor("Stewardshiplayer", ("FFY")) as rows:
  for row in rows:
    #get the value of the date

    FFY = row[0]
    FFYnumber = int(FFY)
    #convert to string
    DateStartstr = str(DateStart)

    # Get only the month
    # To get only the first two characters:
    DateStart2 = DateStartstr[0:2]

    if "/" in DateStart2:
                # if for example 6/28/2013
            DateStart3 = DateStart[0:1]
            FFYnumbercount = FFYnumber
    if "/" not in DateStart2:
         # if for example 10/10/2013
        DateStart4 = int(DateStart2)
        FFYnumbercount = FFYnumber + 1

    row[0] = str(FFYnumbercount)


# We get the value of FFY and County
trows = arcpy.SearchCursor("Stewardshiplayer")

for row in trows:
##               FFYCountytxt = row.getValue("FFYCounty")
           FFYtxt = row.getValue("FFY")
           Countytxt = row.getValue("County")

del trows, row

# we list the layer to create a layer feature without a selection (because it is selected by the user)
#lyr = arcpy.mapping.ListLayers(mxd, 'Stewardship')[0]

#arcpy.MakeFeatureLayer_management(lyr.dataSource, "TMP")
# To get the total ammount of records, not only the selected

Stewardshipfc = "Stewardship2"
arcpy.MakeFeatureLayer_management(Stewardshipfc , "TMP2")

result = int(arcpy.GetCount_management("TMP2").getOutput(0))

# write a query to get the county and year using the FFY and County of the object id of the Stewardship layer (the one that is selected)

query = "FFY" +"=" + "'"+ str(FFYtxt)+"'" + "AND " + "County" +"=" + "'"+ str(Countytxt)+"'"

# select all the records that have the FFYcounty the same as the FFYCounty of the hightest ObjectID
arcpy.SelectLayerByAttribute_management("TMP2",  "NEW_SELECTION", query )
result = int(arcpy.GetCount_management("TMP2").getOutput(0))

# if there are more than 1 records with the same ffy and county
# select only the record witht the highest sequencenumber (sort first, then select)
if  result > 1:
            maxValue4 = arcpy.SearchCursor("TMP2", "", "", "", "SequenceNumber D").next().getValue("SequenceNumber")
            query = "SequenceNumber = "
            arcpy.SelectLayerByAttribute_management("TMP2",  "SUBSET_SELECTION", "SequenceNumber = " + str(maxValue4))
            result = int(arcpy.GetCount_management("TMP2").getOutput(0))

# if there is no other record with the same FFY and County, we don't need to select it


    #  to get the sequence number
rows = arcpy.SearchCursor("TMP2")

for row in rows:

       Seqnum = row.SequenceNumber

# If the sequence number is equal or higher than 1 it means there are other records with the same FFY and County, so we add 1 to the number

with arcpy.da.UpdateCursor("Stewardshiplayer", ("Sequencenumber")) as rows:

        if Seqnum >= 1:

                vsn = Seqnum + 1
                vsn = 1
        for row in rows:

           row[0] = vsn

# we Create the Plan ID concatenating the FFY, the county and the Sequence number, adding zeros in front of the sequence number if necessary to make the lenght always 3
with arcpy.da.UpdateCursor(Stewardship, ("PlanID")) as rowsffycounty2:
    for row in rowsffycounty2:
        row[0] = str(FFYtxt) + "-" + str(Countytxt) + "-" + str(vsn).zfill(3)