-2

can some one help me to figure out what changes i need to make on this trigger Hello ,
The intention of this trigger is to associate Contacts to different accounts depending on the contacts record type. Contacts with Record Type 'US' should be related to the Account whose name is 'US', Contacts with Record Type 'Canada' should be related to the Account whose name is 'Canada' Contact Trigger mentioned below:

trigger ContactTrigger on Contact (before insert, after insert, before update, after update, before delete, after delete) {

private static final String CANADA= 'Canada';
private static final String US = 'US';

if(Trigger.isafter){
    if((trigger.isInsert || Trigger.isUpdate) && Trigger.isafter){
        for (Contact c : trigger.new) {
            if (c.AccountId == null || c.AccountId == ''){
                if (c.RecordType.DeveloperName == 'US') {
                    c.AccountId = getDefaultAccountIdUS();
                } else {
                    c.AccountId = getDefaultAccountCN();
                }
            }
        }
    }
}

private static Id getDefaultAccountCN(){
    return [SELECT Id FROM Account WHERE Name = :CANADA LIMIT 1].Id;
}

private static Id getDefaultAccountIdUS(){
    return [select Id from Account where Name = :US limit 1].Id;
}

}

Contact Trigger Test:

@isTest
public class ContactTrigger_Test {
@isTest static void createContact() {
    Contact theContact = new Contact(LastName = 'Smith');
    try {
        insert theContact;
    } catch (Exception e) {

    }
}
}
  • 3
    What is your specific question? What does the code do now? What is wrong? What errors are thrown? Please read How to Ask and edit your post to add information. – David Reed Nov 15 at 19:35
  • sure thanks David I will make sure HOW TO ASK QUESTIONS , I am getting error execution of AfterInsert caused by: System.StringException: Invalid id: Trigger.ContactTrigger: line 9, column 1 while saving contacts – Hari Dave Nov 15 at 20:00
  • Please edit your question. We ask that you not use comments to add information. – David Reed Nov 15 at 20:09
1

You may be having an unrelated problem problem here. As I see your code, you are trying to update the Contact using Trigger.new in an after trigger. You won't be able to do so.

You should review your code and make the necessary changes. E.g., a working version of this could be using this approach in before trigger.

if((trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore) {
    for (Contact c : trigger.new) {
        if (c.AccountId == null || c.AccountId == '') {
            if (c.RecordType.DeveloperName == 'US') {
                c.AccountId = getDefaultAccountIdUS();
            } else {
                c.AccountId = getDefaultAccountCN();
            }
        }
    }
}
  • Nice catch. There were so many other issues, I missed this one. – sfdcfox Nov 15 at 21:27
  • thanks @Jayant after using after trigger i am able to save contacts but when I save contact for example US the account is not created can you please provide some suggestions will be much helpful – Hari Dave Nov 17 at 4:31
  • @HariDave you will need to open another question with specific problem you are facing. At SFSE we keep one problem specific to one question. – Jayant Das Nov 17 at 14:39

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