0

I created a ampscript to embedded into cloud page to update the object in Salesforce. Here is my Cloudpage landingpage:

<!doctype html>
<script language="javascript" runat="server">
Platform.Load("Core", "1.1.5");
var CONTROLLER = "https://consent.e-news.content.com/information-controller";
</script>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
  <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400&display=swap" rel="stylesheet">
  <!-- UAT Link --><link href="https://consent.e-news.content.com/information-css" rel="stylesheet">
  <link href="https://www.content.com/Themes/CONNew/Portal/favicon.ico" rel="shortcut icon" />
  <title>content Staff information</title>
</head>
<body>
  <nav class="site-header">
    <div class="container">
      <div class="header-logos row">
        <div class="logo-left col-12 col-sm-6 col-md-6 col-lg-6">
          <img src="https://image.e-news.content.com/lib/fe2f11717164047f7d1078/m/1/7ea18532-6044-4d0a-960e-ddf2cef706b1.png" alt="content" class="logo-vna" />
        </div>
        <div class="logo-right col-12 col-sm-6 col-md-6 col-lg-6">
         
        </div>
      </div>
    </div>
  </nav>
  <section class="banner jumbotron">
    <div class="banner-header container">
      <h1>STAFF INFORMATION</h1>
    </div>
  </section>

  <div class="container" id="Success">
    <div class="outer container">
      <div class="outer-block">
        <p>
          Update Successfully.
        </p>
      </div>
    </div>
  </div>

  <div class="container" id="Error">
    <div class="outer container">
      <div class="outer-block">
        <p>
          Update Failed.
        </p>
      </div>
    </div>
  </div>

  <div class="container" id="Load">
    <p>
      Welcome to Staff Information Page.
    </p>
    <div class="outer container">  
  
   <!--
  %%[  
  VAR @ContactID 
  SET @ContactID = RequestParameter("ContactID")
  VAR @ContactRows , @ContactRow
  SET @ContactRows = RetrieveSalesforceObjects(
  "Contact",
  "Id, AccountId",
  "Id", "=", @ContactID)
  
  
  
  VAR @AccountID, @AccountName, @AccountEmail , @AccountPhone , @AccountGender 
  VAR @AccountRows, @AccountRow
  
  
  IF RowCount(@ContactRows) >= 1 THEN
   SET @ContactRow = Row(@ContactRows,1)
   SET @AccountID = FIELD(@ContactRow,"AccountId")
  ENDIF
  
  

  IF @AccountID == ""  OR Empty(@AccountID) THEN
   Redirect("https://www.content.com/vn/en/home")
  ENDIF
 
  SET @AccountRows = RetrieveSalesforceObjects(
  "Account",
  "Id, Name, PersonEmail, Phone, Gender__c",
  "Id", "=", @AccountID)
  
  IF RowCount(@AccountRows) >= 1 THEN 
   SET @AccountRow            = Row(@AccountRows,1)   
   SET @AccountName           = FIELD(@AccountRow, "Name")
   SET @AccountEmail          = FIELD(@AccountRow, "PersonEmail")
   SET @AccountPhone          = FIELD(@AccountRow, "Phone")
   SET @AccountGender         = FIELD(@AccountRow, "Gender__c")
  
  ENDIF
  
   VAR @StaffId, @StaffName, @StaffPhone, @StaffGender, @StaffEmail, @StaffDepartment, @StaffCompany, @StaffTitle, @StaffBirthDate
   VAR @StaffRows , @StaffRow 
   VAR @StaffRecordType
   SET @StaffRecordType = "0127F000000KIinQAG"
   SET @StaffRows = RetrieveSalesforceObjects(
   "Relationship__c",
   "Id , Name, Email_VNA__c, Phone__c, Staff_Gender__c, Account__c, Staff_Level_5__c, Staff_Level_6__c, Staff_Title__c, Birthday__c",
   "Account__c", "=" , @AccountID, "RecordTypeId", "=" , @StaffRecordType
   )
   
   IF RowCount(@StaffRows) >= 1 THEN
   SET @StaffRow             = Row(@StaffRows, 1)
   SET @StaffId              = FIELD(@StaffRow, "Id")
   SET @StaffName            = FIELD(@StaffRow, "Name")
   SET @StaffPhone           = FIELD(@StaffRow, "Phone__c")
   SET @StaffGender          = FIELD(@StaffRow, "Staff_Gender__c")
   SET @StaffEmail           = FIELD(@StaffRow, "Email_VNA__c")
   SET @StaffDepartment      = FIELD(@StaffRow, "Staff_Level_6__c")
   SET @StaffCompany         = FIELD(@StaffRow, "Staff_Level_5__c")
   SET @StaffTitle           = FIELD(@StaffRow, "Staff_Title__c")
   SET @StaffBirthDate       = FIELD(@StaffRow, "Birthday__c")
   
   ENDIF 
  
  ]%%
    
    -->
 <div class="outer-block">
        <h4>
          Staff Information
        </h4>
      <input type="hidden" id="StaffId" name="StaffId" value="%%=v(@StaffId)=%%" />
       <div class="gold-pattern row">
          <div class="inner col-6">
            <h6>
              Name
            </h6>            
          </div>
          <div class="inner col-6">
            <input type="text" class="form-control" id="StaffName" name="StaffName" placeholder="%%=v(@StaffName)=%%" value="%%=v(@StaffName)=%%">
          </div>
        </div>
        
  <div class="gold-pattern row">
          <div class="inner col-6">
            <h6>
              Company
            </h6>            
          </div>
          <div class="inner col-6">
            <input type="text" class="form-control" id="StaffCompany" name="StaffCompany" placeholder="%%=v(@StaffCompany)=%%" value="%%=v(@StaffCompany)=%%">
          </div>
        </div>
        
   <div class="gold-pattern row">
          <div class="inner col-6">
            <h6>
              Department
            </h6>            
          </div>
          <div class="inner col-6">
            <input type="text" class="form-control" id="StaffDepartment" name="StaffDepartment" placeholder="%%=v(@StaffDepartment)=%%" value="%%=v(@StaffDepartment)=%%">
          </div>
        </div>
        
      <div class="gold-pattern row">
          <div class="inner col-6">
            <h6>
              Title
            </h6>            
          </div>
          <div class="inner col-6">
            <input type="text" class="form-control" id="StaffTitle" name="StaffTitle" placeholder="%%=v(@StaffTitle)=%%" value="%%=v(@StaffTitle)=%%">
          </div>
        </div> 
        
        <div class="gold-pattern row">
          <div class="inner col-6">
            <h6>
              Gender
            </h6>            
          </div>
          <div class="inner col-6">            
              <input type="radio" name="StaffGender" value="M" %%[IF @StaffGender == "M" THEN]%%checked%%[ENDIF]%%>Male  
              <input type="radio" name="StaffGender" value="F" %%[IF @StaffGender == "F" THEN]%%checked%%[ENDIF]%%>Female              
            </div>
           
          </div>        
        
        <div class="gold-pattern row">
          <div class="inner col-6">
            <h6>
              Birth date
            </h6>            
          </div>
          <div class="inner col-6">
            <input type="date" class="form-control" id="StaffBirthDate" name="StaffBirthDate" placeholder="%%=v(@StaffBirthDate)=%%" value="%%=v(@StaffBirthDate)=%%">
          </div>
        </div>  
  
   
  
  <div class="gold-pattern row">
          <div class="inner col-6">
            <h6>
              Phone
            </h6>            
          </div>
          <div class="inner col-6">
            <input type="text" class="form-control" id="StaffPhone" name="StaffPhone" placeholder="%%=v(@StaffPhone)=%%" value="%%=v(@StaffPhone)=%%">
          </div>
        </div>
        
   <div class="gold-pattern row">
          <div class="inner col-6">
            <h6>
              VNA Email Address
            </h6>            
          </div>
          <div class="inner col-6">
            <input type="email" class="form-control" id="StaffEmail" name="StaffEmail" placeholder="%%=v(@StaffEmail)=%%" value="%%=v(@StaffEmail)=%%">
          </div>
        </div>
        
    <div class="outer-block">
        <div class="row">
          <div class="align-right container">
            <button type="button" class="btn btn-outline-primary" name="UpdateStaff" id="btn-update" onClick="update('UpdateStaff');">Update</button>
          </div>
        </div>
      </div>
  
  </div> 
   
   <!-- 
  %%[ 
   VAR @RelationId, @RelationName, @RelationPhone, @RelationGender, @RelationBirthdate, @Relation
   VAR @RelationRows, @RelationRow   
   VAR @RelationRecordType
   SET @RelationRecordType = "0127F000000KIiiQAG"
   VAR @NumberRelation
   SET @RelationRows = RetrieveSalesforceObjects(
   "Relationship__c",
   "Id , Name, Phone__c, Staff_Gender__c, Account__c, Relationship__c, Birthday__c",
   "Account__c", "=" , @AccountID, "RecordTypeId", "=" ,@RelationRecordType
   )
   ]%%
   -->
   %%[IF RowCount(@RelationRows) > 0 THEN ]%% 
      <div class="outer-block">
        <h4>
          Relation Information
        </h4> 
   <!-- %%[ENDIF]%% -->
   <!-- 
   %%[
   IF RowCount(@RelationRows) > 0 THEN
    SET @NumberRelation            = RowCount(@RelationRows)
    FOR @i = 1 to RowCount(@RelationRows) do
     SET @RelationRow           = Row(@RelationRows,@i)
     SET @RelationId            = FIELD(@RelationRow, "Id")
     SET @RelationName          = FIELD(@RelationRow, "Name")
     SET @RelationPhone         = FIELD(@RelationRow, "Phone__c") 
     SET @Relation              = FIELD(@RelationRow, "Relationship__c")
     SET @RelationBirthdate     = FIELD(@RelationRow, "Birthday__c")
  ]%%
  -->
        <input type="hidden" id="RelationId%%=v(@i)=%%" name="RelationId%%=v(@i)=%%" value="%%=v(@RelationId)=%%" />
  <div class="gold-pattern row">
          <div class="inner col-6">
            <h6>
              Name
            </h6>            
          </div>
          <div class="inner col-6">
            <input type="text" class="form-control" id="RelationName%%=v(@i)=%%" name="RelationName%%=v(@i)=%%" placeholder="%%=v(@RelationName)=%%" value="%%=v(@RelationName)=%%">
          </div>
        </div>
        
   <div class="gold-pattern row">
          <div class="inner col-6">
            <h6>
              Relation
            </h6>            
          </div>
          <div class="inner col-6">
            <input type="text" class="form-control" id="Relation%%=v(@i)=%%" name="Relation%%=v(@i)=%%" placeholder="%%=v(@Relation)=%%" value="%%=v(@Relation)=%%">
          </div>
        </div>     
  
   <div class="gold-pattern row">
          <div class="inner col-6">
            <h6>
              Birth date
            </h6>            
          </div>
          <div class="inner col-6">
            <input type="date" class="form-control" id="RelationBirthdate%%=v(@i)=%%" name="RelationBirthdate%%=v(@i)=%%" placeholder="%%=v(@RelationBirthdate)=%%" value="%%=v(@RelationBirthdate)=%%">
          </div>
        </div>
        
  <div class="gold-pattern row">
          <div class="inner col-6">
            <h6>
              Phone
            </h6>            
          </div>
          <div class="inner col-6">
            <input type="text" class="form-control" id="RelationPhone%%=v(@i)=%%" name="RelationPhone%%=v(@i)=%%" placeholder="%%=v(@RelationPhone)=%%" value="%%=v(@RelationPhone)=%%">
          </div>
        </div>
  
  
   <div class="outer-block">
        <div class="row">
          <div class="align-right container">
            <button type="button" class="btn btn-outline-primary" name="UpdateRelation" id="btn-update" onClick="update('UpdateRelation%%=v(@i)=%%');">Update</button>
          </div>
        </div>
      </div>
  <!--
  %%[
   NEXT @i
  ENDIF
  ]%% 
  -->
  
  </div>        
     
     
    </div>
  </div>

  <div class="container-fluid footer-band">
    <div class="copyright">
      <p>
        &copy; 2019 content JSC<br />
        content JSC -<br />
        Tel: xxxxx.<br />
        Certificate Of Business Registration - No.: 0100107518, 5th registration dated 08/07/2016
      </p>
    </div>
  </div>
