cancel
Showing results for 
Search instead for 
Did you mean: 

Auto Aupprove Vendor on creation with webservice

0 Kudos
198

Hello,

I am trying to auto-approve Supplier on the creation of a supplier with web service.

After saving vendor I called method ApproveVendor when I am approving my vendor in script

The script is executing, the supplier is created, but not being approved. Please help me to get in right direction.

Please find my script below.

import java.math.BigInteger; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.springframework.util.StopWatch; import com.sap.odp.api.usermgmt.vendormgmt.VendorRegistrationIBeanHomeIfc; import com.sap.odp.api.usermgmt.vendormgmt.VendorRegistrationIBeanIfc; import com.sap.odp.api.common.exception.ApplicationException; import com.sap.odp.api.common.exception.ChainedException; import com.sap.odp.api.common.exception.DatabaseException; import com.sap.odp.api.common.log.LogMessageIfc; import com.sap.odp.api.common.log.Logger; import com.sap.odp.api.common.types.LocalizedObjectReferenceIfc; import com.sap.odp.api.common.types.ObjectReferenceIfc; import com.sap.odp.api.common.types.TypeFactory; import com.sap.odp.api.ibean.ExtensionFieldIfc; import com.sap.odp.api.ibean.IBeanHomeIfc; import com.sap.odp.api.ibean.IBeanHomeLocator; import com.sap.odp.api.ibean.IBeanIfc; import com.sap.odp.api.ibean.SubordinateCollectionIfc; import com.sap.odp.api.integration.config.OperationConfigIfc; import com.sap.odp.api.integration.model.core.adapter.AmountAdapter; import com.sap.odp.api.integration.model.core.adapter.AttachmentAdapter; import com.sap.odp.api.integration.model.core.adapter.DualPriceAdapter; import com.sap.odp.api.integration.model.core.adapter.EnumTypeAdapter; import com.sap.odp.api.integration.model.core.adapter.IsoObjectReferenceAdapter; import com.sap.odp.api.integration.model.core.adapter.ObjectReferenceAdapter; import com.sap.odp.api.integration.model.core.adapter.PriceAdapter; import com.sap.odp.api.integration.model.core.adapter.SysDateAdapter; import com.sap.odp.api.integration.model.core.adapter.SysDatetimeAdapter; import com.sap.odp.api.integration.model.core.ObjectReference; import com.sap.odp.common.db.ObjectReference; import com.sap.odp.common.db.LocalizedObjectReference; import com.sap.odp.common.platform.SessionContextIfc; import com.sap.odp.common.types.BigText; import com.sap.odp.common.types.SysDatetime; import com.sap.odp.common.types.SysDate; import com.sap.odp.integration.mapping.HttpMethodEnum; import com.sap.odp.util.StringUtil; import com.sap.eso.api.doccommon.integration.SapSystemEnumType; import com.sap.odp.api.ibean.HighLevelBizDocIBeanIfc; import com.sap.odp.api.doccommon.doc.StatusIndicatorEnumType; import com.sap.odp.api.doccommon.doc.SchedItemEventTypeEnumType; import com.sap.eso.api.projects.ProjectSchedItemIBeanIfc; import com.sap.odp.api.doccommon.doc.SchedItemStartTypeEnumType; import com.sap.sourcing.supplierpocservice.SupplierPost; import com.sap.sourcing.supplierpocservice.ObjectFactory; import com.sap.sourcing.supplierpocservice.Response; /** * Common services class. This class would be replicated in other * Project scripts if there were any, because cross-script references * are not supported. */ public class VendorRegServiceCommon { // This is generated by the XSD stubs private ObjectFactory fac = new ObjectFactory(); // This is the name of an important field that we must find in the metadata. private final String FIELD_UDN = "UNIQUE_DOC_NAME"; // XML marshal/unmarshal adapters private AmountAdapter amountAdapter = null; private ObjectReferenceAdapter objRefAdapter = null; private SysDateAdapter sysdateAdapter = null; private SysDatetimeAdapter sysdateTimeAdapter = null; private DualPriceAdapter dualPriceAdapter = null; private PriceAdapter priceAdapter = null; private IsoObjectReferenceAdapter isoObjRefAdapter = null; // store a hash of enum adapters, per enum type private Map enumAdapters = new HashMap(); // store a hash of attachment adapters, per ibean instance private Map attachmentAdapters = new HashMap(); // We cache homes in a map so we can use them again later. // No point in creating and destroying them. private Map homes = new HashMap(); private LogMessageIfc log; VendorRegServiceCommon(String parentClassName) { // we will fluff up this log object with the rest of the details, right // before actually logging. log = Logger.createLogMessage(session) .setClass(parentClassName + ".VendorRegServiceCommon"); } public AmountAdapter getAmountAdapter() { if (amountAdapter == null) amountAdapter = new AmountAdapter(session); return amountAdapter; } public ObjectReferenceAdapter getObjRefAdapter() { if (objRefAdapter == null) objRefAdapter = new ObjectReferenceAdapter(session); return objRefAdapter; } public SysDateAdapter getSysDateAdapter() { if (sysdateAdapter == null) sysdateAdapter = new SysDateAdapter(session); return sysdateAdapter; } public SysDatetimeAdapter getSysDatetimeAdapter() { if (sysdateTimeAdapter == null) sysdateTimeAdapter = new SysDatetimeAdapter(session); return sysdateTimeAdapter; } public PriceAdapter getPriceAdapter() { if (priceAdapter == null) priceAdapter = new PriceAdapter(session); return priceAdapter; } public DualPriceAdapter getDualPriceAdapter() { if (dualPriceAdapter == null) dualPriceAdapter = new DualPriceAdapter(); return dualPriceAdapter; } public IsoObjectReferenceAdapter getIsoObjRefAdapter() { if (isoObjRefAdapter == null) isoObjRefAdapter = new IsoObjectReferenceAdapter(); return isoObjRefAdapter; } // AttachmentAdapters need an IBeanIfc to construct. Therefore, we have to store them in a Map // in case we need adapters for multiple beans in one flow public AttachmentAdapter getAttachmentAdapter(IBeanIfc bean) { ObjectReferenceIfc ref = bean.getObjectReference(); if (attachmentAdapters.containsKey(ref)) return (AttachmentAdapter) attachmentAdapters.get(ref); AttachmentAdapter attachmentAdapter = new AttachmentAdapter(session, TypeFactory.createParentObjectReference(bean)); attachmentAdapters.put(ref, attachmentAdapter); return attachmentAdapter; } // EnumAdapters need a Class to construct. Therefore, we have to store them in a Map public EnumTypeAdapter getEnumAdapter(Class classy) { if (enumAdapters.containsKey(classy)) return (EnumTypeAdapter) enumAdapters.get(classy); EnumTypeAdapter ad = new EnumTypeAdapter(session, classy); enumAdapters.put(classy, ad); return ad; } /** * Find and return the VendorRegistrationIBeanIfc for the given UNIQUE_DOC_NAME * * @param uniqueName * @return * @throws ApplicationException * @throws DatabaseException */ public VendorRegistrationIBeanIfc findByUniqueDocName(String uniqueName) throws ApplicationException, DatabaseException { VendorRegistrationIBeanHomeIfc home = (VendorRegistrationIBeanHomeIfc) getHome(VendorRegistrationIBeanHomeIfc.sHOME_NAME); log.setMethod("findByUniqueDocName"); log.setLogMessage("finding Project: " + uniqueName); Logger.debug(log); return (VendorRegistrationIBeanIfc) home.secureFindByUniqueDocName(uniqueName); } /** * Find, set to edit mode and return the VendorRegistrationIBeanIfc for the given * UNIQUE_DOC_NAME * * @param uniqueName * @return * @throws ApplicationException * @throws DatabaseException */ public VendorRegistrationIBeanIfc findForEditByUniqueDocName(String uniqueName) throws ApplicationException, DatabaseException { VendorRegistrationIBeanHomeIfc home = (VendorRegistrationIBeanHomeIfc) getHome(VendorRegistrationIBeanHomeIfc.sHOME_NAME); log.setMethod("findForEditByUniqueDocName"); log.setLogMessage("finding Project: " + uniqueName); Logger.debug(log); return (VendorRegistrationIBeanIfc) home.secureFindForEditByUniqueDocName(uniqueName); } /** * Return the Home class identified by the home name provided. * * @param homeName * @return * @throws ApplicationException */ public IBeanHomeIfc getHome(String homeName) throws ApplicationException { if (homes.containsKey(homeName)) return (IBeanHomeIfc) homes.get(homeName); IBeanHomeIfc home = IBeanHomeLocator.lookup(session, homeName); homes.put(homeName, home); return home; } /** * Create a simple Response object whose ID is the unique document name * of the provided Project. * * @param vendor * @return * @throws ApplicationException * @throws DatabaseException */ public Response createResponse(VendorRegistrationIBeanIfc vendor) throws ApplicationException, DatabaseException { // getFieldMetadata is how we deal with fields that are not in IAPI String id = (String) vendor.getFieldMetadata(FIELD_UDN).get(vendor); return createResponse(id); } /** * Create a simple Response object whose ID is provided. * * @param id * @return */ public Response createResponse(String id) { Response resp = fac.createResponse(); resp.setId(id); log.setMethod("createResponse"); log.setLogMessage("created response for id: " + id); Logger.debug(log); return resp; } /** * Save MA and return a Response. * * @param vendor * @return * @throws ApplicationException * @throws DatabaseException */ public Response saveAndRespond(VendorRegistrationIBeanIfc vendor) throws ApplicationException, DatabaseException { getHome(VendorRegistrationIBeanHomeIfc.sHOME_NAME).save(vendor); log.setMethod("saveAndRespond"); log.setLogMessage("saved vendor"); Logger.debug(log); return createResponse(vendor); } /** * Validate that the given map of params contains the given list of keys. * * @param params * @param requireds * @throws ApplicationException */ public void validateParams(Map params, String[] requireds) throws ApplicationException { StringBuilder errors = new StringBuilder(); for (String required: requireds) { if (!params.containsKey(required)) errors.append(required).append(". "); } if (errors.length() > 0) { ApplicationException ae = new ApplicationException(session, "exception.supplierpocservice.rest_url_params_missing"); ae.setMessageModifiers(new Object[] { errors.toString() }); throw ae; } } /** * Return the value of a given extension field for given attribute. * * @param bean * @param attr * @return * @throws ApplicationException * @throws DatabaseException */ public Object getExtensionField(IBeanIfc bean, String attr) throws ApplicationException, DatabaseException { ExtensionFieldIfc ef = bean.getExtensionField(attr); if (ef != null) return ef.get(); return null; } // Utility method to set the value of a given extension field (by attribute) to the given object // if the extension is not found, it's ignored. public void setExtensionField(IBeanIfc bean, String attr, Object value) throws ApplicationException, DatabaseException { ExtensionFieldIfc ef = bean.getExtensionField(attr); if (ef != null) ef.set(value); else { log.setMethod("setExtensionField"); log.setLogMessage("requested extension field (" + attr + ") not found"); Logger.warning(log); } } } /** * Class implements the actions underlying the service for manipulating a Project header. */ private class VendorRegistrationService { // This corresponds to the "id" REST URL parameter as declared in the operation configuration // Refers to the vendor unique name. private final String PARAM_ID = "id"; // ObjectFactory is a helper class included in the code generated from the XSD private ObjectFactory fac = new ObjectFactory(); // include common utilities (make sure VendorRegServiceCommon inner class is in the script def // above this class) private VendorRegServiceCommon common = new VendorRegServiceCommon("VendorRegistrationService"); // for some timing reporting private StopWatch sw = new StopWatch("VendorRegistrationService"); // This corresponds to the code generated by the XSD private final String PARAM_HEADER_POST = "{http://sourcing.sap.com/supplierpocservice}SupplierPost"; // Standard parameter containing URL variables private final String PARAM_VARMAP = "REST_URL_VARIABLE_MAP"; // Standard parameter containing the OperationConfigIfc used to execute the service private final String PARAM_OP = "REST_OPERATION_CONFIG"; // used for logging private LogMessageIfc log = Logger.createLogMessage(session).setClass("VendorRegistrationService"); // base constructor public VendorRegistrationService() {} /* * This is the main entry point for the inner class. A simple call to this go() method is all * you need in the body of the script. See the "driver()" method below for how to use. * * This pattern assumes that there are four operations declared to support the "CRUD" style * * C = "create" = "POST" * R = "retrieve" = "GET" * U = "update" = "PUT" * D = "delete" = "DELETE" * * go() will inspect the HTTP verb used during the execution of the script and call the correct * implementation for that action. This allows the use of a single script definition to support all 4 operations. * */ public Object go() throws Exception { sw.start("go"); // get the map of URL variables Map params = (Map) parameters.get(PARAM_VARMAP); // get the operation config OperationConfigIfc op = (OperationConfigIfc) parameters.get(PARAM_OP); Object ret = null; // direct to appropriate behavior based on operation config switch (op.getHttpMethodIfc().getValue()) { case HttpMethodEnum.POST: { SupplierPost inboundData = (SupplierPost) parameters.get(PARAM_HEADER_POST); if (inboundData == null) { ApplicationException ae = new ApplicationException(session, "exception.supplierpocservice.no_payload"); throw ae; } ret = doPost(inboundData, params); break; } } sw.stop(); log.setMethod("go"); log.setLogMessage("Stopwatch: " + sw.toString()); Logger.debug(log); return ret; } private String[] getRequiredParams() { return new String[] { PARAM_ID }; } private Response doPost(SupplierPost data, Map params) throws Exception { log.setMethod("doPost"); log.setLogMessage("starting post"); Logger.debug(log); VendorRegistrationIBeanIfc vendor = null; Response response = null; try { //vendor = createVendor((String)params.get(PARAM_ID)); vendor = populate(data); if (vendor == null) { ApplicationException ae = new ApplicationException(session, "exception.supplierpocservice.no_payload"); throw ae; } response = common.saveAndRespond(vendor); } finally { common.getHome(VendorRegistrationIBeanHomeIfc.sHOME_NAME).downgradeToViewAndRelease(vendor); } return response; } private void approveVendor(VendorRegistrationIBeanIfc vendor) { log.setMethod("approveVendor"); log.setLogMessage("starting Approve"); Logger.debug(log); Logger.debug(log.setLogMessage("Received vendor" + vendor)); try{ vendor.getIBeanHomeIfc().upgradeToEdit(vendor); vendor.getIBeanHomeIfc().changePhase(vendor, "Modified"); vendor.getIBeanHomeIfc().changePhase(vendor, "Approved"); vendor.getIBeanHomeIfc().save(vendor); } catch(Exception e) { Logger.debug(log.setLogMessage("Approve exception "+ e)); } log.setLogMessage("Ending Approve"); Logger.debug(log); } /* * POST / PUT stuff * CREATE / UPDATE */ private VendorRegistrationIBeanIfc populate(SupplierPost data) throws Exception { log.setMethod("populate"); VendorRegistrationIBeanIfc vendor = common.getHome(VendorRegistrationIBeanHomeIfc.sHOME_NAME).create(); vendor.setDisplayName(data.getDisplayName()); vendor.setExternalId(data.getExternalId()); vendor.setCurrencyObjRef(common.getIsoObjRefAdapter().unmarshal(data.getCurrency(), "masterdata.Currency")); vendor.setContactFirstName(data.getContactFirstName()); vendor.setContactLastName(data.getContactLastName()); vendor.setContactEmail(data.getContactEmail()); log.setMethod("populate"); log.setLogMessage("Populated vendor"); Logger.debug(log); return vendor; } } VendorRegistrationService vendorService = new VendorRegistrationService(); vendorService.go();

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member13619
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

First of all my question arose why do you have a vendor approval if you automatically want to approve a vendor?

Second remark: if there is a workflow behind it which forces you to approve then you are taking the wrong approach to try to move the phase to "Approve", this will not approve the workitem behind it.

If there is a workflow behind it, the approval is in the workflow engine so you have to approve the workitem and then it will get approved. First you have to get the workitem and then you approve the workitem (e.g. approveWorkItem(ObjectReferenceIfc, String)).

Hope this helps.

Erik

0 Kudos

Hi Erik,

I did check the logs. The script is being executed with out any issue.

Following is piece of script I am using to AutoApprove supplier, but it is showing no change in supplier approval and no errors.

venIn = IBeanHomeLocator.lookup(session, VendorRegistrationIBeanHomeIfc.sHOME_NAME);

vendor=(VendorRegistrationIBeanIfc)venIn.findByUniqueDocName((String)doc.getFieldMetadata("UNIQUE_DOC_NAME").get(doc)); venIn.upgradeToEdit(vendor);

venIn.changePhase(vendor, "Approved");

venIn.save(vendor); venIn.downgradeToViewAndRelease(vendor);

former_member13619
Product and Topic Expert
Product and Topic Expert
0 Kudos

Did you check your logs after execution as mostly you will find there an accompanied error message which directs you to the issue?