Skip to main content

Sync Products Filtered by Sales Area

You can filter your products by sales area 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.

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

LimitProductsBySalesAreaCallable.apxc
global class LimitProductsBySalesAreaCallable 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, 'SEARCHPARAMS', new Map<String, String> {
'VKORG' => '3000', // Sales Org
'VTWEG' => '01' // Distribution Channel
});
applyAll(searchContext, 'DIVISION', new List<Map<String, String>> {
new Map<String, String> { 'SPART' => '01' },
new Map<String, String> { 'SPART' => '02' }
});
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
LimitProductsBySalesAreaCallableTests.apxc
@IsTest
private class LimitProductsBySalesAreaCallableTests {
private static LimitProductsBySalesAreaCallable testInstance = new LimitProductsBySalesAreaCallable();

@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');
}
}