Skip to Content
1
May 23, 2018 at 06:30 PM

Create Interactions in yMarketing with OData Service CUAN_IMPORT_SRV

275 Views Last edit May 24, 2018 at 11:04 AM 10 rev

I work on a SAPUI5 application (in Web IDE) which creates an interaction in Hybris Marketing. Therefore I use OData service CUAN_IMPORT_SRV. I already tried it with a simple Postman request and it worked fine. The interaction was created in Hybris Marketing.

The example JSON body for upload looks like this (oriented by Call OData Service):

{
  "Id" : "",
  "Timestamp" : "/Date(1506014140593)/",
  "UserName" : "UNAME",
  "SourceSystemId" : "ANY",
  "Interactions" : [
    {
      "Key" : "",
      "CampaignId" : "",
      "CommunicationMedium" : "BUSINESS_DOCUMENT",
      "ContactId" : "005056966DBE1EE4B299CD12B99F60C4",
      "ContactIdOrigin" : "SAP_HYBRIS_MKT_IC",
      "ContentData" : "First Event",
      "ContentTitle" : "",
      "InitiativeId" : "100001622",
      "InitiativeVersion" : 0,
      "InteractionType" : "CRM_ACTIVITY",
      "IsAnonymous" : false,
      "MarketingOrchestrationId" : 0,
      "Quantifier" : 1,
      "Timestamp" : "/Date(1506014140283)/"
    }
  ]
}

It shall not be discussed if the data makes sense or not. It's right now just a test case. In my SAPUI5 application I started writing a simple function which shall handle the data upload. I oriented on this topic.

This is my function:

onPressScan: function() {
	sap.ndc.BarcodeScanner.scan(
		
		function(mResult) {
			
			if (!mResult.cancelled) {
				// begin POST Interaction with OData
				var oModel = new sap.ui.model.odata.v2.ODataModel("https://host:port/sap/opu/odata/sap/CUAN_IMPORT_SRV/", true, "UNAME", "PASSWORD");
				oModel.setHeaders({
					"X-CSRF-Token" : "Fetch"
				});
				
						var jsonString = '{ "Id" : "", "Timestamp" : "/Date(1506014140591)/",  "UserName" : "UNAME", "SourceSystemId" : "ANY", "Interactions" : [ ' +
						'{ "Key" : "" , "CampaignId" : "" , "CommunicationMedium" : "BUSINESS_DOCUMENT" , "ContactId" : "005056966DBE1EE4B299CD12B99F60D4" ' +
						', "ContactIdOrigin" : "SAP_HYBRIS_MKT_IC" , "ContentData" : "Try from Scanner App", "ContentTitle" : "Scan", "InitiativeId" : "100001622" ' +
						', "InitiativeVersion" : 0, "InteractionType" : "CRM_ACTIVITY", "IsAnonymous" : false, "MarketingOrchestrationId" : 0, "Quantifier" : 1, "Timestamp" : "/Date(1506014140281)/" }]}';
						
				
				var json = JSON.parse(jsonString);
			
				// get X-CSRF-Token for POST request
				oModel.read("/$metadata", null, null, true, function(oData, oResponse) {


				var xcsrfToken = oResponse.headers["x-csrf-token"];


				oModel.setHeaders({
					"X-CSRF-Token" : xcsrfToken,
					"Content-Type" : "application/json"
				});


				// POST interaction to yMarketing
				oModel.create("/ImportHeaders", json, null, function() {
					alert("Create successful");
				}, function(){
					alert("Create failed");
				});
			}, function() {
				alert("Read failed");
			});
		}}, function(Error) {
			sap.m.MessageBox.error("Scanning failed due to following error: " + Error, {
				title: "Error while scanning"                                     
			});
		}
	);
}

When I try the application I get the following error:

What do I do wrong? I guess I can't fetch the X-CSRF-Token like I'm trying to do it. I could not find any topics on doing that with OData Model.

Thanks for every hint and your advice.

Attachments

error.png (31.4 kB)