zoukankan      html  css  js  c++  java
  • 如何给SAP Cloud for Customer UI上的字段添加自定义校验逻辑

    There is a good blog SAP Cloud for Customer Phone Number Parsing and Formatting which gives a detail explanation about the parse logic of phone number parse logic in C4C. By default it allows the Alphanumeric Value to be entered, the reason of this behavior is explained in note 2525818 – In an Account and Contact, the Phone Number Allows Alphanumeric Value.

    However, at least in China, a valid mobile phone number only consists of pure numeric values. The requirement of local customer is whenever the value other than pure number is maintained, the error message will be popped up and the save is terminated.

    The fulfilled scenario is: for example if I input an “A” character in Mobile field and press save button, error message is raised:

    And save is prevented.

    Here below is how to achieve this custom validation logic in Cloud Application Studio.
    Create an extension field phoneInvalidRel in BO BusinessPartnerRelationship, and declare the error message to be raised when invalid digit is detected.

    Implement BeforeSave event. This event is responsible to check whether the mobile phone number consists of pure numeric value and set the check result into extension field phoneInvalidRel, which will be used by onSave Validation later.

    import ABSL;
    
    var phone;
    this.phoneInvalidRel = false;
    
    var BPRelationship = this;
    if ( !BPRelationship.ContactPerson.IsSet())
    	return;
    var contact = BPRelationship.ContactPerson;
    if( !contact.ContactPersonWorkplaceAddressInformation.IsSet())
    	return;
    var workplaceAddress = contact.ContactPersonWorkplaceAddressInformation;
    
    if( !workplaceAddress.ContactPersonWorkplaceAddress.DefaultMobilePhone.IsSet())
    	return;
    phone = workplaceAddress.ContactPersonWorkplaceAddress.DefaultMobilePhone;
    if( phone.FormattedNumberDescription.IsInitial())
    	return;
    if( phone.FormattedNumberDescription.FindRegex("1\d{10}") < 0){
       // raise "Invalid mobile phone number!";
       this.phoneInvalidRel = true;
       raise Error_phone_msg_rel.Create("E", phone.FormattedNumberDescription );
    }
    

    Implement OnSave validation, and simply use the check result stored in extension field phoneInvalidRel.

    import ABSL;
    return !this.phoneInvalidRel;
    

    In the runtime, when I enter “A” in mobile phone field and save, first the BeforeSave event is triggered,

    And then the onSave Validation is called.

    The above implementation could only cover the scenario that end user directly changes the mobile phone number which is technically modelled in BO BusinessPartnerRelationship. There is another possibility that end user will also changes the field on Root node of BO BusinessPartner, as a result the same logic should be done again on BO BusinessPartner as well.

    BeforeSave implementation on BusinessPartner Root node:

    import ABSL;
    
    var phone;
    var common = this.Common.GetFirst();
    common.phoneInvalid = false;
    
    if( !this.CurrentDefaultIsContactPersonFor.IsSet())
    	return;
    var currentDefaultContact = this.CurrentDefaultIsContactPersonFor;
    
    var BPRelationship = currentDefaultContact.BusinessPartnerRelationship;
    if ( !BPRelationship.ContactPerson.IsSet())
    	return;
    var contact = BPRelationship.ContactPerson;
    if( !contact.ContactPersonWorkplaceAddressInformation.IsSet())
    	return;
    var workplaceAddress = contact.ContactPersonWorkplaceAddressInformation;
    
    if( !workplaceAddress.ContactPersonWorkplaceAddress.DefaultMobilePhone.IsSet())
    	return;
    phone = workplaceAddress.ContactPersonWorkplaceAddress.DefaultMobilePhone;
    if( phone.FormattedNumberDescription.IsInitial())
    	return;
    if( phone.FormattedNumberDescription.FindRegex("1\d{10}") < 0){
       // raise "Invalid mobile phone number!";
       common.phoneInvalid = true;
       raise Error_phone_msg.Create("E", phone.FormattedNumberDescription );
    }
    

    OnSave Validation on BusinessPartner Root node:

    import ABSL;
    
    var common = this.Common.GetFirst();
    return !common.phoneInvalid;
    

    要获取更多Jerry的原创文章,请关注公众号"汪子熙":

  • 相关阅读:
    Windows如何快速远程到另一台Windows并管理多个远程服务器
    基于视觉反馈的步进电机X-Y平台控制
    相机位姿估计3:根据两幅图像的位姿估计结果求某点的世界坐标
    相机位姿估计2:[应用]实时位姿估计与三维重建相机姿态
    相机位姿估计1_1:OpenCV:solvePnP二次封装与性能测试
    求空间内两条直线的最近距离以及最近点的坐标(C++)
    相机位姿估计1:根据四个特征点估计相机姿态
    相机位姿估计0:基本原理之如何解PNP问题
    记2016年中国移动广西公司面试(计算机类)
    子坐标系C在父坐标系W中的旋转问题
  • 原文地址:https://www.cnblogs.com/sap-jerry/p/13577037.html
Copyright © 2011-2022 走看看