1

I made a script (SSJS) to create a new Import Definition in Marketing Cloud. That works well, and if I then trigger the import manually in the GUI the import successfully completes. However, I also want to start the ImportDefinition from my script, but I can't find how to do it.

This is how I (successfully) create an ImportDefinition:

var api = new Script.Util.WSProxy();
var importDef = {
    Name: "CreatedDE_" + CAMPAIGN_NAME,
    CustomerKey: NEW_GUID,
    AllowErrors: true,
    DestinationObject: { // DestinationObject?
        CustomerKey: DE_KEY
    },
    RetrieveFileTransferLocation: {
        CustomerKey: "ExactTarget Enhanced FTP"
    },
    UpdateType: "Overwrite",
    FieldMappingType: "InferFromColumnHeadings",
    FileSpec: FILE_NAME, // set CSV file name
    FileType: "CSV"
};
var res = api.createItem("ImportDefinition", importDef);

I can get the CustomerKey and the ObjectID from the res object after it is created. I tried to use that to start the ImportDefinition, like this (or CustomerKey or Name instead of ObjectID):

var res = api.performItem("ImportDefinition", { ObjectID: res.Results[0].NewObjectID },"start",{});

But it always gives an error, with very descriptive Error Messages like:
Exception occurred during [Perform] ErrorID: 1646841981

The response of the performItem looks as follows. Only the ObjectID is initialized, so would that be an indication that it did not initialize the object properly?

