REST Attachments

Good morning everyone.

Please can someone explain to me the process of uploading attached to a table using rest? I want to attach a file to the table of UD100 based on Key1

I will pull out the data using UD100 and set a variable being Key1 and use that to submit the attachment back into epicor.

I don’t know where to start or what objects to use.

If someone that understands uploading attachments can explain to me what methods/objects need to talk to each other in order to preform this task.

Kind regards,
Aaron.

There is an attachment BO you can use for the upload that returns usually a file reference

Then you can use the UD100Attach endpoint to set the prior reference as an attachment to a related ud100 record

I will get an example when I get in

Thank you Jose

This is an example using DocStar but the BO’s are the same just use UploadFile instead of DocStarUploadFile

//Read file from Disk into a byte array
Byte[] bytes = File.ReadAllBytes(x);
                        String fileData = Convert.ToBase64String(bytes);
//Create the File Object to be uploaded to Epicor 
                        var file = new
                        {
                            fileName = System.IO.Path.GetFileName(x),
                            data = fileData,
                            docTypeID = "PGOrderD",
                            parentTable = "UD103",
                            metadata = new
                            {
                                _Author = "Epicor PG Importer",
                                _TableName = "Ice.UD03",
                                _TableSysRowID = sysRowID
                            }
                        };
//Upload the file using the Attachment BO
var addedDocStarFile = EpicorRest.DynamicPost("Ice.BO.AttachmentSvc", "DocStarUploadFile", file);

//Create the Epicor XFileRef
var xFileRef = new { Company = company, XFileName = addedDocStarFile.ds.ToString(), XFileDesc = file.fileName, DocTypeID = "PGOrderD" };
var addedXRef = EpicorRest.DynamicPost("Ice.BO.XFileRefSvc", "XFileRefs", xFileRef);

// Once the XFileRef exists attach it to a UD103 record.
var UD103Attch = new
                        {
                            Company=companyID,
                            Key1=key1,
                            Key2 = key2,
                            Key3 = key3,
                            Key4 = key4,
                            Key5 = key5,
                            addedXRef.XFileRefNum,
                            FileName = addedXRef.XFileName,
                            DrawDesc = $"{key1}-{key2}-{key3}-{key4}",
                            DocTypeID = file.docTypeID,
                            ForeignSysRowID = sysRowID,
                            RowMod = "A"
                        };
dynamic newUDAttch = EpicorRest.DynamicPost("Ice.BO.UD103Svc", "UD103Attchs", UD103Attch);
5 Likes

Is it possible to use REST to print attachments with a button in Kinetic, without DocStar? I.e. In the Kinetic Customer Shipment Entry window, add a “Print Attachments” button where the event workflow prints all line items’ Instruction doc type attachments to a specific printer… Has anyone done something similar?

Sure just invoke a server side function and ahve the function do the priting work. You’ll have to figure out the Printer settings and such either hard coded or per user or “server side” printer setup.

1 Like

Hi Jose,

Take a look at this PHP code. I cannot seem to get it work correctly.

Maybe I’m missing something?

 <?PHP
    $status = "";
    require_once("api.php");
?>
<!-- POST to this page, with a multipart form data 

encoding -->
<form method="post" enctype="multipart/form-data">
    <input type="file" name="fileUpload" />
    <input type="submit" name="formSubmit" 

