简体   繁体   English

Salesforce父ID触发器

[英]Salesforce Parent ID Trigger

I have a question, I hope I can tell you clearly my problem. 我有一个问题,我希望我能清楚地告诉你我的问题。

Regarding the parentfunction at the account, I added two fields to the account ParentID (15) and ParentIDLong (18) . 关于帐户的父功能,我在帐户ParentID(15)ParentIDLong(18)中添加了两个字段 The ParentID field is a formulafield with the code below. ParentID字段是一个公式字段,其代码如下。

IF(LEN(Parent.Id) < 1, Id, 
IF(LEN(Parent.Parent.Id) < 1, (Parent.Id), 
IF(LEN(Parent.Parent.Parent.Id) < 1, (Parent.Parent.Id), 
IF(LEN(Parent.Parent.Parent.Parent.Id) < 1, (Parent.Parent.Parent.Id), 
IF(LEN(Parent.Parent.Parent.Parent.Parent.Id) < 1,    (Parent.Parent.Parent.Parent.Id), 
IF(LEN(Parent.Parent.Parent.Parent.Parent.Parent.Id) < 1, (Parent.Parent.Parent.Parent.Parent.Id), 
IF(LEN(Parent.Parent.Parent.Parent.Parent.Parent.Parent.Id) < 1, (Parent.Parent.Parent.Parent.Parent.Parent.Id), 
IF(LEN(Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Id) < 1,(Parent.Parent.Parent.Parent.Parent.Parent.Parent.Id),">7"))))))))

Now I need the ParentLongID, unfortunately its not possible to change the formula, because I will get an error which is "the formula is too big". 现在我需要ParentLongID,遗憾的是它不可能改变公式,因为我会得到一个错误,即“公式太大”。

I wrote a trigger which posted the long ID into the field ParentLongID (code below). 我写了一个触发器,它将长ID发布到字段ParentLongID(下面的代码)中。

Trigger SetParentIDLong on Account (before update) {

FOR (Account Acc : Trigger.new) {

   Account oldAcc = Trigger.oldMap.get(Acc.Id);

String idStr = Acc.ParentID__c;
Id idVal = idStr;
idStr = idVal;

If (oldAcc.ParentId__c != Acc.ParentID__c) {
    Acc.ParentIdLong__c = idStr;
}

So far so good, it works if I create the hierarchy from the top to the bottom. 到目前为止,如果我从上到下创建层次结构,它就可以工作。

Example: 例:

MotherCompany ParentIDLong: blank MotherCompany ParentIDLong:空白

Daughter1 ParentIDLong: 001b000000XUTQAAA5 Daughter1 ParentIDLong:001b000000XUTQAAA5

Daughter2 ParnetIDLong: 001b000000XUTQAAA5 Daughter2 ParnetIDLong:001b000000XUTQAAA5

But if I add a MotherMotherCompany 但是,如果我添加MotherMotherCompany

Example: 例:

MotherMotherCompany ParentIDLong: blank MotherMotherCompany ParentIDLong:空白

MotherCompany ParentIDLong: 001m000000Ft35yAAB MotherCompany ParentIDLong:001m000000Ft35yAAB

Daughter1 ParentIDLong: 001b000000XUTQAAA5 Daughter1 ParentIDLong:001b000000XUTQAAA5

Daughter2 ParnetIDLong: 001b000000XUTQAAA5 Daughter2 ParnetIDLong:001b000000XUTQAAA5

The trigger doesn't work, because the trigger changed only the ParentIDLong at the Mothercompany. 触发器不起作用,因为触发器仅更改了Mothercompany中的ParentIDLong。

Its possible to change with trigger all other companies as well? 它可以随着触发所有其他公司而改变吗?

Thanks, Sascha 谢谢,Sascha

In order to create the relationship of all other accounts you would need to have an update account on this trigger so your trigger is called recursively. 为了创建所有其他帐户的关系,您需要在此触发器上拥有更新帐户,以便递归调用您的触发器。

like this: 像这样:

Trigger SetParentIDLong on Account (before update) {

FOR (Account Acc : Trigger.new) {
 Account oldAcc = Trigger.oldMap.get(Acc.Id);

 String idStr = Acc.ParentID__c;
 Id idVal = idStr;
 idStr = idVal;

 If (oldAcc.ParentId__c != Acc.ParentID__c) {
  Acc.ParentIdLong__c = idStr;
  update Acc; //this line would call again your trigger
              //recursively until the last Account
 }

try and let us know how it went 试着让我们知道它是怎么回事

OK Let's try this: 好的,让我们试试这个:

Trigger SetParentIDLong on Account (before update) {
    List<Id> accids = new List<Id>();

    for(Account a : Trigger.New){
        accids.add(a.Id);
    }

    List<Account> accounts = [SELECT Id,ParentId__c FROM Account WHERE Id IN :accids];

    for(Account child : accounts){
        Account oldAcc = Trigger.oldMap.get(child.Id);

        String idStr = child.ParentID__c;
        Id idVal = idStr;
        idStr = idVal;

        if(oldAcc.ParentId__c != child.ParentID__c) {
        child.ParentIdLong__c = idStr;
        update child; //this line would call again your trigger
                    //recursively until the last Account
        }

    }
}

I just changed some names and created a new list of accounts based on the trigger.new because we cannot update the trigger.new, 我刚刚更改了一些名称,并根据trigger.new创建了一个新的帐户列表,因为我们无法更新trigger.new,

Let's see if this works. 让我们看看这是否有效。

Keep me posted 让我发布

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM