# Salesforce

## Create Salesforce Opportunity

{% embed url="<https://youtu.be/15KbJKPVrtM?si=OnfvPpcM9jDG1v2F>" %}

The purpose of this example is to create a [Salesforce Opportunity](https://help.salesforce.com/s/articleView?id=sales.opportunities.htm\&type=5) when a Project is requested in AttackForge.

This example Flow can be downloaded from our [Flows GitHub Repository](https://github.com/AttackForge/Flows) and [imported](#importing-exporting-flows) into your AttackForge.

**Initial Set Up**

* **Event**: Project Requested
* **Secrets**:
  * sf\_client\_id - your [Salesforce OAuth Client Id](https://help.salesforce.com/s/articleView?id=xcloud.remoteaccess_oauth_flows.htm\&type=5)
  * sf\_client\_secret - your [Salesforce OAuth Client Secret](https://help.salesforce.com/s/articleView?id=xcloud.remoteaccess_oauth_flows.htm\&type=5)
  * sf\_password - Your [Salesforce User Password](https://help.salesforce.com/s/articleView?id=xcloud.remoteaccess_oauth_flows.htm\&type=5)
  * sf\_username - Your [SalesForce Username](https://help.salesforce.com/s/articleView?id=xcloud.remoteaccess_oauth_flows.htm\&type=5)

**Action 1 - Get Salesforce OAuth Token**

* **Method**: POST
* **URL**: <https://login.salesforce.com/services/oauth2/token>
* **Headers**:
  * Key = Content-Type; Type = Value; Value = application/x-www-form-urlencoded
* **Request Script**:

```javascript
return {
  request: {
    body: 'grant_type=password&client_id=' + secrets.sf_client_id + '&client_secret=' + secrets.sf_client_secret + '&username=' + secrets.sf_username + '&password=' + secrets.sf_password
  }, 
  data: {
    project_request: data
  } 
};
```

* **Response Script**:

```javascript
let body;

if (response.headers['Content-Type'] === 'application/json;charset=UTF-8') {
  body = JSON.parse(response.body);
}
else {
  return {
    decision: {
      status: 'abort',
      message: 'Content-Type is expected to be application/json;charset=UTF-8'
    }
  };
}

if (body?.access_token) {
  return {
    data: {
      project_request: data.project_request,
      sf_access_token: body.access_token
    }  
  };
}
else {
  return {
    decision: {  
      status: 'abort',
      message: 'Salesforce access token not found'
    }
  };
}
```

**Action 2 - Create Salesforce Opportunity**

* **Method**: POST
* **URL**: https\://\<YOUR-SALESFORCE>/services/data/v63.0/sobjects/Opportunity
* **Headers**:
  * Key = Content-Type; Type = Value; Value = application/json
* **Request Script**:

```javascript
if (data?.project_request && data?.sf_access_token) {
  let closeDate = '2030-12-31';
  
  if (data.project_request.project_request_end_date) {
    closeDate = String.slice(data.project_request.project_request_end_date, 0, 10);
  }

  return {
    request: {
      headers: {
        'Authorization': 'Bearer ' + data.sf_access_token
      },
      body: {
        'Name': data.project_request.project_request_name,
        'StageName': 'Needs Analysis',
        'CloseDate': closeDate
      }
    }
  };
}
else {
  return {
    decision: {  
      status: 'abort',
      message: 'Salesforce access token not found'
    }
  };
}
```

* **Response Script**:

```javascript
let body;

if (response.headers['Content-Type'] === 'application/json;charset=UTF-8') {
  body = JSON.parse(response.body);
}
else {
  return {
    decision: {
      status: 'abort',
      message: 'Content-Type is expected to be application/json;charset=UTF-8'
    }
  };
}

if (response.statusCode === 201 && body?.id && body.success === true) {
  return {
    decision: 'finish'
  };
}
else {
  return {
    decision: {  
      status: 'abort',
      message: 'Salesforce Opportunity not created'
    }
  };
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://support.attackforge.com/attackforge-enterprise/modules/flows/salesforce.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
