Skip to main content

Hybris Beanshell and Groovy script example


While doing support, we need to check some of the scenario and their result in the production server. The beanshell and groovy script comes very handy on this. Below are the few example of how I used/using it in my various project. This is very handy to test your data as well
1.       Check the log: Sometimes getting server access is very painful in the enterprise landscape. However, person joined in the project is required to work as soon as they inducted in the project. In this scenario, below groovy script is very useful.  Please note, run these in the rollback mode unless and until the otherwise is required.
a.       The application is running from the container. For the POC, it was running in the tomcat server (tomcat/bin). To know the exact directory, run the below script in groovy console
println "pwd".execute().text
Below will be printed in the output tab

b.      println "tail -n 50 ../../../../log/tomcat/console-20180206.log".execute().text

c.       Below script will fetch the order using flexiblesearch(beanshell)
import java.util.Map;
import java.util.HashMap;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.core.model.order.OrderModel;
import java.util.Collection;
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;
import de.hybris.platform.servicelayer.search.SearchResult;
import de.hybris.platform.servicelayer.search.SearchResult;

ModelService modelService = spring.getBean("modelService");
FlexibleSearchService flexibleSearchService = spring.getBean("flexibleSearchService");
String orderNumber = "0005574014";
Map attr = new HashMap(1);
attr.put("code", orderNumber );
StringBuilder sql = new StringBuilder();
sql.append("SELECT {o:pk} from { ").append("Order").append(" as o} WHERE {o:code} = ?code ")
  .append("AND {o:versionID} IS NULL");
FlexibleSearchQuery query = new FlexibleSearchQuery(sql.toString());
query.getQueryParameters().putAll(attr);
SearchResult result = flexibleSearchService.search(query);
List orders = result.getResult();
out.println(orders.get(0).getCode());

d.       Bulk update of object (POC is on ticket) where assigned group changed from one to another (beanshell). In this case, run the script in commit mode

import de.hybris.platform.core.model.ItemModel;
import de.hybris.platform.core.model.security.PrincipalGroupModel;
import de.hybris.platform.core.model.user.UserModel;
import de.hybris.platform.cscockpit.services.search.generic.query.DefaultTicketSearchQueryBuilder;
import de.hybris.platform.cscockpit.services.search.impl.DefaultCsTextSearchCommand;
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.servicelayer.user.UserService;
import de.hybris.platform.ticket.enums.CsTicketState;
import de.hybris.platform.ticket.model.CsAgentGroupModel;
import de.hybris.platform.ticket.service.TicketBusinessService;
import de.hybris.platform.ticket.model.CsTicketModel;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;
import de.hybris.platform.servicelayer.search.SearchResult;


ModelService modelService = spring.getBean("modelService");
UserService userService = spring.getBean("userService");
FlexibleSearchService flexibleSearchService = spring.getBean("flexibleSearchService");
TicketBusinessService ticketBusinessService = spring.getBean("ticketBusinessService");
String fromGroup = "groupFrom";
String toGrouo = "toGroup";

String query = "Select {t:pk} from {CsTicket AS t }, {UserGroup AS G} WHERE {t:assignedGroup}={G:pk} AND {G:uid} = '" + fromGroup +"'";

FlexibleSearchQuery searchQuery = new FlexibleSearchQuery(query);
SearchResult searchResult = flexibleSearchService.search(searchQuery);
List ticketList = searchResult.getResult();
int counter=0;
for(CsTicketModel ticket: ticketList)
{
              counter++;
              out.println("Before Update....." + ticket.getTicketID());
ticket.setAssignedGroup(userService.getUserGroupForUID(toGrouo));
              modelService.save(ticket);
 
}


out.println(counter);

Comments

  1. Hybris Beanshell And Groovy Script Example >>>>> Download Now

    >>>>> Download Full

    Hybris Beanshell And Groovy Script Example >>>>> Download LINK

    >>>>> Download Now

    Hybris Beanshell And Groovy Script Example >>>>> Download Full

    >>>>> Download LINK mE

    ReplyDelete

Post a Comment

Popular posts from this blog

SonarCube and Hybris integration

There are some issues reported for SonarCube 6.7 integration with Hybris. The recommended version is Sonar 6.4 or less. For Sonar 6.4, there is no need to build the database. Please follow the below steps to use sonarcube with Hybris. For the POC, we have used Hybris 6.4.0.2 and sonar 6.4. Sonar Cube configuration 1.        Download sonar 6.4 and install/unzip. 2.        For the POC, we have used Embedded Database (default). But multiple database is supported. Access management can also be implemented. However, for the simplicity purpose, it is not used for the POC 3.        The java properties can be set at the sonar.properties file (not used in our case). Example of the property is sonar.web.javaOpts=-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError which can be enabled in the sonar.properties file under conf folder of the installed folder. 4.      ...

SAP Commerce License

There are two types of license for SAP commerce application. Temporary license (valid for 90 days). Permanent license Will discuss how to install both the type of license one by one. Before that, let’s discuss few details about the SAP Commerce License. ·        Unlike other SAP products, SAP commerce license is not used for any specific H/W. So, the field HARDWARE-KEY in the license file is filled with some preset values. ·        Usually customer will choose three letter system ID (SID – SAPSYSTEM) while creating the SAP license. This SID must be configured in the local.properties # System ID license.sap.sapsystem=XYZ If this is omitted, system will think that CPS as the default value. Installing the temporary license ·        Run license.bat from the platform folder license.bat -temp CPS_SQL In the above, license type is temporary (-temp) for the dev environm...