<script>
    var CONTROLLER = "<ctrl:var name=CONTROLLER />";
    
    $(document).ready(function() {
      $("#Success").hide();
      $("#Error").hide();
      $("#Load").show();
    });

    function update(action) {
      var Action            = action;
      var StaffId           = $('#StaffId').val();
      var StaffName         = $('#AccountID').val();
      var StaffCompany      = $('#AccountName').val();
      var StaffDepartment   = $('#AccountEmail').val();
      var StaffTitle        = $('#AccountPhone').val();
      var StaffGender       = document.querySelector('input[name="StaffGender"]:checked').value;
      var StaffBirthDate    = $('#StaffBirthDate').val();
      var StaffPhone        = $('#StaffPhone').val();
      var StaffEmail        = $('#StaffEmail').val();
   
      console.log(StaffGender);
      if (action == 'UpdateStaff') {     
        console.log('Action is UPDATE STAFF');
        $.ajax({
          url:      CONTROLLER,
          dataType: 'json',
          type:     'POST',
          data: {
            Action:            Action,
            StaffId:           StaffId,
            StaffName:         StaffName,
            StaffCompany:      StaffCompany,
            StaffDepartment:   StaffDepartment,
            StaffTitle:        StaffTitle,
            StaffGender:       StaffGender,
            StaffBirthDate:    StaffBirthDate,
            StaffPhone:        StaffPhone,
            StaffEmail:        StaffEmail
     
          },
          error: function(jqXHR, exception) {
            displayError(jqXHR, exception);
          },
          success: function(data) {
            result = eval(data);
            if (result.status == "Success") {
              $("#Error").hide();
              $("#Load").hide();
              $("#Success").show();
            } else {
              $("#Success").hide();
              $("#Load").hide();
              $("#Error").show();
            }
          }
        });
      }else if(action.includes('UpdateRelation')) {
          var i = action.substring(action.length - 1, action.length);
          var  RelationName      = $('#RelationName'+i).val();                 
          var  RelationPhone     = $('#RelationPhone'+i).val();    
          var  Relation          = $('#Relation'+i).val();     
          var  RelationBirthdate = $('#RelationBirthdate'+i).val();
    $.ajax({
          url:      CONTROLLER,
          dataType: 'json',
          type:     'POST',
          data: {
            Action:             Action,
            RelationName:       RelationName,
            RelationPhone:      RelationPhone,
            Relation:           Relation,
            RelationBirthdate:  RelationBirthdate
     
          },
          error: function(jqXHR, exception) {
            displayError(jqXHR, exception);
          },
          success: function(data) {
            result = eval(data);
            if (result.status == "Success") {
              $("#Error").hide();
              $("#Load").hide();
              $("#Success").show();
            } else {
              $("#Success").hide();
              $("#Load").hide();
              $("#Error").show();
            }
          }
        });

   }     
    }

    function displayError(jqXHR, exception) {
      var message;

      if (jqXHR.status === 0) {
        message = "Not connect.\n Verify Network.";
      } else if (jqXHR.status == 404) {
        message = "Requested page not found.";
      } else if (jqXHR.status == 500) {
        message = "Internal Server Error.";
      } else if (exception === 'parsererror') {
        message = "Requested JSON parse failed.";
      } else if (exception === 'timeout') {
        message = "Timeout error.";
      } else if (exception === 'abort') {
        message = "Ajax request aborted.";
      } else {
        message = "Uncaught Error.\n" + jqXHR.responseText;
      }
      alert("Error: "  + message);
    }
  </script>
  
  
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>

