salesforce更改master-detail值

修改Master-Details类型字段的值


在salesforce中,master-detail字段值必填,且后面不可更改,可以将sobject转成map进行更改。

Savepoint sp = Database.setSavepoint();
OpportunityLineItem line = [select id, OpportunityId from OpportunityLineItem where OpportunityId != null limit 1];
Opportunity oppty = [select id from Opportunity where id != :line.OpportunityId limit 1];
//line.Opportunity = oppty.Id; 报错, master details关系不可修改master id
System.debug(LoggingLevel.DEBUG, '*** Opportunity1: ' + line.OpportunityId);//旧的master id
//先把子对象转json字符串,再把json字符串转成Object,再把Object转map,
Map<String, Object> mp = (Map<String, Object>)(JSON.deserializeUntyped(Json.serialize(line)));
//修改master id
mp.put('Opportunity', oppty.Id);
//map转json, json再转子对象
OpportunityLineItem line2 = (OpportunityLineItem)Json.deserializeStrict(Json.serialize(mp), OpportunityLineItem.class);
//更新子对象,这样就能更改mater id了
update line2;
System.debug(LoggingLevel.DEBUG, '*** Opportunity2: ' + line2.OpportunityId);//

Database.rollback(sp);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值