{"Status":"Error","StatusMessage":"","RequestID":"bd47e454-6f78-465d-b616-20122c398049","Results":[{"Object":{"Name":null,"CustomerKey":null,"AllowErrors":false,"DestinationObject":null,"RetrieveFileTransferLocation":null,"UpdateType":"AddAndUpdate","FieldMappingType":"InferFromColumnHeadings","FileSpec":null,"FileType":"CSV","FieldMaps":null,"Notification":null,"SubscriberImportType":"Email","MaxFileAge":0,"MaxFileAgeScheduleOffset":0,"MaxImportFrequency":0,"Delimiter":null,"HeaderLines":0,"AutoGenerateDestination":null,"ControlColumn":null,"ControlColumnDefaultAction":"AddAndUpdate","ControlColumnActions":null,"EndOfLineRepresentation":null,"NullRepresentation":null,"StandardQuotedStrings":false,"Filter":null,"DateFormattingLocale":null,"DeleteFile":false,"SourceObject":null,"DestinationType":0,"SubscriptionDefinitionId":null,"EncodingCodePage":0,"SmsMemberSharedShortCodeId":null,"HasMultipleFiles":false,"InteractionObjectID":null,"Description":null,"Keyword":null,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"ObjectID":"20e8daf1-e3d3-e911-a2dd-48df37015621","Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false},"Task":{"StatusCode":"Error","StatusMessage":"Exception occurred during [Perform] ErrorID: 270858153","OrdinalID":0,"ErrorCode":0,"ID":null,"TblAsyncID":0,"InteractionObjectID":"fbdb6f91-7520-45a9-b556-275de4306dd5"},"ProgramActivityInstanceId":null,"StatusCode":"Error","StatusMessage":"Exception occurred during [Perform] ErrorID: 270858153","OrdinalID":0,"ErrorCode":2,"RequestID":null,"ConversationID":null,"OverallStatusCode":null,"RequestType":"Synchronous","ResultType":null,"ResultDetailXML":null}]}

I also tried using the old fashioned API calls Platform.Function.InvokePerform() but that gives the same errors.

Anybody knows what is the correct way to start an ImportDefinition using WSProxy or another working method in SSJS?

  • Have you verified that the res.Results[0].NewObjectID is returning a value? The syntax you have is 100% correct and during my tests worked as expected. My guess is that the returned JSON you are using to get your ObjectID is in fact stringifying the nested arrays, making it return null results in your value declaration. – Gortonington Sep 10 '19 at 15:24
  • @Gortonington thanks for your comment! Yes, I did check and NewObjectID does indeed return the guid of the new object.. I also tried it with a hardcoded CustomerKey (that I knew was correct), also didn't work.. But have you been successful with performItem in this way, using ObjectID? – Jhu884 Sep 10 '19 at 15:50
  • I have run it successfully using nearly identical to what you have above. I did set all the props and action, etc. via vars but that shouldn't make a difference. – Gortonington Sep 10 '19 at 16:36
  • I added the output, but it's weird as the object is largely uninitialized (not sure if that's expected behavior). Also tried it with old-fashioned API calls, but that gives exactly the same errors. Referencing the object by Name instead of CustomerKey/ObjectID also did not yield other results. I'm puzzled! If you know of another method in SSJS (not WSProxy), also open to that. – Jhu884 Sep 11 '19 at 6:27
  • I looked up the error code you received and it looks like the issue is around authentication (which is weird considering its WSProxy so its in platform) I would check to make sure you have API permissions, etc. set for your account. – Gortonington Sep 11 '19 at 12:59
1

I looked up the error code you received and it looks like the issue is around authentication (which is weird considering its WSProxy so its in platform) I would check to make sure you have API permissions, etc. set for your account.

As an alternative as it seems like WSProxy and SOAP API seem to be giving you issue, you could try an undocumented REST endpoint: automation/v1/imports/{id}/actions/start

Basically doing something like this:

var endPoint = endPointURL + '/automation/v1/imports/' + importID + '/actions/start';
var contentType = 'application/json';
var payload = '';
var headers = ['Authorization'];
var headervalues = [authToken];
var results = HTTP.Post(endPoint, contentType, payload, headers, headervalues);

Where impotID is the ObjectID of your import Definition and authToken is the Oauth Token you gather via the auth endpoint.

|improve this answer|||||
  • 1
    Thanks, indeed Salesforce support also said it had to do with authentication, but they are still checking into it, will report back when they found the issue. Curious if you have a public source for the error code explanation? Or do you have a contact within SF tech team for that? And thanks for the alternative workaround. Must say, I never worked with the REST endpoint yet and also not sure if we have API credentials already, can check with our Administrator. Worth giving a try if SF support cannot fix the issue. – Jhu884 Sep 12 '19 at 11:31
  • 1
    Error codes for SFMC SOAP (developer.salesforce.com/docs/…) – Gortonington Sep 12 '19 at 11:54
  • 1
    Thanks, those error codes are really helpful, immediately bookmarked! Unfortunately I did not get to try your REST API solution as I do not have API credentials, and I found a workaround with only WSProxy. – Jhu884 Sep 16 '19 at 12:02
0

I found a workaround (a bit hacky), using WSProxy, by putting the ImportDefinition in an Automation. Somehow I don't have any authentication issues with starting an Automation from WSProxy! I did need to setClientID() on the WSProxy object though, to get this to work.

// SAME CODE AS ABOVE TO START WITH
var api = new Script.Util.WSProxy();
var importDef = {
    Name: "CreatedDE_" + CAMPAIGN_NAME,
    CustomerKey: NEW_GUID,
    AllowErrors: true,
    DestinationObject: { // DestinationObject?
        CustomerKey: DE_KEY
    },
    RetrieveFileTransferLocation: {
        CustomerKey: "ExactTarget Enhanced FTP"
    },
    UpdateType: "Overwrite",
    FieldMappingType: "InferFromColumnHeadings",
    FileSpec: FILE_NAME, // set CSV file name
    FileType: "CSV"
};
var res = api.createItem("ImportDefinition", importDef);

// ADDED CODE TO USE AUTOMATION WORKAROUND
api.setClientId({ "ID": YOUR_CLIENT_ID });
var importObjectID = res.Results[0].NewObjectID;
var automationKey = Platform.Function.GUID();
var automationDef = {
    Name: 'AutomatedImport_' + automationKey,
    CustomerKey: automationKey,
    Description: 'Automated Import',
    AutomationTasks: [
        {
            Name: 'Import',
            Activities: [
                {
                    ObjectID: importObjectID,
                    Name: 'Import',
                    ActivityObject: {
                        "__Type__": "ImportDefinition",
                        ObjectID: importObjectID,
                        Name: 'Import'
                    }
                }
            ]
        }
    ],
    'AutomationType': 'Scheduled'
};
res = api.createItem('Automation', automationDef);
res = api.performItem("Automation", { "CustomerKey": automationKey }, "start");
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.