function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
John LatimerJohn Latimer 

Creating a Contact when Creating a New Account with a Field = True

I created a Trigger to create a New Contact when an Account is created.  I only want the Contact created when a boolean field is True. The Trigger will create the Contact when the if(acc.Diamond_Contractor__c == '1') is commented out, but never creates a Contact if the statement is included.  Am I checking for the field too soon in the Trigger?  Thanks!  John!

trigger CreateAccountContact on Account (after insert, after update){

if(Trigger.isInsert){
    
    List<Contact> ct = new List <Contact>();

    for(Account acc : trigger.new){
if(acc.Diamond_Contractor__c == '1') {
        Contact c = new Contact(LastName = 'User',
                    FirstName = 'Extranet',
                    AccountId=acc.id,
                    Fax=acc.Fax,
                    MailingStreet=acc.BillingStreet,
                    MailingCity=acc.BillingCity,
                    /* similarly add all fields which you want */
                    MailingState=acc.BillingState,
                    MailingPostalCode=acc.BillingPostalCode,
                    MailingCountry=acc.BillingCountry,
                    Phone=acc.Phone);

        ct.add(c);
    }
    insert ct;
}
}
}
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Please see below:
 
trigger CreateAccountContact on Account (after insert, after update){

if(Trigger.isInsert){
    
    List<Contact> ct = new List <Contact>();

    for(Account acc : trigger.new){
       if(acc.Diamond_Contractor__c) {
/* Try this version of expression and see if works! also check what is the default value at the field level is it checked or unchecked
Go to field in the object schema, edit the field and in that screen you can select a default behaviour
Check the debug log and see if any other process is changing the checkbox value before saving the record, like before trigger if you have any!
*/
        Contact c = new Contact(LastName = 'User',
                    FirstName = 'Extranet',
                    AccountId=acc.id,
                    Fax=acc.Fax,
                    MailingStreet=acc.BillingStreet,
                    MailingCity=acc.BillingCity,
                    /* similarly add all fields which you want */
                    MailingState=acc.BillingState,
                    MailingPostalCode=acc.BillingPostalCode,
                    MailingCountry=acc.BillingCountry,
                    Phone=acc.Phone);

        ct.add(c);
    }
}

insert ct;// DML should be out side of for loop

}
}

Thanks,
Balaji
John LatimerJohn Latimer
Thanks for the reply! I changed the if statement to if(acc.Diamond_Contractor__c) and it gives an error that it must be a Boolean statement.
Balaji Chowdary GarapatiBalaji Chowdary Garapati
I assumed Diamond_Contractor__c  is a Checkbox as you mentioned it is a Boolean, isnt it not the case? what type is Diamond_Contractor__c ?
John LatimerJohn Latimer
Balaji: It is a Picklist – None – default Yes, No.
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Then try this:

You mentioned it as a boolean variable, where it is not! It is a picklist which has string values Yes,No,None. So you need to verify against those valid values instead you were validating the value against "1". So try the below version now!
trigger CreateAccountContact on Account (after insert, after update){

if(Trigger.isInsert){
    
    List<Contact> ct = new List <Contact>();

    for(Account acc : trigger.new){
       if(acc.Diamond_Contractor__c=='Yes') {

        Contact c = new Contact(LastName = 'User',
                    FirstName = 'Extranet',
                    AccountId=acc.id,
                    Fax=acc.Fax,
                    MailingStreet=acc.BillingStreet,
                    MailingCity=acc.BillingCity,
                    /* similarly add all fields which you want */
                    MailingState=acc.BillingState,
                    MailingPostalCode=acc.BillingPostalCode,
                    MailingCountry=acc.BillingCountry,
                    Phone=acc.Phone);

        ct.add(c);
    }
}

insert ct;// DML should be out side of for loop

}
}

Hope it helps:

Thanks,
balaji