通过使用 Suitelet 脚本将 link 发送到他的电子邮件来激活客户

activation of customer by sending link to his email using Suitelet Scripts

概览:

我使用 Suitelet Script 创建了一个 自定义客户表单,在 GET 方法中包含姓名、电子邮件、phone 等字段,并且在此字段中输入的值在 POST 方法由 request.getParameter() 方法。客户记录已成功创建,但是当 单击提交按钮时,客户记录将处于 "inactive" 模式并且 url link 将发送到客户电子邮件地址,当他点击 link 时,他的记录将更改为 "active" 模式,这必须在同一个 suitelet 页面中。

我的要求:

我使用非活动模式成功创建了客户记录,link 这会发送客户邮件,但我需要如何在他单击 link 时将客户记录设置为活动模式,结果是在同一个套件页面中查看。

我在下面给出了我的代码示例:

function getCustomerInfo(request, response){
       if(request.getMethod() == 'GET'){
        //CREATING THE CUSTOM FORM AND ADDING FIELDS IN THE FORM
        var form = nlapiCreateForm('Custom Customer Form');
           form.addField('sfg_company', 'text', 'Company Name').setMandatory(true);
           form.addField('sfg_address1','text','Address1');
           form.addField('sfg_address2','text','Address2');
           form.addField('sfg_city','text','City');
           form.addField('sfg_state','text','State');
           form.addField('sfg_emailaddr','email','Email').setMandatory(true);
           form.addField('sfg_phone','phone','Phone');
           form.addSubmitButton('Submit');
           response.writePage(form);
           
        }else{
         nlapiLogExecution('DEBUG','form',form);
         // CREATING THE RECORD BY GETTING THE VALUES ENTERED IN THE CUSTOM FORM
          var compName = request.getParameter('sfg_company');
             var compAdd1 = request.getParameter('sfg_address1');    
             var compAdd2 = request.getParameter('sfg_address2');   
             var cities = request.getParameter('sfg_city');
             var stateName =request.getParameter('sfg_state');
             var email_addr = request.getParameter('sfg_emailaddr');
             var phone_num  = request.getParameter('sfg_phone');
             var newCust = nlapiCreateRecord('customer');
             newCust.setFieldValue('companyname', compName);
             newCust.setLineItemValue('addressbook', 'addr1', 1, compAdd1);
             newCust.setLineItemValue('addressbook', 'addr2', 1, compAdd2);
             newCust.setLineItemValue('addressbook', 'city', 1, cities);
             newCust.setLineItemValue('addressbook', 'state', 1, stateName);
             newCust.setFieldValue('email', email_addr);
             newCust.setFieldValue('phone', phone_num);
             newCust.setFieldValue('subsidiary', 1);
             newCust.setFieldValue('isinactive','T');
             //sending activation link to the customer
             var sender = nlapiGetUser();
             var receiver =  email_addr;
             var subject = 'Customer Activation Link';
             var recordId = nlapiGetRecordId();
             var url = "https://system.na1.netsuite.com/app/common/entity/custjob.nl?id="+recordId+"&whence=";
             var body = 'Dear Customer,Your Record is Created Successfully and it will activated by click the following link :'+ url;
             nlapiSendEmail(sender,receiver,subject,body);
             var id = nlapiSubmitRecord(newCust);
             nlapiSetRedirectURL('RECORD','customer',id,null,false);
         
        }
         
   }

听起来您基本上想要的是向客户发送确认 link,他们可以点击并完成某种注册。

如果您想重复使用您的套件代码,那么您需要传递一个自定义阶段或操作参数,您可以从中触发不同的操作。例如:

var stage = request.getParameter('custparam_action') || 'showForm';
switch(stage){
  case 'showForm': doShowForm(request, response); break;
  case 'validate' : doValidate(request, response); break;
}...

那么您需要发送的 url 需要引用 suitelet 而不是 Netsuite 中的客户页面:

