[Salesforce] Using the with sharing or without sharing Keywords

Use the  with sharing or  without sharing keywords on a class to specify whether or not to enforce sharing rules.

The with sharing keyword allows you to specify that the sharing rules for the current user be taken into account for a class. You have to explicitly set this keyword for the class because Apex code runs in system context. In system context, Apexcode has access to all objects and fields— object permissions, field-level security, sharing rules aren’t applied for the current user. This is to ensure that code won’t fail to run because of hidden fields or objects for a user. The only exceptions to this rule are Apex code that is executed with the executeAnonymous call and Chatter in ApexexecuteAnonymous always executes using the full permissions of the current user. For more information on executeAnonymous, seeAnonymous Blocks.

Use the with sharing keywords when declaring a class to enforce the sharing rules that apply to the current user. For example:

public with sharing class sharingClass {

// Code here

}

Use the without sharing keywords when declaring a class to ensure that the sharing rules for the current user are not enforced. For example:

public without sharing class noSharing {

// Code here

}
Some things to note about sharing keywords:
  • The sharing setting of the class where the method is defined is applied, not of the class where the method is called. For example, if a method is defined in a class declared with with sharing is called by a class declared with without sharing, the method will execute with sharing rules enforced.
  • If a class isn’t declared as either with or without sharing, the current sharing rules remain in effect. This means that if the class is called by a class that has sharing enforced, then sharing is enforced for the called class.
  • Both inner classes and outer classes can be declared as with sharing. The sharing setting applies to all code contained in the class, including initialization code, constructors, and methods.
  • Inner classes do not inherit the sharing setting from their container class.
  • Classes inherit this setting from a parent class when one class extends or implements another.
### 回答1: Salesforce Lightning 是一种用于构建功能丰富的销售、客户服务和市场营销应用程序的开发平台。它使用 JavaScript 作为客户端脚本语言,可以通过 REST API 与服务器端进行通信。 下面是一个使用 Python 通过 REST API 访问 Salesforce Lightning 的示例: ```python import requests # Replace <your_username> and <your_password> with your Salesforce login credentials auth = ('<your_username>', '<your_password>') # Replace <instance_url> with the URL of your Salesforce instance instance_url = '<instance_url>' # Set the API version to use api_version = 'v48.0' # Set the URL for the REST API resource you want to access resource_url = f'{instance_url}/services/data/{api_version}/sobjects/Account/describe' # Send the GET request and store the response response = requests.get(resource_url, auth=auth) # Print the response status code print(response.status_code) # Print the response body print(response.json()) ``` 在这个示例中,我们使用 Python 的 `requests` 库向 Salesforce 发送了一个 GET 请求,访问了 Salesforce 的 `Account` 对象的描述信息。如果请求成功,将会返回状态代码 200 和 `Account` 对象的描述信息。 注意,要使用这个示例,你需要在 Salesforce 中创建一个账户,并将你的登录凭据和 Salesforce 实例 URL 替换到示例中。此外,你还需要在你的 Salesforce 实例中启用 REST API。 希望这个示例能帮助你了解如何使用 Python 通过 REST API 与 Salesforce Lightning 通信。如果你有任何其他问题,请随时给我留言。 ### 回答2: Salesforce Lightning是Salesforce的一种用户界面框架,用于创建现代化、交互式的Salesforce应用程序。REST API是一种基于HTTP协议的Web服务,用于在不同的应用程序之间进行通信。 要使用Python与Salesforce Lightning结合使用REST API,可以按照以下步骤进行: 1. 首先,导入必要的Python库,如requests和json。安装这些库可以使用pip命令。 2. 获取Salesforce实例的访问令牌(access token)。可以使用OAuth 2.0进行身份验证,并使用自己的凭据来获取令牌。 3. 在Python中,我们可以使用requests库发送HTTP请求来与Salesforce REST API进行通信。可以使用GET、POST、PUT和DELETE等HTTP方法来执行不同的操作。 4. 可以使用URL来指定要访问的Salesforce对象和操作。例如,要获取所有账户记录,可以使用以下URL: https://<Salesforce实例URL>/services/data/vXX.X/query?q=SELECT Id, Name FROM Account 5. 使用requests库发送对应的HTTP请求,将访问令牌作为请求头部(headers)的一部分添加到请求中。 6. 解析从Salesforce REST API返回的响应,可以使用json库将响应中的JSON数据转换为Python对象。 7. 根据需求,可以进行进一步的操作,如创建、更新、删除记录。 以下是一个使用Python与Salesforce Lightning结合使用REST API的示例代码: ```python import requests import json # 获取访问令牌 access_token = "<YOUR_ACCESS_TOKEN>" # 要获取的Salesforce对象和操作的URL url = "https://<Salesforce_instance_url>/services/data/vXX.X/query?q=SELECT Id, Name FROM Account" # 添加访问令牌到请求头部 headers = {"Authorization": "Bearer " + access_token} # 发送GET请求 response = requests.get(url, headers=headers) # 解析JSON响应 data = json.loads(response.text) # 输出结果 for record in data["records"]: print(record["Id"], record["Name"]) ``` 以上就是一个简单的使用Python与Salesforce Lightning结合使用REST API的示例。通过类似的方式,可以执行其他操作,如创建、更新和删除记录等。 ### 回答3: Salesforce Lightning是Salesforce的一种应用界面模式,可以帮助用户更加便捷地操作Salesforce平台上的数据和功能。而REST API是一种用于与网络上的资源进行交互的技术,可以通过HTTP协议发送请求并获取响应数据。 如果要在Python中使用Salesforce Lightning的REST API进行开发,以下是一个示例: 首先,我们需要使用Python的请求库来发送HTTP请求。你可以使用Python的`requests`库,首先需要安装它,然后在你的代码中导入它。 ```python import requests ``` 然后,我们需要构建Salesforce REST API的请求URL,并添加所需的请求头信息。 ```python base_url = "https://your_salesforce_instance.salesforce.com" api_version = "vXX.X" endpoint = "/services/data/{api_version}/" token = "your_oauth_token" url = base_url + endpoint headers = {"Authorization": "Bearer " + token, "Content-Type": "application/json"} ``` 接下来,我们可以使用`requests`库发送各种类型的HTTP请求,例如GET、POST、PATCH、DELETE等。下面是一个使用Salesforce REST API获取所有账户的示例: ```python def get_all_accounts(): resource = "sobjects/Account" query_url = url + resource response = requests.get(query_url, headers=headers) return response.json() ``` 或者,我们可以创建新的账户,如下所示: ```python def create_account(account_data): resource = "sobjects/Account" create_url = url + resource response = requests.post(create_url, headers=headers, json=account_data) return response.json() ``` 这只是一个简单的示例,你可以根据实际需求进一步开发。希望这个示例能够帮助你理解如何使用Python与Salesforce Lightning的REST API进行交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值