Skip to main content

Sync Products Filtered by Material Types

You can filter your products by material type when using EnosixMatSync.

info

Use the following link for help on creating Apex classes in Salesforce: Adding an Apex Class.

Use information from the valence.LinkContext if you wish to use the same Apex class with more than one Link and wish to vary the behavior of your filter based on which Link is currently running.

Example of SAP material type codes:

  • Finished products = FERT
  • Configurable materials = KMAT

Here is the code you will need for your Apex class to filter your link by material type:

LimitProductsByMaterialTypeCallable.apxc
global class LimitProductsByMaterialTypeCallable implements Callable {
global Object call(String action, Map<String, Object> args) {
if (action == ensxsync.EnosixAdapter.UPDATE_PARAMS_ACTION) {
Map<String, Object> searchContext = (Map<String, Object>) args.get(ensxsync.EnosixAdapter.SEARCH_CONTEXT_KEY);
Valence.LinkContext linkContext = (Valence.LinkContext) args.get(ensxsync.EnosixAdapter.LINK_CONTEXT_KEY);
applyAll(searchContext, 'MATERIAL_TYPE', new List<Map<String, String>> {
new Map<String, String> { 'MTART' => 'FERT' },
new Map<String, String> { 'MTART' => 'KMAT' }
});
return searchContext;
}
return null;
}
@TestVisible
private void applyAll(Map<String, Object> searchContext, String key, Object values) {
if (searchContext.containsKey(key)) {
if (searchContext.get(key) instanceof Map<String, String>) {
((Map<String,String>)searchContext.get(key)).putAll((Map<String, String>)values);
}
if (searchContext.get(key) instanceof List<Map<String, String>>) {
((List<Map<String, String>>)searchContext.get(key)).addAll((List<Map<String, String>>)values);
}
} else {
searchContext.put(key, values);
}
}
}
Apex Unit Test Code
LimitProductsByMaterialTypeCallableTests.apxc
@IsTest
private class LimitProductsByMaterialTypeCallableTests {
private static LimitProductsByMaterialTypeCallable testInstance = new LimitProductsByMaterialTypeCallable();

@IsTest static void testCall() {
System.assertEquals(null, testInstance.call(null, null), 'Expect null return and no Exceptions with null inputs');
String originalTestValue = '42';
Object actual = testInstance.call(
ensxsync.EnosixAdapter.UPDATE_PARAMS_ACTION,
new Map<String, Object>{
ensxsync.EnosixAdapter.SEARCH_CONTEXT_KEY => new Map<String, Object>{
'SEARCHPARAMS' => new Map<String, String> { 'TEST_VALUE' => originalTestValue }
}
}
);
System.assertNotEquals(null, actual, 'Expect search context returned');
String actualTestValue = ((Map<String, String>)((Map<String, Object>)actual).get('SEARCHPARAMS')).get('TEST_VALUE');
System.assertEquals(originalTestValue, actualTestValue, 'Expect SEARCHPARAMS=>TEST_VALUE to be untouched');
}

@IsTest static void testCoverageForApplyAll() {
testInstance.applyAll(
new Map<String, Object>{},
'KEY',
null
);
testInstance.applyAll(
new Map<String, Object>{
'KEY' => new Map<String, String>{}
},
'KEY',
new Map<String, String>{}
);
testInstance.applyAll(
new Map<String, Object>{
'KEY' => new List<Map<String, String>>{}
},
'KEY',
new List<Map<String, String>>{}
);
System.assert(true, 'Expect completion without Exceptions');
}
}