and this is my controller (SSJS)

<script language="javascript" runat="server">
Platform.Load("Core", "1.1.5");
var wsproxySOAP       = new Script.Util.WSProxy();

var Action            = Request.GetFormField("Action");


Variable.SetValue('@getAction', Action);
Variable.SetValue('@getContactID', ContactID);
Variable.SetValue('@getContactName', ContactName);
Variable.SetValue('@getSubID', SubID);
Variable.SetValue('@getPrefID', PrefID);
Variable.SetValue('@getEmailAddress', EmailAddress);

if (Action == 'UpdateStaff') {
  var StaffId           = Request.GetFormField("StaffId");
  var StaffName         = Request.GetFormField("StaffName");
  var StaffCompany      = Request.GetFormField("StaffCompany");
  var StaffDepartment   = Request.GetFormField("StaffDepartment");
  var StaffTitle        = Request.GetFormField("StaffTitle");
  var StaffGender       = Request.GetFormField("StaffGender");
  var StaffBirthDate    = Request.GetFormField("StaffBirthDate");
  var StaffPhone        = Request.GetFormField("StaffPhone");
  var StaffEmail        = Request.GetFormField("StaffEmail");
  
  console.log('StaffName' , StaffName);
  
  Variable.SetValue('@getStaffId', StaffId);
  Variable.SetValue('@getStaffName', StaffName);
  Variable.SetValue('@getStaffCompany', StaffCompany);
  Variable.SetValue('@getStaffDepartment', StaffDepartment);
  Variable.SetValue('@getStaffTitle', StaffTitle);
  Variable.SetValue('@getStaffGender', StaffGender);
  Variable.SetValue('@getStaffBirthDate', StaffBirthDate);
  Variable.SetValue('@getStaffPhone', StaffPhone);
  Variable.SetValue('@getStaffEmail', StaffEmail);
  
}