var ctx = nlapiGetContext();
var url = nlapiResolveURL('SUITELET', ctx.getScriptId(), ctx.getDeploymentId(), true) +"&custparam_action=validate"+ getSecureValidationParams());

函数 getSecureValidationParams 应该创建某种时间敏感参数,包括散列键。如果请求通过验证,那么您将显示您想要显示或重定向到或显示感谢页面的任何其他形式。

function getCustomerInfo(request, response){
    if(request.getParameter('custscript_sfg_custmer_param') != null){
        var value = request.getParameter('custscript_sfg_custmer_param')
        var loadRecord = nlapiLoadRecord('customer',value);
        loadRecord.setFieldValue('isinactive','F');
                loadRecord.setFieldValue('custentity_sfg_referredby',1);
        nlapiSubmitRecord(loadRecord);
        nlapiSetRedirectURL('RECORD','customer',value);
    }
    if(request.getParameter('custscript_sfg_custmer_param') == null){
        if(request.getMethod() == 'GET'){
               //CREATING THE CUSTOM FORM AND ADDING FIELDS IN THE FORM
               var form = nlapiCreateForm('Custom Customer Form');
               form.addField('sfg_company', 'text', 'Company Name').setMandatory(true);
               form.addField('sfg_address1','text','Address1');
               form.addField('sfg_address2','text','Address2');
               form.addField('sfg_city','text','City');
               form.addField('sfg_state','text','State');
               form.addField('sfg_emailaddr','email','Email').setMandatory(true);
               form.addField('sfg_phone','phone','Phone');
               form.addSubmitButton('Submit');
               response.writePage(form);        
    }
   }
    if(request.getMethod() == 'POST'){
        nlapiLogExecution('DEBUG','form',form);
        // CREATING THE RECORD BY GETTING THE VALUES ENTERED IN THE CUSTOM FORM
         var compName = request.getParameter('sfg_company');
         var compAdd1 = request.getParameter('sfg_address1');    
         var compAdd2 = request.getParameter('sfg_address2');   
         var cities = request.getParameter('sfg_city');
         var stateName =request.getParameter('sfg_state');
         var email_addr = request.getParameter('sfg_emailaddr');
         var phone_num  = request.getParameter('sfg_phone');
         var newCust = nlapiCreateRecord('customer');
         newCust.setFieldValue('companyname', compName);
         newCust.setLineItemValue('addressbook', 'addr1', 1, compAdd1);
         newCust.setLineItemValue('addressbook', 'addr2', 1, compAdd2);
         newCust.setLineItemValue('addressbook', 'city', 1, cities);
         newCust.setLineItemValue('addressbook', 'state', 1, stateName);
         newCust.setFieldValue('email', email_addr);
         newCust.setFieldValue('phone', phone_num);
         newCust.setFieldValue('subsidiary', 1);
         newCust.setFieldValue('isinactive','T');
         //sending activation link to the customer
         var sender = nlapiGetUser();
         var receiver =  email_addr;
         var subject = 'Customer Activation Link';
         var recordId = nlapiGetRecordId();
         var webAddress = "https://system.na1.netsuite.com"
         var scriptType = nlapiGetContext().getScriptId();
         var scriptId = nlapiGetContext().getDeploymentId();
         var location = nlapiResolveURL('SUITELET',scriptType,scriptId);
         var id = nlapiSubmitRecord(newCust);
         var link = webAddress+location+'&custscript_sfg_custmer_param='+id+"&whence=";
         var url = '<a href="'+link+'">Click Here </a>';
         var body = 'Dear Customer,Your Record is Created Successfully and it will activated by click the following link :'+ url;
         nlapiSendEmail(sender,receiver,subject,body);
         nlapiSetRedirectURL('SUITELET',scriptType,scriptId);

    }

以上代码将通过外部Suitelet表单获取客户信息并保存在系统中。