fix: change agreement status only if ongoing
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index 8ae9267..5beaea7 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -264,7 +264,8 @@
 		"erpnext.projects.doctype.project.project.update_project_sales_billing",
 		"erpnext.projects.doctype.project.project.send_project_status_email_to_users",
 		"erpnext.quality_management.doctype.quality_review.quality_review.review",
-		"erpnext.support.doctype.service_level_agreement.service_level_agreement.check_agreement_status"
+		"erpnext.support.doctype.service_level_agreement.service_level_agreement.check_agreement_status",
+		"erpnext.support.doctype.issue.issue.set_service_level_agreement_status"
 	],
 	"daily_long": [
 		"erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.update_latest_price_in_all_boms"
diff --git a/erpnext/support/doctype/issue/issue.py b/erpnext/support/doctype/issue/issue.py
index 7eeb783..37be95b 100644
--- a/erpnext/support/doctype/issue/issue.py
+++ b/erpnext/support/doctype/issue/issue.py
@@ -77,9 +77,9 @@
 
 	def update_agreement_status(self):
 		current_time = frappe.flags.current_time or now_datetime()
-		if self.service_level_agreement:
-			if (round(time_diff_in_hours(self.response_by, current_time), 2) < 0
-				or round(time_diff_in_hours(self.resolution_by, current_time), 2) < 0):
+		if self.service_level_agreement and self.agreement_status == "Ongoing":
+			if (round(time_diff_in_hours(self.response_by, current_time), 2) < 0 or
+				round(time_diff_in_hours(self.resolution_by, current_time), 2) < 0):
 				self.agreement_status = "Failed"
 			else:
 				self.agreement_status = "Fulfilled"
@@ -220,6 +220,17 @@
 
 	return current_date_time
 
+def set_service_level_agreement_status():
+	issues = frappe.get_list("Issue", filters={"status": "Open", "agreement_status": "Ongoing"})
+	for issue in issues:
+		doc = frappe.get_doc("Issue", issue.name)
+		if self.service_level_agreement and self.agreement_status == "Ongoing":
+			if (round(time_diff_in_hours(self.response_by, now_datetime()), 2) < 0 or
+				round(time_diff_in_hours(self.resolution_by, now_datetime()), 2) < 0):
+				frappe.db.set_value("Issue", doc.name, "agreement_status", "Failed")
+			else:
+				frappe.db.set_value("Issue", doc.name, "agreement_status", "Fulfilled")
+
 def get_list_context(context=None):
 	return {
 		"title": _("Issues"),
diff --git a/erpnext/support/doctype/issue/test_issue.py b/erpnext/support/doctype/issue/test_issue.py
index 5266678..8bf0f3a 100644
--- a/erpnext/support/doctype/issue/test_issue.py
+++ b/erpnext/support/doctype/issue/test_issue.py
@@ -48,18 +48,6 @@
 
 		self.assertEqual(issue.agreement_status, 'Fulfilled')
 
-		issue.status = 'Open'
-		issue.save()
-
-		frappe.flags.current_time = datetime.datetime(2019, 3, 5, 12, 0)
-
-		issue.status = 'Closed'
-		issue.save()
-
-		self.assertEqual(issue.agreement_status, 'Failed')
-
-
-
 def make_issue(creation=None, customer=None):
 
 	issue = frappe.get_doc({
diff --git a/erpnext/support/doctype/service_level_agreement/service_level_agreement.py b/erpnext/support/doctype/service_level_agreement/service_level_agreement.py
index 00f5cb3..e0cbc59 100644
--- a/erpnext/support/doctype/service_level_agreement/service_level_agreement.py
+++ b/erpnext/support/doctype/service_level_agreement/service_level_agreement.py
@@ -30,11 +30,9 @@
 	service_level_agreements = frappe.get_list("Service Level Agreement", filters=[
 		{"agreement_status": "Active"},
 		{"default_service_level_agreement": 0}
-	])
+	], fields=["name", "end_date"])
 
 	for service_level_agreement in service_level_agreements:
-		service_level_agreement = frappe.get_doc("Service Level Agreement", service_level_agreement)
-
 		if service_level_agreement.end_date < frappe.utils.getdate():
 			frappe.db.set_value("Service Level Agreement", service_level_agreement.name,
 				"agreement_status", "Expired")