How to create an offer using an API call in Adobe Campaign Classic? | Interaction

adobe-campaign-offers-api-creation-interaction

One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man.

Yesterday, I was reading through one of my article that was published a couple of weeks back. The article shows How to validate and approve offers programmatically?

This kept me wondering that, if I can approve an offer using an API call then there should be a way to create offers using API call as well.

After working for a couple of hours, I finalized and tested my solution successfully. Let me take you through the implementation. I hope you are excited!

Goal:

The goal is to create an offer through an API call.

We will be using JSON in this implementation. In your web service definition, make sure that the attribute “dom” is set to true.

Method Definition:

<method dom="true" library="cus:offerAPI.js" name="GetOfferContent" static="true">
  <parameters>
    <param desc="Offer information" inout="in" name="offer"
           type="DOMElement"/>
    <param desc="Result" inout="out" name="result" type="string"/>
  </parameters>
</method>

Script

The example consider:

  • Two default weight
  • Default eligibility rules (based on custom parameters)
  • Content (custom fields)

Script to create offer with API

/*
Author: Prajwal Shetty
Version: 1.6
Date:21/12/2018
Purpose: Create offer using API
*/

loadLibrary("xtk:shared/nl.js");
NL.require('/nl/core/shared/xml.js');
 
//[of]:nms_offer_UpdateOfferContent
function nms_offer_UpdateOfferContent(offer){
  var doc = initResult();
  var result = doc.root;
   
  try
  {
    //Insert the offer
    insertUpdateOffer(offer);
    //Insert weight
    insertWeight("selectionRate", 1, offer.$id); //name, ranking, offerId
    insertWeight("orderRate", 2, offer.$id); //name, ranking, offerId
  }
  catch(e)
  {
    return "Error";
  }
     
  return "Success";
}
//[cf]
 
function insertUpdateOffer(offer) {
 
    //Create the main offer object
    var offerObject = {offer: {
                          "category-id": offer['$category-id'],
                          isModel: "0",
                          label: offer.$label,
                          "owner-id": "0",
                          id: offer.$id,
                          xtkschema: "nms:offer",
                          _operation: "insertOrUpdate",
                          _key: "@id",
                          eligibilityValidation: {
                            validation: {
                              delay: "355500",
                              type: "0"
                            }
                          },
                          contentValidation: {
                            validation: {
                              delay: "355500",
                              type: "0"
                            }
                          },
                          filter: {
                            label: "preview date for interaction on or after order start date for offer being processed and preview date for interaction on or before order end date for offer being processed",
                            schema: "nms:recipient",
                            targetSchema: "nms:recipient",
                            where: {
                              displayFilter: "current date if preview date for interaction is NULL, preview date for interaction otherwise greater than or equal to offer start date and current date if preview date for interaction is NULL, preview date for interaction otherwise less than or equal to offer end date",
                              filterName: "backGroundFilterFrm",
                              filteringSchema: "nms:recipient",
                              id: "1774387208",
                              condition: [
                                { expr: "Coalesce([interaction/@previewDate] ,  GetDate()) > [currentOffer/@offerStartDate]",
                                  internalId: "2722758687"
                                },
                                { boolOperator: "AND",
                                  expr: "Coalesce([interaction/@previewDate] ,  GetDate()) < [currentOffer/@offerEndDate]",
                                  internalId: "2726035489"
                                },
                                { boolOperator: "AND",
                                  expr: "'-' + Coalesce([currentOffer/@split] ,[interaction/@split]) + '-' LIKE '%' + Iif([interaction/@split] = '', '', '-' + [interaction/@split] + '-') + '%'",
                                  internalId: "3141992514"
                                }
                              ]
                            },
                            humanCond: {
                              $: "Query: current date if preview date for interaction is NULL, preview date for interaction otherwise greater than or equal to offer start date and current date if preview date for interaction is NULL, preview date for interaction otherwise less than or equal to offer end date"
                            }
                          },
                          forecast: {
                            simuResponseType: "0"
                          },
                          view: {
                            unitaryUpdateStatusUrl_jst: {
                              $: "<%@ include view='UpdateStatusUrl' %>"
                            }
                          }
                      }};
 
    //Create the dom document from the offer object
    var doc = DOMDocument.fromJXON(offerObject);
 
    //Add the content
    for each(var content in offer.getFirstElement("contents").getElementsByTagName("content")) {
       
      var type = content.getFirstElement("type").textContent;
      var source = content.getFirstElement("source").textContent;
      var view = doc.root.getFirstElement("view");
 
      switch(type) {
        case "web1html":
          view.appendChild( doc.createElement("web1html_jst") );
          view.getFirstElement("web1html_jst").textContent = source;
          break;
        case "web2html":
          view.appendChild( doc.createElement("web2html_jst") );
          view.getFirstElement("web2html_jst").textContent = source;
          break;
        case "smsHtml":
          view.appendChild( doc.createElement("smsHtml_jst") );
          view.getFirstElement("smsHtml_jst").textContent = source;
          break;
      }
    }
     
    //Insert the object
    NLWS.xtkSession.Write(doc.root);
}
 
//insertWeight to add weight to the offer object
function insertWeight(name, ranking, offerId) {
 
  NLWS.xtkSession.Write({offerContext: {
                        label: name,
                        "offer-id": offerId,
                        weightExpr: "1",
                        xtkschema: "nms:offerContext",
                        _operation: "insertOrUpdate",
                        _key: "offer/@id,@label",
                        filter: {
                          label: "ranking for interaction equal to '" + ranking + "'",
                          schema: "nms:recipient",
                          where: {
                            condition: {
                              boolOperator: "OR",
                              expr: "[interaction/@ranking] = '" + ranking + "'",
                              internalId: "1518665750"
                            }
                          },
                          humanCond: {
                            $: "Query: ranking for interaction equal to '" + ranking + "'"
                          }
                        },
                        offer: {
                          id: offerId,
                          status: "0"
                        }
                     }});
}

This is how you create an offer using an API call. We can do much more using this technique. Keep exploring!

Hope this helps

error: Content is protected !!