单据退出时,对于指定字段,不校验字段是否修改原创
金蝶云社区-CRU杨少博
CRU杨少博
14人赞赏了该文章 1574次浏览 未经作者许可,禁止转载编辑于2021年07月19日 17:34:53

业务场景:

单据退出时,平台会校验提示,存在修改的字段,是否退出?

image.png



如果想要退出单据时不提示该校验,代码可以这样操作

@Override
     public void beforeClosed(BeforeClosedEvent e) {
         super.beforeClosed(e);
         e.setCheckDataChange(false);
     }


以上代码是用来设置所有已经修改的字段都不做是否改动校验。

 

但是,如果业务需要,只有各别字段不需要校验是否修改,应该怎么操作呢?show code

/**
	 * 描述:设置动态对象值改变状态
	 *
	 * @param dataEntity 	对象
	 * @param isChanged		对象属性值是否改变
	 * @param propKeys		对象属性
	 */
	public static void setBizChanged(DynamicObject dataEntity, boolean isChanged, String... propKeys) {
		DataEntityState dataEntityState = dataEntity.getDataEntityState();
		DataEntityPropertyCollection properties = dataEntity.getDataEntityType().getProperties();

		// 单据头
		for (String propKey : propKeys) {
			IDataEntityProperty property = properties.get(propKey);
			if(property != null){
				dataEntityState.setBizChanged(property.getOrdinal(), isChanged);
				
				// 基础资料类型的字段,需要同时设置basedata_id字段BizChanged,不然不会生效
				if (property instanceof BasedataProp) {
					property = properties.get(propKey + "_id");
					Optional.ofNullable(property).ifPresent(p -> {
						dataEntityState.setBizChanged(p.getOrdinal(), isChanged);
					});
				}
			}
		}
// 分录
		List<ICollectionProperty> collectionProperties = properties.getCollectionProperties(false);
		for (ICollectionProperty collectionPropertie :collectionProperties){

			// 树形分录
			if(collectionPropertie instanceof TreeEntryProp){
				DynamicObjectCollection entryColl = dataEntity.getDynamicObjectCollection(collectionPropertie.getName());
				DataEntityPropertyCollection treeEntryProperties = entryColl.getDynamicObjectType().getProperties();

				// 分录存在属性
				for (String propKey : propKeys) {
					IDataEntityProperty property = treeEntryProperties.get(propKey);
					if(property != null) {
						for (DynamicObject entry : entryColl) {

							// 基础资料属性特殊处理(同时设置ID列)
							if(property instanceof BasedataProp) {
								String baseDataIdPropKey = propKey + "_" + ReTrdConst.ID;
								IDataEntityProperty  baseDataIdProperty = treeEntryProperties.get(baseDataIdPropKey);
								if(baseDataIdProperty != null){
									entry.getDataEntityState().setBizChanged(baseDataIdProperty.getOrdinal(), isChanged);
								}
							}

							entry.getDataEntityState().setBizChanged(property.getOrdinal(), isChanged);
						}
					}
				}

			}
		}
	}


 

 

欢迎大家一起交流学习


 


赞 14