</script>
%%[
SET @Action             = @getAction

IF @Action == 'UpdateStaff' THEN
  SET @StaffId            = @getStaffId
  SET @StaffName          = @getStaffName
  SET @StaffCompany       = @getStaffCompany
  SET @StaffDepartment    = @getStaffDepartment
  SET @StaffTitle         = @getStaffTitle
  SET @StaffGender        = @getStaffGender
  SET @StaffBirthDate     = @getStaffBirthDate
  SET @StaffPhone         = @getStaffPhone
  SET @StaffEmail         = @getStaffEmail  
ENDIF

SET @UpdateStaffInfo = UpdateSingleSalesforceObject(
    "Relationship__c", @StaffId,
    "Name", @StaffName, 
    "Phone__c", @StaffPhone,
    "Staff_Gender__c", @StaffGender,
    "Email_VNA__c", @StaffEmail,
    "Staff_Level_6__c", @StaffDepartment,
    "Staff_Level_5__c", @StaffCompany,
    "Staff_Title__c", @StaffTitle,
    "Birthday__c", FormatDate(@StaffBirthDate, "iso"
)

VAR @Status
IF (@UpdateStaffInfo == 1) THEN
  SET @Status = "Success"
ELSE
  SET @Status = "Failure"
ENDIF
]%%
<script language="javascript" runat="server">
Platform.Load("Core", "1.1.5");

var result;
var status = Variable.GetValue('@Status');

result = {status: status};
Write(Stringify(result));

</script>

but when I try to hit the button Update, I get this error message.

enter image description here

Please can anyone let me know where did I go wrong ? Thanks in advance

0

SO I think the issue is coming from that you have a missing closing parenthesis.

SET @UpdateStaffInfo = UpdateSingleSalesforceObject(
    "Relationship__c", @StaffId,
    "Name", @StaffName, 
    "Phone__c", @StaffPhone,
    "Staff_Gender__c", @StaffGender,
    "Email_VNA__c", @StaffEmail,
    "Staff_Level_6__c", @StaffDepartment,
    "Staff_Level_5__c", @StaffCompany,
    "Staff_Title__c", @StaffTitle,
    "Birthday__c", FormatDate(@StaffBirthDate, "iso"
)

needs to be:

SET @UpdateStaffInfo = UpdateSingleSalesforceObject(
    "Relationship__c", @StaffId,
    "Name", @StaffName, 
    "Phone__c", @StaffPhone,
    "Staff_Gender__c", @StaffGender,
    "Email_VNA__c", @StaffEmail,
    "Staff_Level_6__c", @StaffDepartment,
    "Staff_Level_5__c", @StaffCompany,
    "Staff_Title__c", @StaffTitle,
    "Birthday__c", FormatDate(@StaffBirthDate, "iso")
)

I also wanted to point out two things. First, you are setting variables in AMPscript via SSJS that do not have values in SSJS nor are they used later in the Controller:

Variable.SetValue('@getContactID', ContactID);
Variable.SetValue('@getContactName', ContactName);
Variable.SetValue('@getSubID', SubID);
Variable.SetValue('@getPrefID', PrefID);
Variable.SetValue('@getEmailAddress', EmailAddress);

As well as setting up WSProxy, but not using that either:

var wsproxySOAP       = new Script.Util.WSProxy();

With your above code, these do not need to be included.

Also, and it pains me to say it as I am a HUGE fan of SSJS, but you really don't need it here. You can do the whole thing easily in just AMPscript (since you need to use AMPscript for the Salesforce Object functions). The less you switch between languages, the more performant your script/page will be.

It could probably be simplified to the following:

%%[

set @Action              = RequestParameter("Action");

IF @Action == 'UpdateStaff' THEN
  set @StaffId           = RequestParameter("StaffId");
  set @StaffName         = RequestParameter("StaffName");
  set @StaffCompany      = RequestParameter("StaffCompany");
  set @StaffDepartment   = RequestParameter("StaffDepartment");
  set @StaffTitle        = RequestParameter("StaffTitle");
  set @StaffGender       = RequestParameter("StaffGender");
  set @StaffBirthDate    = RequestParameter("StaffBirthDate");
  set @StaffPhone        = RequestParameter("StaffPhone");
  set @StaffEmail        = RequestParameter("StaffEmail");
ENDIF

SET @UpdateStaffInfo = UpdateSingleSalesforceObject(
    "Relationship__c", @StaffId,
    "Name", @StaffName, 
    "Phone__c", @StaffPhone,
    "Staff_Gender__c", @StaffGender,
    "Email_VNA__c", @StaffEmail,
    "Staff_Level_6__c", @StaffDepartment,
    "Staff_Level_5__c", @StaffCompany,
    "Staff_Title__c", @StaffTitle,
    "Birthday__c", FormatDate(@StaffBirthDate, "iso")
)

VAR @Status
IF (@UpdateStaffInfo == 1) THEN
  SET @Status = "Success"
ELSE
  SET @Status = "Failure"
ENDIF

OUTPUT(CONCAT("{status:",@status,"}"))

]%%
| improve this answer | |
  • Thanks Gortonington for your answer however when I change my controller as your advice the same issue still happened. The code of my controller is bellow. – Trung Hieu Tran Jun 25 at 14:47
  • In addition, it return "Failed to load resource: the server responded with a status of 500 (Internal Server Error)" and then – Trung Hieu Tran Jun 25 at 15:23
  • Have you tried just running the processing page by itself with manually adding fields? The only potential issue I can see in the processing is if the action is not Update Staff, then it will still try to run the UpdateSingleSalesforceObject with null values. – Gortonington Jun 25 at 16:21
  • whenever I try to hit the update button it always return that issue and when I try to get access into controller by using the direct URL I get this "500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed." Could it be the root cause ? – Trung Hieu Tran Jun 26 at 2:54
  • And I think by somehow the javascript in controller does not get what has been pass into it and I try to using OUTPUT(CONCAT("{action:",@Action,"}")) in ampscript in the controller file and see it return null. – Trung Hieu Tran Jun 26 at 2:59
0

I would like to update the answer to this controller.

Turn out the error root cause was because of the un support function in SSJS. And in this case I need to remove all the console.log() call in SSJS out.

<script language="javascript" type="text/javascript" runat="server">
Platform.Load("Core", "1.1.5");

var Action            = Request.GetFormField("Action");
var TargetNumber      = Request.GetFormField("TargetNumber");
// console.log('SSJS Action', Action);

Variable.SetValue('@getAction', Action);

if (Action == 'UpdateStaff') {
  var StaffId           = Request.GetFormField("StaffId");
  var StaffName         = Request.GetFormField("StaffName");
  var StaffCompany      = Request.GetFormField("StaffCompany");
  var StaffDepartment   = Request.GetFormField("StaffDepartment");
  var StaffTitle        = Request.GetFormField("StaffTitle");
  var StaffGender       = Request.GetFormField("StaffGender");
  var StaffBirthDate    = Request.GetFormField("StaffBirthDate");
  var StaffPhone        = Request.GetFormField("StaffPhone");
  var StaffEmail        = Request.GetFormField("StaffEmail");
  
  // console.log('StaffName' , StaffName);
  
  Variable.SetValue('@getStaffId', StaffId);
  Variable.SetValue('@getStaffName', StaffName);
  Variable.SetValue('@getStaffCompany', StaffCompany);
  Variable.SetValue('@getStaffDepartment', StaffDepartment);
  Variable.SetValue('@getStaffTitle', StaffTitle);
  Variable.SetValue('@getStaffGender', StaffGender);
  Variable.SetValue('@getStaffBirthDate', StaffBirthDate);
  Variable.SetValue('@getStaffPhone', StaffPhone);
  Variable.SetValue('@getStaffEmail', StaffEmail);
  
}else{
  var RelationId        = Request.GetFormField("RelationId");
  var RelationName      = Request.GetFormField("RelationName");
  var RelationPhone     = Request.GetFormField("RelationPhone");
  var RelationBirthdate = Request.GetFormField("RelationBirthdate");
  var Relation          = Request.GetFormField("Relation");
  
  Variable.SetValue('@getRelationId', RelationId);
  Variable.SetValue('@getRelationName', RelationName);
  Variable.SetValue('@getRelationPhone', RelationPhone);
  Variable.SetValue('@getRelationBirthdate', RelationBirthdate);
  Variable.SetValue('@getRelation', Relation);
}
</script>
%%[

set @Action              = @getAction

IF @Action == "UpdateStaff" THEN
  SET @StaffId            = @getStaffId
  SET @StaffName          = @getStaffName
  SET @StaffCompany       = @getStaffCompany
  SET @StaffDepartment    = @getStaffDepartment
  SET @StaffTitle         = @getStaffTitle
  SET @StaffGender        = @getStaffGender
  SET @StaffBirthDate     = @getStaffBirthDate
  SET @StaffPhone         = @getStaffPhone
  SET @StaffEmail         = @getStaffEmail  
  
  SET @UpdateStaffInfo = UpdateSingleSalesforceObject(
    "Relationship__c", @StaffId,
    "Name", @StaffName, 
    "Phone__c", @StaffPhone,
    "Staff_Gender__c", @StaffGender,
    "Email_VNA__c", @StaffEmail,
    "Staff_Level_6__c", @StaffDepartment,
    "Staff_Level_5__c", @StaffCompany,
    "Staff_Title__c", @StaffTitle,
    "Birthday__c", FormatDate(@StaffBirthDate, "iso")
 
) 
ELSE 
  SET @RelationId            = @getRelationId
  SET @RelationName          = @getRelationName
  SET @RelationPhone         = @getRelationPhone
  SET @RelationBirthdate     = @getRelationBirthdate
  SET @Relation              = @getRelation
  
   SET @UpdateRelationInfo = UpdateSingleSalesforceObject(
    "Relationship__c", @RelationId,
    "Name", @RelationName, 
    "Phone__c", @RelationPhone,
    "Relationship__c", @Relation,
    "Birthday__c", FormatDate(@RelationBirthdate, "iso") 
) 
ENDIF

VAR @Status
IF (@UpdateStaffInfo == 1) OR (@UpdateRelationInfo == 1) THEN
  SET @Status = "Success"
ELSE
  SET @Status = "Failure"
ENDIF

/*
OUTPUT(CONCAT("{status:",@status,"}"))
OUTPUT(CONCAT("{action:",@Action,"}"))
OUTPUT(CONCAT("{UpdateStaffInfo:",@UpdateStaffInfo,"}"))
*/

]%%
<script language="javascript" runat="server">
Platform.Load("Core", "1.1.5");

var result;
var status = Variable.GetValue('@Status');

result = {status: status};
Write(Stringify(result));

</script>
| 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.