Skip to content

Commit

Permalink
Merge pull request #27 from niveathika/main
Browse files Browse the repository at this point in the history
Tweak namings in examples
  • Loading branch information
niveathika authored Jul 16, 2024
2 parents 83f655e + d85f9dd commit 4ac072a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 42 deletions.
12 changes: 6 additions & 6 deletions examples/salesforce-to-sap/Salesforce To S4hana.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ necessary credentials (username, password, secret).
Configure Salesforce and S/4HANA API credentials in `Config.toml` in the example directory:

```toml
[salesforceClientConfig]
[sfListenerConfig]
username = "<Username>"
password = "<Password + Secret>"
environment = "DEVELOPER"

[sfClientConfig]
clientId = "<Client_ID>"
clientSecret = "<Client_Secret>"
refreshToken = "<Refresh_token>"
baseUrl = "<Base_url>"
refreshUrl = "<Refresh_url>"

[salesforceListenerConfig]
username = "<Username>"
password = "<Password + Secret>"
environment = "DEVELOPER"

[s4hanaClientConfig]
hostname = "<Hostname>"
username = "<Username>"
Expand Down
56 changes: 26 additions & 30 deletions examples/salesforce-to-sap/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,28 @@ import ballerina/log;
import ballerina/random;
import ballerinax/salesforce;
import ballerinax/sap.s4hana.api_sales_order_srv as salesorder;
import ballerinax/trigger.salesforce as sftrigger;
import ballerinax/trigger.salesforce as sfListener;

configurable SalesforceListenerConfig salesforceListenerConfig = ?;
configurable SalesforceClientConfig salesforceClientConfig = ?;
configurable SfListenerConfig sfListenerConfig = ?;
configurable SfClientConfig sfClientConfig = ?;
configurable S4HanaClientConfig s4hanaClientConfig = ?;

listener sftrigger:Listener sfdcEventListener = new ({
username: salesforceListenerConfig.username,
password: salesforceListenerConfig.password,
channelName: "/data/OpportunityChangeEvent",
environment: salesforceListenerConfig.environment
listener sfListener:Listener sfListener = new ({
...sfListenerConfig,
channelName: "/data/OpportunityChangeEvent"
});

final salesforce:Client sfClient = check new ({
baseUrl: salesforceClientConfig.baseUrl,
baseUrl: sfClientConfig.baseUrl,
auth: {
clientId: salesforceClientConfig.clientId,
clientSecret: salesforceClientConfig.clientSecret,
refreshToken: salesforceClientConfig.refreshToken,
refreshUrl: salesforceClientConfig.refreshUrl
clientId: sfClientConfig.clientId,
clientSecret: sfClientConfig.clientSecret,
refreshToken: sfClientConfig.refreshToken,
refreshUrl: sfClientConfig.refreshUrl
}
});

final salesorder:Client salesOrderClient = check new (
{
final salesorder:Client salesOrderClient = check new ({
auth: {
username: s4hanaClientConfig.username,
password: s4hanaClientConfig.password
Expand All @@ -51,32 +48,32 @@ final salesorder:Client salesOrderClient = check new (
s4hanaClientConfig.hostname
);

service sftrigger:RecordService on sfdcEventListener {
isolated remote function onCreate(sftrigger:EventData payload) {
service sfListener:RecordService on sfListener {
isolated remote function onCreate(sfListener:EventData payload) {
log:printInfo(string `New opportunity created: ${payload.metadata?.recordId ?: ""}`);
}

isolated remote function onUpdate(sftrigger:EventData payload) returns error? {
isolated remote function onUpdate(sfListener:EventData payload) returns error? {
string? opportunityId = payload.metadata?.recordId;
if opportunityId == null {
log:printError("Error while creating SAP order: invalid opportunityId from event");
return;
}
log:printInfo(string `Recieved an opportunity update event for id: ${opportunityId}`);

boolean isClosed = check boolean:fromString(<string>(payload.changedData["IsClosed"] ?: "false"));
if !isClosed {
string isClosed = check payload.changedData["IsClosed"].ensureType();
if isClosed != "true" {
log:printInfo("Opportunity is not closed. Skipping order creation.");
return;
}

boolean isWon = check boolean:fromString(<string>(payload.changedData["IsWon"] ?: "false"));
if !isWon {
string isWon = check payload.changedData["IsWon"].ensureType();
if isWon != "true" {
log:printInfo("Opportunity is not won. Skipping order creation.");
return;
}

SalesforceOpportunityItem[]|error retrievedItems = retrieveOpportunityItems(opportunityId);
SfOpportunityItem[]|error retrievedItems = retrieveOpportunityItems(opportunityId);
if retrievedItems is error {
log:printError("Error while retrieving opportunity items: " + retrievedItems.message());
return;
Expand All @@ -96,25 +93,24 @@ service sftrigger:RecordService on sfdcEventListener {
}
}

isolated remote function onDelete(sftrigger:EventData payload) {
isolated remote function onDelete(sfListener:EventData payload) {
log:printInfo(string `Opportunity deleted: ${payload.metadata?.recordId ?: ""}`);
}

isolated remote function onRestore(sftrigger:EventData payload) returns error? {
isolated remote function onRestore(sfListener:EventData payload) returns error? {
log:printInfo(string `Opportunity restored: ${payload.metadata?.recordId ?: ""}`);
}
}

isolated function retrieveOpportunityItems(string opportunityId) returns SalesforceOpportunityItem[]|error {
stream<SalesforceOpportunityItem, error?> sfOpportunityItems = check sfClient->query(
isolated function retrieveOpportunityItems(string opportunityId) returns SfOpportunityItem[]|error {
stream<SfOpportunityItem, error?> sfOpportunityItems = check sfClient->query(
string `SELECT ProductCode, Name, Quantity FROM OpportunityLineItem
WHERE OpportunityId='${opportunityId}'`);
return check from var item in sfOpportunityItems
return from var item in sfOpportunityItems
select {...item};
}

isolated function transformOrderData(SalesforceOpportunityItem[] salesforceItems) returns salesorder:CreateA_SalesOrder|error {

isolated function transformOrderData(SfOpportunityItem[] salesforceItems) returns salesorder:CreateA_SalesOrder|error {
int salesOrderId = check random:createIntInRange(5000000, 5999999);
salesorder:CreateA_SalesOrder salesOrder = {
SalesOrder: salesOrderId.toString(),
Expand Down
6 changes: 3 additions & 3 deletions examples/salesforce-to-sap/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
// specific language governing permissions and limitations
// under the License.

type SalesforceListenerConfig record {|
type SfListenerConfig record {|
string username;
string password;
string environment;
|};

type SalesforceClientConfig record {|
type SfClientConfig record {|
string baseUrl;
string clientId;
string clientSecret;
Expand All @@ -34,7 +34,7 @@ type S4HanaClientConfig record {|
string password;
|};

type SalesforceOpportunityItem record {
type SfOpportunityItem record {
string ProductCode;
decimal Quantity;
string Name;
Expand Down
4 changes: 1 addition & 3 deletions examples/shopify-to-sap/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import ballerinax/sap.s4hana.api_sales_order_srv as salesorder;

configurable S4HanaClientConfig s4hanaClientConfig = ?;

final salesorder:Client salesOrderClient = check new (
{
final salesorder:Client salesOrderClient = check new ({
auth: {
username: s4hanaClientConfig.username,
password: s4hanaClientConfig.password
Expand Down Expand Up @@ -53,7 +52,6 @@ service /sap\-bridge on new http:Listener(9090) {
}

isolated function transformShopifyOrder(ShopifyOrder shopifyOrder) returns salesorder:CreateA_SalesOrder|error {

int salesOrderId = check random:createIntInRange(5000000, 5999999);
salesorder:CreateA_SalesOrder salesOrder = {
SalesOrder: salesOrderId.toString(),
Expand Down

0 comments on commit 4ac072a

Please sign in to comment.