> ## Documentation Index
> Fetch the complete documentation index at: https://docs.peliqan.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Write predictions to a SaaS app

> Integrate predictive ML models with SaaS apps and update fields in your business applications such as Salesforce.

Integrate predictive ML models with SaaS apps and update fields in your business applications.

The Peliqan platform allows you to **write predictions to a SaaS application** such as **Salesforce**. Use Writeback to **update fields or create new records (e.g. Tasks) to automate actions** based on predictions.

This tutorial is tailored for a [**Churn Prediction App**](/low-code-python-data-apps/predictions-machine-learning-templates/churn-prediction)**. We will create Tasks in Salesforce,** you can modify it to work with other models as well.

### Import required modules

```python theme={null}
from sklearn.feature_extraction.text import TfidfVectorizer
from joblib import load
import pandas as pd

# Loading models to make predictions
model = load('/data_app/model_churn_prediction')
encoder = load('/data_app/encoder_churn_prediction')
```

### Load new records

Load new records that do not have a prediction yet (prediction is null). Make sure you create a "Prediction" column first in the spreadsheet view of the table:

```python theme={null}
dbconn = pq.dbconnect(pq.DW_NAME)
my_query = 'select * from customer_churn where prediction is null limit 5'
df = dbconn.fetch(pq.DW_NAME, query = my_query, df = True)
```

### Data Preprocessing & Predicting

For predicting, we have to **prepare the data** in the same format as it was trained on.

```python theme={null}
X = df.drop(['Prediction', 'Churn', 'CustomerName'], axis=1)
# converting categorical columns to numeric
cat_cols = X.select_dtypes('object').columns
X[cat_cols] = X[cat_cols].apply(lambda x: encoder[x.name].transform(x))

# Making prediction
prediction = model.predict(X)
proba = model.predict_proba(X)[0][prediction]
result = pd.DataFrame({'CustomerID':X['CustomerID'], 'Customer Name':df['CustomerName'],'Prediction': prediction, })#'Probability': proba})

st.dataframe(result)
```

<Note>
  It's recommended to add Try/Except statement to capture the error when there are no records to update.
</Note>

### Task creation in Salesforce

```python theme={null}
for _, record in results.iterrows()
	if record['Prediction'] == 1:
		# create a Task in Salesforce for follow up by an Sales Account Manager
		Salesforce.add("task", title = "Reach out to " + record["name"])
```

[Click here](/low-code-python-data-apps/writeback-reverse-etl-data-sync) to learn more about write-back to SaaS apps.

<details>
  <summary>Expand this to see the full code</summary>

  ```python theme={null}
  from sklearn.feature_extraction.text import TfidfVectorizer
  from joblib import load
  import pandas as pd

  # Loading models to make predictions
  model = load('/data_app/model_churn_prediction')
  encoder = load('/data_app/encoder_churn_prediction')

  # Load new records
  my_query = 'select * from customer_churn where prediction is null limit 5'
  dbconn = pq.dbconnect('dw_123')
  df = dbconn.fetch('dw_123', query = my_query, df = True)

  # Data Preprocessing & Predicting
  X = df.drop(['Prediction', 'Churn', 'CustomerName'], axis=1)
  cat_cols = X.select_dtypes('object').columns
  X[cat_cols] = X[cat_cols].apply(lambda x: encoder[x.name].transform(x))

  prediction = model.predict(X)
  proba = model.predict_proba(X)[0][prediction]
  result = pd.DataFrame({'CustomerID':X['CustomerID'], 'Customer Name':df['CustomerName'],'Prediction': prediction, })#'Probability': proba})

  st.dataframe(result)

  # Task creation in Salesforce
  for _, record in results.iterrows():
  	if record['Prediction'] == 1:
  		# create a Task in Salesforce for follow up by an Sales Account Manager
  		Salesforce.add("task", title = "Reach out to " + record["name"])
  ```
</details>

## What's Next

1. You can make real-time predictions on new incoming data and send **alerts to slack** if the model makes a prediction **above a certain threshold**. Learn more about [sending critical notifications to slack](/low-code-python-data-apps/predictions-machine-learning-templates/send-alerts-to-slack)**.**
2. Using **Peliqan** you can **create an app for business users to consume** the model you have made in a **simple and intuitive UI**. Learn more about [creating apps for business users to consume your model](/low-code-python-data-apps/predictions-machine-learning-templates/create-an-app-for-business-users-to-get-predictions)**.**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.