When searching for accounts we f.i. can't search for their Sales Area Data. But let's say we only want to display accounts that have a certain Sales Office. This guide explains how to add a field from one component (Sales) into a search of another (Account Search). Of course the data has to be related -> check the GENIL Model Browser.
Step by Step
1. Get technical data of the Account Search
Log in to the Web UI and navigate to the Account Management. Under Search, click on Accounts. Now click in any input field and press F2 to get the technical data. So we write down:
Context Node: SEARCH
Role Key (Found): ZSALES
2. Get technical data of the Sales Office
Log in to the Web UI and navigate to the Account Management. Under Search, click on Accounts. Search for any account and look at it. Find the Sales Area Data and click on Edit respectively New. Locate Sales Office under Organization and click into the picklist. While still clicking, press F2 to get the technical data. So we write down:
Context Node: BUILRESORG
3. Get the dictionary structure of the Account Search
We can find structures through the Genil Model Browser - Transaction GENIL_MODEL_BROWSER. Choose Component Set ALL and press F8. Open the node Search Objects and locate BuilHeaderSearch. Open Attribute Structure and write down...
Account Head Search Structure: CRMT_BUPA_IL_HEADER_SEARCH
4. Get the dictionary data element of the Sales Office
Again open the Genil Model Browser with Component Set ALL. We know that account and sales data are related. So we search through the Relations.
Open Root Objects and locate BuilHeader. Open node Relations and search for the sales relation. You'll find one called BuilSalesArrangementRel.
Open it and again open the Relations node. There you'll find an object called BuilResponsibleOrganisationRel. Open this relation and its object. Now open Attribute Structure and the structure below it: there is our Sales Office. Open it to get the dictionary data element and write it down...
Sales Office Data Element: CRMT_SALES_OFFICE
5. Extend the Account Search structure
Open structure CRMT_BUPA_IL_HEADER_SEARCH and click on the "Append Structure" button. Create a new structure in your customer Namespace i.e. ZCRMT_BUPA_IL_HEADER_SEARCH and add the Sales Office as ZZSALES_OFFICE with the data element we found in Step 4.
6. Add the Sales Office to the Account Search page
Now that we have the field Sales Office in the account search structure can we add it to the View MainSearch as search parameter.
Open the Component Workbench - transaction BSP_WD_CMPWB - and display BP_HEAD_SEARCH. Double click on View MainSearch and go the Configuration tab. Select the configuration of found Role Key in step 1 (or copy configuration if there is none existing yet). Locate in Available Search Criteria the newly added Sales Office, move it to the right side and display it.
Can we search yet? No!
We added the field to the search structure but that's it. The search logic is missing. At the moment the search would try to find the Sales Office directly in the account header data - and that obviously won't work.
7. Enhance the Business Partner Search with BAdI implementation
Because we added a new field which doesn't belong to the account header data, must we program our own search logic. This is done using BAdI Implementation. In transaction se18 we got to find the needed BAdI and its Enhancement Spot:
Enhancement Spot: CRM_BUPA_IL_SEARCH
Open the above mentioned Enhancement Spot and go to tab Enh. Spot Element Definitions. Open the search extension BAdI, right click on Implementation and choose Create Implementation. You will get a window showing all existing implementations. Click on New (empty paper icon). Follow the instructions to create the implementation.
Open the newly created Enhancement Implementation. In tab Enh. Implementation Elements expand your BAdI and double click on Implementing Class. If a class has not yet been created, create one.
Now we reached the programming part. Open your Implementing Class and the method IF_EX_CRM_BUPA_IL_SEARCH_EXT~SEARCH_CRITERIA_INITIAL. This method should check if there are relevant search criteria for this implementation. In our case we check if the Sales Office has been filled:
Now open the actual search method IF_EX_CRM_BUPA_IL_SEARCH_EXT~SEARCH_PARTNERS. This method either gets the business partner that were found so far or nothing (parameter CT_PARTNER_KEYS). In the second case, this method has to search for all partners and return the ones that have the searched Sales Office. The code I'm going to post is just an example. There are maybe better ways to do it.
Getting the account search parameters:
Search for partners if none received:
Filter partner that have the searched Sales Office:
Raise exception if no partners were found:
NOW we can search for the Sales Office. But... only for its object key. Wouldn't a picklist be much better? Of course it would. Let's do it.
8. Create an Enhancement Set (optional)
If we want to add a picklist for our Sales Office we have to enhance the account main search and add the picklist through programming. To do that we need an Enhancement Set. Such a set is created using transaction sm34 with View Cluster BSPWDVC_CMP_EXT.
Secondly the Enhancement Set needs to be assigned to a client. This is done in transaction sm30 with View BSPWDV_EHSET_ASG.
9. Enhance Account Search View (optional)
Display BP_HEAD_SEARCH in the Component Workbench with your just created Enhancement Set. Right click on View MainSearch and choose Enhance. Create all objects needed.
10. Add a picklist to the Sales Office search parameter (optional)
Go to the implementation class of the enhanced MainSearch View (probably called ZL_BP_HEAD_MAINSEARCH_IMPL). Redefine the method GET_DQUERY_DEFINITIONS. Make sure that you call the original code from class CL_BP_HEAD_MAINSEARCH_IMPL by using the super->get_dquery_definitions call. In addition to the Sales Office we also provide a dropdown list for the Sales Group. This is how I've done it:
Calling super method:
Adding Sales Office and Sales Group:
Note: the method call of get_val_for_sales_office was found when looking at component BP_SALES and its view (as found in step 2). There the attribute STRUCT.SALES_OFFICE has a GET_V method which stands for get value help.