1

I'm new to coding but have been an admin of salesforce for a few years so hopefully this is something that is simple to sort. I'm trying to create a trigger that creates a contact when a lead is created and when a lead field called Paymentinfo__c has the value "createcontact". I've created the code below but it isn't creating the contact. Is is possible to create a contact in this way or is my code wrong?

trigger rttest on Lead (after insert) {
    for(Lead u : Trigger.new)
    {
        if ('createcontact'.equals(u.Paymentinfo__c))
        {
            Contact c=new Contact();
            c.owner=u.owner;
            c.firstname=u.firstname;
            c.lastname=u.lastname;
            c.Product_for_Activation__c='NO PRODUCT';
            c.Contact_Status__c='Non-User';
            insert c;
        }
    }
}
  • I believe the code is correct apart from the fact that DML is in the for loop which will give errors. For now please check if the trigger is active and it will be helpful if you can post the debug log. Do you get any errors when you create a lead? – manjit5190 Mar 10 '16 at 17:22
  • also .equals() is case-sensitive so if there is a 'Createcontact' value that won't work while == is case-insensitive. – Oles Malkov Mar 10 '16 at 17:28
  • 1
    Just use Process Builder... – Adrian Larson Mar 10 '16 at 17:30
3

You should not use code to accomplish this objective. Instead use Process Builder to create a flow.

  1. Navigate to Create > Workflows & Approvals > Process Builder.

  2. Click New.
    Fill in Process Name > Create Contact.
    No need to fill in API Name, it will auto-populate.
    You may fill in an optional Description.
    Click Save.

  3. Click + Add Object.
    Select Lead.
    Click Save.

  4. Click + Add Criteria.
    Fill in Criteria Name > Insert.
    Select Formula Evaluates to True.
    Fill in AND(ISNEW(), [Lead].PaymentInfo__c ="createcontact").
    Click Save.

  5. Click + Add Action.
    Select Action Type > Create a Record.
    Fill in Action Name > Create Contact.
    Select Record Type > Contact.
    Select Field > Last Name.
    Select Type > Reference.
    Select Value > [Lead].LastName.
    Add other fields as above.
    Click Save.

  6. Click Activate.

  • 1
    Thanks very much Adrian! That's worked a treat. Thanks again for taking the time to time to answer this with great instructions – richtea432 Mar 11 '16 at 9:37
0

Please use the following code and check it. Also enable debug log and check if there is any exception throwing while inserting contacts and add appropriate exception handler if necessary.

trigger rttest on Lead (after insert) {

List<contact> conInsertList = new List<contact>();
for(Lead u : Trigger.new)
 {

    if (u.Paymentinfo__c == 'createcontact')
        {

            Contact c=new Contact();
            c.owner=u.owner;
            c.firstname=u.firstname;
            c.lastname=u.lastname;
            c.Product_for_Activation__c='NO PRODUCT';
            c.Contact_Status__c='Non-User';
            conInsertList.add(c);
    }

 }

if(conInsertList.size()>0)
         Insert conInsertList;

}

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.