" CONTACT"记录应该CONVERT回到" LEAD"删除联系人中的ACCOUNT NAME时记录

时间:2014-09-01 20:26:54

标签: triggers salesforce apex-code soql

每当联系人帐户记录发生变化时,联系人都应转换回潜在客户。我有从联系到领导的映射字段。这可能吗 ?怎么做到这一点?

我试图通过编写触发器来实现:

trigger insertLead on Contact (before update,before delete) {
    Set<Id> aId = new Set<Id>();
    Lead myLead = new Lead();

    for (Contact opp : Trigger.new ) {
        aId.add(opp.AccountId);
        List<Account> acc = [select Name from Account where Id in:aId];
        List<Contact> con = [select LastName,FirstName from Contact where accountId = :aId];

        for(Account a: acc){
            myLead.Company = a.Name;
        }

        for(Contact c: con)
        {
            myLead.LastName = c.LastName;
            myLead.FirstName = c.FirstName;
        }
    insert myLead;
}

这是错误:

  

错误:Apex触发器insertLead导致意外异常,       联系您的管理员:insertLead:执行BeforeUpdate       由以下原因引起:System.DmlException:插入失败。第一个例外       在第0行;第一个错误:REQUIRED_FIELD_MISSING,必填字段       缺少:[公司]:[公司]:Trigger.insertLead:第15行,第1列

1 个答案:

答案 0 :(得分:0)

我已经检查过,您的触发器在联系人更新时工作正常。 要使用删除添加以下条件。

trigger insertLead on Contact (before update,before delete) {
    Set<Id> aId = new Set<Id>();
    Lead myLead = new Lead();
    if (Trigger.isUpdate)
    {
    for (Contact opp : Trigger.new ) {
        aId.add(opp.AccountId);
        List<Account> acc = [select Name from Account where Id in:aId];
        List<Contact> con = [select LastName,FirstName from Contact where AccountId = :aId];

        for(Account a: acc){
            myLead.Company = a.Name;
        }

        for(Contact c: con)
        {
            myLead.LastName = c.LastName;
            myLead.FirstName = c.FirstName;
        }
        insert myLead;
    }
    else if(if (Trigger.isDelete)
    {
        for (Contact opp : Trigger.old ) {
            aId.add(opp.AccountId);
            List<Account> acc = [select Name from Account where Id in:aId];
            List<Contact> con = [select LastName,FirstName from Contact where AccountId = :aId];

            for(Account a: acc){
                myLead.Company = a.Name;
            }

            for(Contact c: con)
            {
                myLead.LastName = c.LastName;
                myLead.FirstName = c.FirstName;
            }
            insert myLead;
        }
    }
}

此致

纳温

SSE

http://www.autorabit.com