Liquid UI - Documentation - 19.30 Custom function to replace text

19.30 Custom function to replace text


Prerequisites

Purpose

In this article, you will learn how to employ a user-defined function to search for and replace strings within a text variable. The search terms must be devoid of spaces between them and separated by commas. Additionally, the search is case-sensitive.

Follow the steps below.

  1. Delete all the elements on the Create Material (Initial Screen)
  2. Add an input field to search the string
  3. Add a textbox to display the string
  4. Add a toolbar push button to replace the text
  5. Add an isblank function
  6. Add a function to replace text
  7. Add a function to search string


User Interface

//Create this file inside your script folder for customizing the Create Material (Initial Screen), SAPLMGMM.E0060.sjs

//Now, let's add the Liquid UI script to the above file and save it.


Customization

  1. Delete all elements on the Create Material (Initial Screen) using clearscreen(), as shown below:
     
    // Deletes all the screen elements 
    clearscreen();
    
     
     
  2. Add a Search input field to enter the string.
     
    // Creates an input field labeled Search and z_search as its technical name 
     inputfield([2,1],"Search",[2,10],{"name":"z_search","size":20});
    
     
     
  3. Add a textbox to enter the text and display the replaced text.
     
    //Creates a textbox with z_textbox as its technical name 
    textbox([3,1],[10,50],{"name":"z_textbox"});
    
     
     
  4. Add a toolbar push button called Replace to execute a process called replaceText on click.
     
     //Creates a toolbar push button labeled Replace
    pushbutton([TOOLBAR],"Replace","/nmm01",{"process":replaceText});
    
     
     
  5. Add an isblank function to check for a blank string.
     
    // Function to check for blank string
    // Specifically used to determine if input exist in the edit field
    function isBlank(strInput) {
       var strVal = getString(strInput);
       var blank = strVal == "";
       return blank;
    }
    
     
  6. Add a custom function called replaceText that takes a string or a regular expression as input, searches for specified values, and returns a new string with those values replaced.
     
    function replaceText(){
    set("V[z_var]" , "&V[z_search]");
    copytext({"fromtext":"z_textbox","tostring":"z_string"});
    replace(z_var,z_string);
    copytext({"fromstring":"z_replace","totext":"z_textbox"});
    }
    
     
  7. Add a function to search for a string in the keyword.
     
    // Function to search for a string
     function search(string,keyword){
          if (string.indexOf(keyword) != -1){
          // if keyWord is found,
          var z_slice_position = string.indexOf(keyword) + keyword.length + 1;
          var new_string = string.slice(z_slice_position);
          return(new_string.split(" ",1));
          }
          } 
     
  8. Incorporate a function to change a file name utilizing ActiveXObject
     
    // Function to rename a file using ActiveXObject in SAP
         function replace(src,str) {
    	var myArray = []; i =0;z_sresult = "";
    	myArray = src.split(',');
    	for (i=0; i<myArray.length;i++){
    		println("--Arry value----" + myArray[i]);
    		if(isBlank(myArray[i])) {
    			message("Please enter find and replace values!");
    			goto end;
    		}
    	}
    	if(search(str,myArray[0]))
    		z_replace = str.replace(myArray[0],myArray[1]);
    	while(isBlank(z_sresult)) {
    		if(search(z_replace,myArray[0]))
    			z_replace = z_replace.replace(myArray[0],myArray[1]);
    		else
    			z_sresult = "X";		
    	}	
    	end:
    	return(z_replace);
    } 
     
     

SAP Process

  1. Refresh the SAP screen. First, enter the text in the textbox. Then, enter the word to search and replace (DIEN, FERT) within the textbox in the Search input field, as shown below.
     
     
  2. Now, click the Replace toolbar push button, and you will find DIEN replaced with FERT in the text entered in the textbox.
     

     

Can't find the answers you're looking for?