SSJS: Day 21 focused on Resource
https://www.youtube.com/@b2shashi

SSJS: Day 21 focused on Resource

Write SSJS Code with Try-Catch-Throw and Handle Exception


<script runat="server" language="javascript">
  
  Platform.Load("core", "1");
  
  //===================================================================//
  // Retrieves up to 2500 rows of data in a data extension
  //===================================================================//
  function DataExtensionRowsRetrieve(dataExtensionProps)
  {
    try{
    var dataExtension = DataExtension.Init(dataExtensionProps.Init);
    var dataSet = dataExtension.Rows.Retrieve(dataExtensionProps.filter);
    return dataSet;
    }
    catch(ex)
    {
      var APIExceptionDE = DataExtension.Init("APIException");
      APIExceptionDE.Rows.Add(
                                {
                                   Message:ex.message
                                  ,Description:ex.description
                                  ,InnerException:ex.jintException
                                  ,FunctionName:"DataExtensionRowsRetrieve"
                                });
       throw ex;
    }
  }
  
  
  //=========================================================================//
  // The SimpleOperators object contains operators to use when filtering data.
  //=========================================================================//
  function SimpleOperators(simpleOperatorsProps)
  {
    try{
    var filter = { 
                    Property:simpleOperatorsProps.Property
                   ,SimpleOperator:simpleOperatorsProps.SimpleOperator
                   ,Value:simpleOperatorsProps.Value
                 };
    return filter;
    }
     catch(ex)
    {
      var APIExceptionDE = DataExtension.Init("APIException");
      APIExceptionDE.Rows.Add(
                                {
                                   Message:ex.message
                                  ,Description:ex.description
                                  ,InnerException:ex.jintException
                                  ,FunctionName:"SimpleOperators"
                                });
       throw ex;
    }
  }
  
  
  
  //=========================================================================//
  // Request token for a Marketing Cloud environment
  //=========================================================================//
  function AccessToken(dataExtensionPropsAPIConfig,dataExtensionPropsAPIMethod)
  {
    try{
    // Call DataExtensionRowsRetrieve function to retrieve for API Config
    var APIConfiDE=DataExtensionRowsRetrieve(dataExtensionPropsAPIConfig);
    
    // Call DataExtensionRowsRetrieve function to retrieve for APIMethod Config
    var APIMethodDE=DataExtensionRowsRetrieve(dataExtensionPropsAPIMethod);
    
    //Assign properties values for v2/Token
    var memberID = Platform.Recipient.GetAttributeValue('memberid');
    var clientId = APIConfiDE[0]["ClientID"];
    var clientSecret= APIConfiDE[0]["ClientSecret"];
    var grantType="client_credentials";
    var authAPI=APIConfiDE[0]["AuthAPI"];
    var contentType = "application/json";
    var headers= {
                    "client_id":clientId
                   ,"client_secret":clientSecret
                   ,"grant_type":grantType
                   ,"account_id": memberID
                 };
    var httpProtocol = APIMethodDE[0]["HttpProtocol"];
    var methodURL = APIMethodDE[0]["MethodURL"];
    
    var accessTokenResult = HTTP.Post(authAPI+methodURL, ContentType, Stringify(headers));
    return accessTokenResult;
    }
     catch(ex)
    {
      var APIExceptionDE = DataExtension.Init("APIException");
      APIExceptionDE.Rows.Add(
                                {
                                   Message:ex.message
                                  ,Description:ex.description
                                  ,InnerException:ex.jintException
                                  ,FunctionName:"AccessToken"
                                });
       throw ex;
    }
  }
  


  
  try{
    
    // Define the variable for SimpleOperator with properties for API Config
    var simpleOperatorsPropsAPIConfig = {
                                  "Property":"APIConfig"
                                 ,"SimpleOperator":"Equals"
                                 ,"Value" :"TransactionalAPI"
                               }
    
   
    
    // Define the variable for dataExtension retrieve with properties for API Config
    var dataExtensionPropsAPIConfig={
                              "Init":"InstallPackages"
                             ,"filter":SimpleOperators(simpleOperatorsPropsAPIConfig)
                           }
    
    
    // Define the variable for SimpleOperator with properties for APIMethod Config
    var simpleOperatorsPropsAPIMethodToken = {
                                  "Property":"MethodName"
                                 ,"SimpleOperator":"Equals"
                                 ,"Value" :"Request SFMC Token"
                               }
    
    // Define the variable for dataExtension retrieve with properties for APIMethod Config
    var dataExtensionPropsAPIMethodToken={
                              "Init":"APIMethod"
                             ,"filter":SimpleOperators(simpleOperatorsPropsAPIMethodToken)
                           }
    
    // Call AccessToken function to retrieve for Access Token
    var accessTokenResult=AccessToken(dataExtensionPropsAPIConfig,dataExtensionPropsAPIMethodToken);
    var statusCode = accessTokenResult["StatusCode"];
    var response = accessTokenResult["Response"][0];
    var accessToken = Platform.Function.ParseJSON(response).access_token;
    var tokenType = Platform.Function.ParseJSON(response).token_type;
    Write(tokenType + ' ' + accessToken);
  
    
  }
  catch(ex)
  {
    var APIExceptionDE = DataExtension.Init("APIException");
      APIExceptionDE.Rows.Add(
                                {
                                   Message:ex.message
                                  ,Description:ex.description
                                  ,InnerException:ex.jintException
                                  ,FunctionName:"Main Block"
                                });
    Write(ex.message + '\n');
    Write(ex.description + '\n');
    Write(ex.jintException + '\n');
  }
</script>
Arijeet Bannerjee

7x Salesforce Certified Professional | Project Lead at Persistent Systems | Ex-Cognizant & Capgemini | Former LAMP Stack Developer

2y

Great content with try, catch and throw examples. Only thing that could be covered here is the use of "finally". Looking forward for posts using finally along with try, catch, throw

Elian Maufras

MarTech Consultant at LineUp7

2y

I think it's GPT Sashi :p

Praveen Kumar

7x Certified SFMC Architect | Automation Studio | Journey Builder | MCI | Advertising Studio | Distribution Marketing | AMP Script | SSJS | REST/SOAP API | Salesforce Admin | SQL | CDP | GA360

2y

I like your videos very much Sasee Prasad

To view or add a comment, sign in

Others also viewed

Explore topics