'ModelService does not remove the model in groovy script
I'm trying to remove a model from database using ModelService in my groovy script but it doesn't remove.
import de.hybris.platform.core.model.order.OrderModel
import de.hybris.platform.ordersplitting.model.ConsignmentModel
List<String> orderList = new ArrayList<>()
orderList.add("P000015003")
OrderModel orderModel = flexibleSearchService.search(String.format("SELECT {PK} FROM {AbstractOrder} WHERE {code} = '%s'", "P000015003")).result.get(0)
ConsignmentModel consignment = orderModel.getConsignments().iterator().next()
ModelService modelService = (ModelService) spring.getBean("modelService");
modelService.remove(consignment)
println("Removed")
Solution 1:[1]
Solution 2:[2]
You could use transaction to perform commit operation:
import de.hybris.platform.core.model.order.OrderModel
import de.hybris.platform.ordersplitting.model.ConsignmentModel
def tx = de.hybris.platform.tx.Transaction.current()
tx.begin()
List<String> orderList = new ArrayList<>()
orderList.add("P000015003")
OrderModel orderModel = flexibleSearchService.search(String.format("SELECT {PK} FROM {AbstractOrder} WHERE {code} = '%s'", "P000015003")).result.get(0)
ConsignmentModel consignment = orderModel.getConsignments().iterator().next()
ModelService modelService = (ModelService) spring.getBean("modelService");
modelService.remove(consignment)
println("Removed")
tx.commit()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Vikrant |
Solution 2 | Devendra |