Updating a Record in OFBiz using Java

This is an example on how to update a record in OFBiz using Java as engine. I am assuming that the you have created the service of this method in you service.xml.

public static Map updatePerson(DispatchContext dctx, Map context) {
Map resultMap = ServiceUtil.returnSuccess();

//this is how you fetch the values from the request
String id = (String) context.get("id");
String firstName = (String) context.get("firstName");
String lastName = (String) context.get("lastName");
String gender = (String) context.get("gender");
String email = (String) context.get("email");

try {
GenericValue personGV = delegator.findOne("Person", UtilMisc.toMap("id", id), false);
if (personGV != null && !personGV.isEmpty()) {
personGV.put("firstName", firstName);
personGV.put("lastName", lastName);
personGV.put("gender", gender);
personGV.put("email", email);
personGV.store();
}
} catch (GenericEntityException e) {
return ServiceUtil.returnError("Failed. " +e.getMessage());
}
return resultMap;
}

1 comment:

  1. Could you tell me how to update a record from a Java program that is not a service?

    http://stackoverflow.com/questions/11803591/how-does-one-get-the-dispatchcontext-in-ofbiz-from-a-java-program

    ReplyDelete