0

I have a simple trigger but I cannot figure out why it's not insert Account List Item records. Here is the code.

Trigger Eisai_createAcctListItem on Affiliation_vod__c (before insert){

    Set<String> affToAcctSet = new Set<String>();
    List<Account_List_vod__c> existingAcctList = new List<Account_List_vod__c>();
    List<Account_List_Item_vod__c> toInsertAcctListItem = new List<Account_List_Item_vod__c>();

    for(Affiliation_vod__c newAff: Trigger.New)
    {
        if(newAff.Parent_vod__c = false)
        {
            affToAcctSet.add(newAff.To_Account_Value__c);
        }   
    }

    existingAcctList = [SELECT Id, Name_Substring__c FROM Account_List_vod__c WHERE Name_Substring__c IN :affToAcctSet];

    for(Account_List_vod__c allAcctList: existingAcctList){
        for(Affiliation_vod__c affRec : Trigger.New)
        {
            Account_List_Item_vod__c AccListItem = new Account_List_Item_vod__c(
                                      Account_List_vod__c = allAcctList.Id,
                                      Account_vod__c = affRec.From_Account_vod__c
            );
            toInsertAcctListItem.add(AccListItem);
        }
    }

    Database.insert(toInsertAcctListItem,false));

}//end of trigger

Basically, what I want to happen is, after child affiliation record was inserted, the trigger should find the Account List record (Master) where Name_Substring__c is in the Set of named 'affToAcctSet' then will create Account List Item record (Details)

  • So... what are you seeing for totalRecordsInserted and totalRecordsFailed? – Adrian Larson Jul 25 '16 at 19:14
  • no, sorry I just coppied that from my class :( – Mariones Jul 25 '16 at 19:14
  • Put in more system debugs, and figure out what list is failing, it also looks like you can use Maps here to improve the overall function of the trigger – EricSSH Jul 25 '16 at 19:18
  • 1
    One thing to change is newAff.Parent_vod__c = false (which is always false) to this newAff.Parent_vod__c == false or this !newAff.Parent_vod__c (which is true when the value is false and false when the value is true). – Keith C Jul 25 '16 at 19:46
  • 1
    this is the nth OP I've seen with field names suffixed by vod__c - what does the vod stand for? In my line of business, it would be video-on-demand but I don't think that's it here. Curious – cropredy Jul 25 '16 at 20:48
1

This may not fix your issue but it is an issue, I bet you affToAcctSet is null because per your comment so with that being said this will need to be an After Insert trigger, you will also have to look into Keiths answer. So as I said put in System.debug('affToAcctSet is null?' + affToAcctSet);

To add, I don't know your structure but if you move this to an After Insert/Update ..? You can use the id of the account, rather than relying on a String to do your query

  • Hi @EricSSH, I cannot use the Account id in my query for existingAcctList because there is no look up field for the Account, only Account List Name and Name_SubString (Formula to get the Account name). I already changed it to After insert event and set newAff.Parent_vod__c == false (It's working) but I think it would be better to use MAP? not sure yet – Mariones Jul 25 '16 at 21:01
  • That would need to be another question after you tried a few things, your data structure does seem kind of wonky which would be a job for another day.. You will have to key your Map<String, account...>? Take a look at blog.jeffdouglas.com/2011/01/06/fun-with-salesforce-collections – EricSSH Jul 25 '16 at 21:07
0

Not exactly sure of the relationships but something like this may be what you are looking for:

Trigger Eisai_createAcctListItem on Affiliation_vod__c (before insert) {

    Set<String> names = new Set<String>();
    for (Affiliation_vod__c a: Trigger.New) {
        if (a.Parent_vod__c == false) {
            names.add(a.To_Account_Value__c);
        }   
    }

    // Only works if only 1 record per Name_Substring__c
    Map<String, Id> m = new Map<String, Id>();
    for (Account_List_vod__c> a : [
            SELECT Id, Name_Substring__c
            FROM Account_List_vod__c
            WHERE Name_Substring__c IN :names
            ]) {
        m.put(a.Name_Substring__c, a.Id);
    }

    List<Account_List_Item_vod__c> inserts = new List<Account_List_Item_vod__c>();
    for (Affiliation_vod__c a : Trigger.New) {
        if (a.Parent_vod__c == false) {
            Id id = m.get(a.To_Account_Value__c);
            if (id != null) {
                inserts.add(new Account_List_Item_vod__c(
                        Account_List_vod__c = id,
                        Account_vod__c = a.From_Account_vod__c
                        ));
            }
        }
    }
    Database.insert(inserts, false);
}
  • To_Account_Value__c is a formula field you make this an after insert @Noiram you should be set – EricSSH Jul 26 '16 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.