cancel
Showing results for 
Search instead for 
Did you mean: 

Automated Workflow Creation AP Invoice from GRPO

attakorn_t
Explorer
0 Kudos
140

Dear Expert,

I'm trying to create AP invoice from GRPO with SAP B1 Workflow.
My code above encounters the error 'DataAccess temporarily unavailable' at the line grpoLines = company.createDIObject('DocumentLines');. I would like to ask for suggestions on how to resolve this issue. Also, if anyone has documentation or tutorials on using various coding commands in workflow, I would greatly appreciate it.

var grpo = CurrentProcess.B1Obj('GRPO1');
var grpoDocEntry = grpo.getDocEntry();

var query = "SELECT CardCode, DocDate, DocDueDate, NumAtCard from OPDN where DocNum = '" + grpoDocEntry + "'";
var param = company.getRecordsetParams();
param.setQuery(query);
var recordset = company.getRecordset();
recordset.doQuery(param);

var cardCode = "";
var docDate = "";
var dueDate = "";
var numAtCard = "";

if (recordset.read()) {
cardCode = recordset.getField(0).getColumnValue();
docDate = recordset.getField(1).getColumnValue();
docDueDate = recordset.getField(2).getColumnValue();
numAtCard = recordset.getField(3).getColumnValue();
}

if( numAtCard != null)
{
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!

var yyyy = today.getFullYear();
if(dd<10)(dd='0'+dd) if(mm<10)(mm='0'+mm) today = yyyy+"-"+mm+"-"+dd;

var invoiceService = company.getBusinessService('18');
var apInvoice = company.createDIObject('Document');
apInvoice.setCardCode(cardCode);
//apInvoice.setDocDate(docDate);
apInvoice.setDocDueDate(today);

var grpoLines = grpo.getDocumentLines();
if (grpoLines == null) {
grpoLines = company.createDIObject('DocumentLines');
}

for (var i = 0; i < grpoLines.size(); i++) {

var line = company.createDIObject('DocumentLine');
line.setItemCode(grpoLine.getItemCode());

grpoLines.add(line);
}

apInvoice.setDocumentLines(grpoLines);

invoiceService.add(apInvoice);
}

Best Regards.

 

View Entire Topic
ANKIT_CHAUHAN
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi attakorn_t,

Can you try something as below?

<script>
var purchaseOrderService = company.getBusinessService("22");
var doc = company.createDIObject("Document");
doc.setCardCode("V10000");
var today = new Date();
doc.setDocDueDate('2021-09-26');
doc.setDocDate('2021-09-26');

var docLines = doc.getDocumentLines();
if (docLines == null) {
docLines = company.createDIObject("DocumentLines");
}
var docLine = company.createDIObject("DocumentLine");
docLine.setItemCode("A00001");
docLine.setQuantity(1);
docLines.add(docLine);
doc.setDocumentLines(docLines);

var docRefs = doc.getDocumentReferences();
if (docRefs == null) {
docRefs = company.createDIObject("DocumentReferences");
}
var docRef = company.createDIObject("DocumentReference");
docRef.setRefDocEntr("1");
var refObjTypeType=Utility.getEnumValue(docRef, "RefObjTypeType", "rot_SalesOrder");
docRef.setRefObjType(refObjTypeType);
docRef.setIssueDate('2021-09-26');
docRef.setRemark("Test");
docRefs.add(docRef);
doc.setDocumentReferences(docRefs);

purchaseOrderService.add(doc);

</script>

Kind regards,

ANKIT CHAUHAN

SAP Business One Support

attakorn_t
Explorer
0 Kudos
From this code, is it an example of creating a PO? Could you please share an example of how to create an A/P Invoice?