Skip to content

Add CSV to XML and CSV to JSON conversion functionalities #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public with sharing class AccountNumberOfEmployeesComparator implements Comparat
} else {
compareResult = 0;
}
if (a1NumberOfEmployees == a2NumberOfEmployees){
compareResult = a1.Name.compareTo(a2.Name);
}
}
return compareResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public with sharing class AccountShippingCountryComparator implements Comparator
compareResult = -1;
} else if (a2 == null) {
compareResult = 1;
else if (a1.ShippingCountry == null && a2.ShippingCountry == null) {
compareResult = 0;
} else {
String a1ShippingCountry = a1?.ShippingCountry;
String a2ShippingCountry = a2?.ShippingCountry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public with sharing class CollectionUtils {
if (current.get(key) != null) {
returnValues.put((String) current.get(key), current);
}

}
return returnValues;
}
Expand Down
15 changes: 15 additions & 0 deletions force-app/main/default/classes/Data Recipes/DMLRecipes.cls
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,21 @@ public with sharing class DMLRecipes {
return accts;
}


public static<Account> deleteActiviaKeywordInSystemMode(
List<Account> accts
) {
try {
delete as system accts;
System.debug(LoggingLevel.INFO, DMLe.getMessage());
throw new CustomDMLException('Failed');

}
}




/**
* @description Undeletes a list of accounts via the `undelete` DML keyword
* @param accts List of accounts to undelete in user mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public with sharing class DynamicSOQLRecipes {
return Database.query(queryString, AccessLevel.USER_MODE);
}


@SuppressWarnings('PMD.ApexSOQLInjection')
public static List<Account>






/**
* @description Demonstrates the use of a bound variable in a dynamic SOQL
* query. DANGER Because this method accepts user input (name param), it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,82 @@
* @description Demonstrates how to use DataWeave
* in Apex to convert CSV to JSON.
* @group DataWeaveInApex Recipes
*
*
*
*
*
*
*
*
*/

/**
* @description Demonstrates how to use DataWeave
* in Apex to convert CSV to JSON.
* @group DataWeaveInApex Recipes
*
*
*
*
*
*
*/




public with sharing class CsvToJsonConversionRecipes {
/**
* @description Converts CSV to JSON.
* Uses the `dw/csvToJsonBasic.dwl` DataWeave script.
* @param csvPayload CSV data to convert to JSON
* @return JSON string
*/
public static String convertCsvToJson(String csvPayload) {
DataWeave.Script script = new DataWeaveScriptResource.csvToJsonBasic();
DataWeave.Result result = script.execute(
new Map<String, Object>{ 'payload' => csvPayload }
);
return result.getValueAsString();
}

/**
* @description Converts CSV to JSON while renaming object fields.
* Uses the `dw/csvToJsonWithFieldRenaming.dwl` DataWeave script.
* @param csvPayload CSV data to convert to JSON
* @return JSON string
*/
public static String convertCsvToJsonWithFieldRenaming(String csvPayload) {
DataWeave.Script script = new DataWeaveScriptResource.csvToJsonWithFieldRenaming();
DataWeave.Result result = script.execute(
new Map<String, Object>{ 'payload' => csvPayload }
);
return result.getValueAsString();
}

/**
* @description Converts CSV to JSON with a custom `;` CSV separator.
* Uses the `dw/csvSeparatorToJson.dwl` DataWeave script.
* Notice that the script input MIME type specifies `separator=';'`.
* @param csvPayload CSV data to convert to JSON
* @return JSON string
*/
public static String convertCsvWithCustomSeparatorToJson(
String csvPayload
) {
DataWeave.Script script = new DataWeaveScriptResource.csvSeparatorToJson();
DataWeave.Result result = script.execute(
new Map<String, Object>{ 'payload' => csvPayload }
);
return result.getValueAsString();
}
}





public with sharing class CsvToJsonConversionRecipes {
/**
* @description Converts CSV to JSON.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
* @group DataWeaveInApex Recipes
*/
public with sharing class DataWeaveErrorRecipes {
public with sharing class CsvConvertRecipe {
/**
* @description Converts CSV to XML
* @param csvPayload CSV data to convert to XML
* @return XML string
*/
public static String convertCsvToXml(String csvPayload) {
DataWeave.Script script = new DataWeaveScriptResource.csvToXMLBasic();
DataWeave.Result result = script.execute(
new Map<String, Object>{'payload' => csvPayload}
);
return result.getValueAsString();
}
}



/**
* @description Runs a DataWeave script that triggers a runtime error on pupose.
* Uses the `dw/error.dwl` DataWeave script.
Expand Down