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
ravinvravinv 

Case to Lead conversion trigger

Hi everyone,

 

I just started using couple months ago and now have just started exploring triggers.

I am looking to create new lead records after case has been closed if it fulfills certain criterias.

Hope someone could help me out here; details are as below.

 

To create new Lead record when from Case object:-

 

Status=Closed

and

Receive_survey_form_future_promotions__c=True

and

(E_mail_c or SuppliedEmail)!=null

 

The Lead record field name mapping for insertion are as follows:-

From Case object ==> To Lead object

Name_c or SuppliedName ==> LastName

E_mail_c or SuppliedEmail ==> Email

Contact_Number__c ==> MobilePhone

 

Lead status will always be New.

Lastly, the lead will only be created if a search based on the email from (E_mail_c or SuppliedEmail) isn't existant in any Lead records.

 

Hope to hear from someone and thanks in advance.

 

P.S. Just wondering, can an email message be triggered to a particular default email if the Lead record isn't created due to its existance in Lead already?

 

Regards,

Ravin

Navatar_DbSupNavatar_DbSup

Hi,


Try the below code as reference and made changes accordingly:
trigger AutoLead on Case(after insert)
{
case c=trigger.new[0];
if(Status=='Closed' && Receive_survey_form_future_promotions__c=True && (E_mail_c or SuppliedEmail)!=null)
{
list<lead> l=[select id,Email from lead where email != c.E_mail_c];

if(l.size()==0)
{
lead l=new lead(lastname=c.name__c,Email=c.E_mail_c,MobilePhone=Contact_Number__c,Company='test');
insert l;
}

}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

ravinvravinv

Hi,

 

I tried it and it had 2 errors, first was the "or" word stated, so I changed it to "||". Then the error below came up, this am not too sure how to handle it.

 

Compile Error: unexpected token: 'c.E_mail_c' at line 6 column 55

Navatar_DbSupNavatar_DbSup

Hi,

Try the modified code:

change E_mail_c with E_mail__c

Try the below code as reference and made changes accordingly:
trigger AutoLead on Case(after insert)
{
case c=trigger.new[0];
if(c.Status=='Closed' && c.Receive_survey_form_future_promotions__c=True && (c.E_mail__c || c.SuppliedEmail)!=null)
{
list<lead> l=[select id,Email from lead where email != c.E_mail__c];

if(l.size()==0)
{
lead l=new lead(lastname=c.name__c,Email=c.E_mail__c,MobilePhone=c.Contact_Number__c,Company='test');
insert l;
}

}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

ravinvravinv

Hi there..

 

Same error occured, "Compile Error: unexpected token: 'c.E_mail__c' at line 6 column 55".

Navatar_DbSupNavatar_DbSup

Hi,


Have you checked the custom field that you have created for email API name =E_mail__c inside the case object??? If not then make the API name as this or use this API name inside the Apex.

ravinvravinv

Hi,

 

Yes, checked, all the API names are as per my first post..just did a double check on it...

Navatar_DbSupNavatar_DbSup

Hi,


I have created the same field on Case and written the same trigger here and its working fine. Please go through the below code as reference:
trigger AutoLead on Case(after insert)
{
case c=trigger.new[0];
if(c.Status=='Working' && c.Receive_survey_form_future_promotions__c==True && c.E_mail__c != null)
{
list<lead> l=[select id,Email from lead where email =: c.E_mail__c];

if(l.size()==0)
{
lead l1=new lead(lastname=c.name__c,Email=c.E_mail__c,MobilePhone=string.valueof(c.Contact_Number__c),Company='test');
insert l1;
}

ravinvravinv

Hi,

 

It's at last working. You have a been a great help. Thanks a lot.

 

By the way, I changed the code slightly as below.

Also, currently, it checks on all leads to see whether it exists before creating; if I want the trigger to check all my contacts too which includes business and person account contact, can I know what code should be added in?

Also, would also want to add a check of SuppliedEmail together with the current E_mail too before inserting any records. Tried adding the code in, but errors started to come up.

 

trigger AutoLead on Case(after insert)
{
case c=trigger.new[0];
if(c.Status=='Closed' && c.Receive_survey_form_future_promotions__c==True && c.E_mail__c != null)
{
list<lead> l=[select id,Email from lead where email =: c.E_mail__c];

if(l.size()==0)
{
lead l1=new lead(lastname=c.name__c,Email=c.E_mail__c,Phone=c.Contact_Number__c);
insert l1;
}
}
}

ravinvravinv

Hi,

 

Was thinking another way for this but was facing error messages, hope you can point me to the correct direction.

 

Instead of searching all my contacts as what I mentioned in my previous post, I was thinking to use where if ContactEmail is null, then to create a lead using this current trigger. But, this would mean that another trigger for before insert would need to be created to check E_mail against all my contacts and populate the field Account and ContactEmail in the Case. This new trigger is like what the feature "E-mail to Case" does where it checks SuppliedEmail against Contacts and populate the Account and ContactEmail.

 

I have editted my code below to try this, but am facing an error as below. This came up after I added the code in red.

Compile Error: Invalid field ContactEmail for SObject Case at line 5 column 57

 

trigger AutoLead on Case(after insert,after update)
{
case c=trigger.new[0];
if(c.Status=='Closed' && c.Receive_survey_form_future_promotions__c==True
&& (c.E_mail__c != null || c.SuppliedEmail != null) && (c.ContactEmail==null))
{
list<lead> l=[select id,Email from lead where email =: c.E_mail__c];

if(l.size()==0)
{
lead l1=new lead(lastname=c.name__c,Email=c.E_mail__c,Phone=c.Contact_Number__c);
insert l1;
}
}
}

Navatar_DbSupNavatar_DbSup

Hi,
Contactemail is not valid field of case. So you have to change c.contactemail into c.contact.email. Your trigger should be this:
trigger AutoLead on Case(after insert,after update)
{
case c=trigger.new[0];
if(c.Status=='Closed' && c.Receive_survey_form_future_promotions__c==True
&& (c.E_mail__c != null || c.SuppliedEmail != null) && (c.Contact.Email==null))
{
list<lead> l=[select id,Email from lead where email =: c.E_mail__c];

if(l.size()==0)
{
lead l1=new lead(lastname=c.name__c,Email=c.E_mail__c,Phone=c.Contact_Number__c);
insert l1;
}
}
}

ravinvravinv

Hi,

 

I just tried that, but though when there is a contact email from account on the case, the lead is still created.

 

Also, can the below lines get the email addresses, phone and name respectively from either 2 different fields?

 

list<lead> l=[select id,Email from lead where email =: c.E_mail__c]; ==> additional field: SuppliedEmail

lead l1=new lead(lastname=c.name__c,Email=c.E_mail__c,Phone=c.Contact_Number__c); ==> additional field: for name: SuppliedName, for email: SuppliedEmail and for phone: SuppliedPhone

ravinvravinv

Hi,

 

I tried duplicating the whole trigger with changing only the existing values to the web values I mentioned above and it seems to work fine. On the Contact.Email I tried changing it to ContactID instead and this works fine too.

Please advise if this is correct as what I did is pretty much based on trial and error.

 

If the method mentioned above is fine, next, would be the trigger to update the contact fields on the case based on checking of the E_mail. Would be grateful if you could guide me on how to go about this. I presume, this would be before insert and before update, right? Below is the code currently. Thanks a lot.

 

trigger AutoLead on Case(after insert,after update)
{
case c=trigger.new[0];
if(c.Status=='Closed' && c.Receive_survey_form_future_promotions__c==True
&& (c.E_mail__c != null || c.SuppliedEmail != null) && c.ContactID == null)
{
list<lead> l=[select id,Email from lead where email =: c.E_mail__c];

if(l.size()==0)
{
lead l1=new lead(lastname=c.name__c,Email=c.E_mail__c,Phone=c.Contact_Number__c);
insert l1;
}
}
}

 

 

trigger AutoLead2 on Case(after insert,after update)
{
case c=trigger.new[0];
if(c.Status=='Closed' && c.Receive_survey_form_future_promotions__c==True
&& (c.E_mail__c != null || c.SuppliedEmail != null) && c.ContactID == null)
{
list<lead> l=[select id,Email from lead where email =: c.SuppliedEmail];

if(l.size()==0)
{
lead l1=new lead(lastname=c.SuppliedName,Email=c.SuppliedEmail,Phone=c.SuppliedPhone);
insert l1;
}
}
}

ziaullahkhaksarziaullahkhaksar

HI....

Can any body tell me .. how i know that new Lead has been created....??

i need it for ... "when new Lead created then i want to sent an e-mail to that new lead.????

Any body please help me.

MUSFAR KT 6MUSFAR KT 6
Hi everyone,


@istest
public class Prefix_Dr_test {
    testmethod static void test(){
        Lead L=New Lead();
        L.FirstName='Dr.Midul';
        L.LastName='ahmed';
        L.Company='sfdc';
        L.Status='Working-contacted'; 
        insert L;
        
        Lead Le=[select Id,firstname,lastname from lead where company='sfdc'];
       System.assertEquals(Le.FirstName.ContainsIgnoreCase('Dr.'),'Dr.');
           
    }
"""""""""""""""""""""""""""""""""""""""""""""""""""
trigger Prefix_Dr on Lead (before insert,before update) {
    for(lead L:trigger.new){
       L.FirstName='Dr.'+L.FirstName;
        }
}

here test class is showing error.