value="Upload to ERP" />
</form>
<br>
<hr>
<?PHP
if(isset($_POST) && $_FILES != null && isset($_FILES) 

&& isset($_FILES["fileUpload"])) {
    // php tmp upload location, get the binary content
    $fileCnt = file_get_contents($_FILES

["fileUpload"]["tmp_name"]);
 
    $parentTable = "UD100";
    $docTypeID = "LPE";
    $key1 = "";
    $key2 = "";
    $key3 = "";
    $key4 = "";
    $key5 = "";
    $sysRowID = "";
    $author = "Epicor LPE Importer";
    $data = (object)[];
    $data->docTypeID = $docTypeID;
    $data->parentTable = $parentTable;
    // image001.jpg
	$data->fileName = $_FILES["fileUpload"]

["name"];
    // convert to b64
    $data->data = base64_encode($fileCnt);
    $data->metadata = (object)[];
    $data->metadata->_Author = "Epicor LPE Importer";
    // AARON - May need to change "Ice."?
    $data->metadata->_TableName = "Ice." . 

$parentTable;
    $data->metadata->_TableSysRowID = $sysRowID;
    $addedDocStarFile = json_decode(set_data

("Ice.BO.AttachmentSvc/UploadFile", $data, $status));
    echo "<p>Ice.BO.AttachmentSvc/UploadFile</p>";
    echo "<pre>" . json_encode($addedDocStarFile) . 

"</pre>";
    $data = (object)[];
    $data->Company = "TEAGLE";
    $data->XFileName = $addedDocStarFile->ds;
    $data->XFileDesc = $_FILES["fileUpload"]["name"];
    $data->DocTypeID = $docTypeID;
    $addedXRef = json_decode(set_data

("Ice.BO.XFileRefSvc/XFileRefs", $data, $status));    
    echo "<p>Ice.BO.XFileRefSvc/XFileRefs</p>";
    echo "<pre>" . json_encode($addedXRef) . "</pre>";
    $data = (object)[];
    $data->Company = "TEAGLE";
    $data->Key1 = $key1;
    $data->Key2 = $key2;
    $data->Key3 = $key3;
    $data->Key4 = $key4;
    $data->Key5 = $key5;
    $data->XFileRefNum = $addedXRef->XFileRefNum;
    $data->FileName = $addedXRef->XFileName;
    $data->DrawDesc = "$​key1​-$​key2​-$​key3​-$​key4​";
    $data->DocTypeID = $docTypeID;
    $data->ForeignSysRowID = $sysRowID;
    $data->RowMod = "A";
    $newUDAttch = json_decode(set_data

("Ice.BO.UD100Svc/UD103Attchs", $data, $status));
    echo "<p>Ice.BO.UD100Svc/UD100Attchs</p>";
    echo "<pre>" . json_encode($newUDAttch) . 

"</pre>";
    /// /\ set_data($entity, $postData, out 

$httpStatus)
}
?>

All I’m getting back is this.

ce.BO.AttachmentSvc/UploadFile

{"HttpStatus":403,"ReasonPhrase":"REST API Exception","ErrorMessage":"Access denied (Ice.BO.Attachment).\r\n\r\nContact your System Administrator to ensure you have access.","ErrorType":"System.UnauthorizedAccessException"}

Notice: Undefined property: stdClass::$ds in C:\xampp\htdocs\LoadPlanning\index.php on line 43
Ice.BO.XFileRefSvc/XFileRefs

{"HttpStatus":403,"ReasonPhrase":"REST API Exception","ErrorMessage":"Access denied (Ice.BO.XFileRef).\r\n\r\nContact your System Administrator to ensure you have access.","ErrorType":"System.UnauthorizedAccessException"}

Notice: Undefined property: stdClass::$XFileRefNum in C:\xampp\htdocs\LoadPlanning\index.php on line 57

Notice: Undefined property: stdClass::$XFileName in C:\xampp\htdocs\LoadPlanning\index.php on line 58
Ice.BO.UD100Svc/UD100Attchs

{"HttpStatus":404,"ReasonPhrase":"REST API Exception","ErrorMessage":"OData path is undefined for the request https:\/\/epicor.teagle.local\/erp10test\/api\/v1\/Ice.BO.UD100Svc\/UD103Attchs?api-key=kM6cKTGiooHTAUkYYCfkPOUbfb8hdj3dWzvJSdRUXbftt","ErrorType":"Epicor.RESTApi.ErrorHandling.ApiException"

Access Denied means your API Key + Scope aren’t setup correctly, or your username and password are wrong.

That fixed one problem :smiley:

Ice.BO.UD100Svc/UD100Attchs

{"odata.error":{"code":"","message":{"lang":"en-US","value":"The request is invalid."},"innererror":{"message":"entity : Cannot convert a primitive value to the expected type 'Edm.Guid'. See the inner exception for more details.\r\n","type":"","stacktrace":""}}}

Any idea on this error?

It’s expecting a GUID for “something” and you are passing not a guid. I’m guessing your foreignSysRow id is wrong?

Can you print your request body?

Boom!

You sir are the man!