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
@taani.ax1426@taani.ax1426 

Lead Title should be mapped to Opportunity Name on conversion

Hi,

 

I want Lead Title should be mapped to Opportunity Name on conversion of lead. Please help me with some piece of code.

 

Eg:

Lead title is "XYZ", when I convert that lead, new opp should be created with the Opp Name: XYZ

 

Regards,

Taani

Best Answer chosen by Admin (Salesforce Developers) 
Laxman RaoLaxman Rao

try this:

 

trigger InfluencerTrig on Lead (after update,before update) {
if (Trigger.isBefore)
{
set<Id> contactIds = new set<Id>();
for(Lead newLead : Trigger.new)
{
if(newLead.Contact__c != null)
{
contactIds.add(newLead.Contact__c);
}
}
map<Id, Contact> mapOfIdAndContacts = new map<Id, Contact>([Select Email From Contact Where Id In: contactIds]);
for(Lead newLeadObj : Trigger.new)
{
if(newLeadObj.IsConverted && newLeadObj.Contact__c != null)
{
newLeadObj.Email = mapOfIdAndContacts.get(newLeadObj.Contact__c).Email;
}
}
}
else if(Trigger.isUpdate) {
Set<Id> leadIds = new Set<Id>();
Map<Id,Id> LeadIdContactId = new Map<Id,Id>();
//Map<Id,Id> LeadIdAccountId = new Map<Id,Id>();
Map<Id,Id> LeadIdOpportunityId = new Map<Id,Id>();
List<Opportunity> lstopp = new List<Opportunity>();
for (Integer i = 0; i < Trigger.new.size(); i++)
{
if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false)
{
leadIds.add(Trigger.new[i].id);
LeadIdContactId.put(Trigger.new[i].id,Trigger.new[i].ConvertedContactId);
//LeadIdAccountId.put(Trigger.new[i].id,Trigger.new[i].ConvertedAccountId);
LeadIdOpportunityId.put(Trigger.new[i].id,Trigger.new[i].ConvertedOpportunityId);
Opportunity oppty = new Opportunity(Id=Trigger.new[i].ConvertedOpportunityId);
oppty.Name=Trigger.new[i].Title;
lstopp.add(oppty);
}
//Update Opporutnity Name with Lead Title
update lstopp;
//Create a list of CustomObject__c to be updated
List<Influencer__c> CustomObjectsToUpdate = new List<Influencer__c>();

List<Influencer__c> customObjectEntries = [select Contact__c, Opportunity__c, Lead__c from Influencer__c where lead__c in :leadIds];
for (Lead lead : [select id,ConvertedContactId,ConvertedOpportunityId from Lead where id in: leadIds]) {
for (Influencer__c CustomObject : customObjectEntries) {
if (CustomObject.Lead__c == lead.Id) {
CustomObject.contact__c = LeadIdContactId.get(lead.id);
CustomObject.opportunity__c = LeadIdOpportunityId.get(lead.id);
CustomObjectsToUpdate.add(CustomObject);
}
}
}
if(CustomObjectsToUpdate.size()>0)
update CustomObjectsToUpdate;
} //End of trigger
}
}

All Answers

Hengky IlawanHengky Ilawan

Hi,

 

You can write a trigger in the opportunity object and get the Lead Title by using the Opportunity's Lead Source field.

 

Regards,

Hengky

@taani.ax1426@taani.ax1426

Could you please help me with code:(

 

thanks

Hengky IlawanHengky Ilawan

Hi,

 

Try this Before-Insert trigger for opportunity object (not tested though)

Set<Id> leadIds = new Set<Id>();
for (Opportunity opp : trigger.new) {
    leadIds.add(opp.LeadSource);
}
Map<Id, Lead> leadMap = new Map<Id, Lead>([SELECT Title FROM Lead WHERE Id IN :leadIds]);
for (Opportunity opp : trigger.new) {
    if (opp.LeadSource != null) {
        opp.Name = leadMap.get(opp.LeadSource).Title;
    }
}

 

Let me know if it works.

 

Regards,

Hengky

@taani.ax1426@taani.ax1426

Getting error:

Error: Compile Error: Illegal assignment from LIST<Lead> to MAP<Id,Lead> at line 4 column 1

Hengky IlawanHengky Ilawan

Yeah. Was trying to assign a list to a map. Fixed.

 

Regards,

Hengky

@taani.ax1426@taani.ax1426

so what can be done now?

 

Regards,

taani

Laxman RaoLaxman Rao

Write a trigger on Lead :

 

trigger CopyLeadTitleToOppName on Lead (after update) {

list<Opportunity> oppsToUpdate = new list<Opportunity>();
for(Lead newLead : trigger.New) {
if(newLead.IsConverted && newLead.ConvertedOpportunityId != null) {
oppsToUpdate.add(new Opportunity(Id = newLead.ConvertedOpportunityId, Name = newLead.Title));
}
}

if(!oppsToUpdate.isEmpty()) {
update oppsToUpdate;
}
}

 

@taani.ax1426@taani.ax1426

HI,

 

Thanks for the soln, but it was alraedy being managed. Now I am facing one problem, on the Lead page layout, i made a lookup from Contact(Contact__c), now what the requirement is, as user chose the record from Contact, the email from selected Contact should come and populate in email field of Lead. I can write trigger seperate, but i have to write all trigger in one. Pasting my code here, just plz add the part of populated field, if possible..Chk bold part of the code.

 

trigger InfluencerTrig on Lead (after update,before update) {
Set<Id> leadIds = new Set<Id>();
    Map<Id,Id> LeadIdContactId = new Map<Id,Id>();
    //Map<Id,Id> LeadIdAccountId = new Map<Id,Id>();
    Map<Id,Id> LeadIdOpportunityId = new Map<Id,Id>();
   
    List<Opportunity> lstopp = new List<Opportunity>();
    for (Integer i = 0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
             leadIds.add(Trigger.new[i].id);
             LeadIdContactId.put(Trigger.new[i].id,Trigger.new[i].ConvertedContactId);
             //LeadIdAccountId.put(Trigger.new[i].id,Trigger.new[i].ConvertedAccountId);
             LeadIdOpportunityId.put(Trigger.new[i].id,Trigger.new[i].ConvertedOpportunityId);

              Opportunity oppty = new Opportunity(Id=Trigger.new[i].ConvertedOpportunityId);
              oppty.Name=Trigger.new[i].Title; 
              lstopp.add(oppty);
         }
        if (Trigger.isBefore) {

 

(Here i have to add for that requirement)
        

    }
    //Update Opporutnity Name with Lead Title
    update lstopp;
    //Create a list of CustomObject__c to be updated
    List<Influencer__c> CustomObjectsToUpdate = new List<Influencer__c>();
      
          
    List<Influencer__c> customObjectEntries = [select Contact__c, Opportunity__c, Lead__c from Influencer__c where lead__c in :leadIds];      
  


    for (Lead lead : [select id,ConvertedContactId,ConvertedOpportunityId from Lead where id in: leadIds])  {
             for (Influencer__c CustomObject : customObjectEntries) {
                if (CustomObject.Lead__c == lead.Id) {
                    CustomObject.contact__c = LeadIdContactId.get(lead.id);
                    CustomObject.opportunity__c = LeadIdOpportunityId.get(lead.id);
                                        CustomObjectsToUpdate.add(CustomObject);
                 }
            }
     }


     if(CustomObjectsToUpdate.size()>0)
         update CustomObjectsToUpdate;
 
} //End of trigger

Laxman RaoLaxman Rao

try this:

 

trigger InfluencerTrig on Lead (after update,before update) {
Set<Id> leadIds = new Set<Id>();
Map<Id,Id> LeadIdContactId = new Map<Id,Id>();
//Map<Id,Id> LeadIdAccountId = new Map<Id,Id>();
Map<Id,Id> LeadIdOpportunityId = new Map<Id,Id>();

List<Opportunity> lstopp = new List<Opportunity>();
for (Integer i = 0; i < Trigger.new.size(); i++){
if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
leadIds.add(Trigger.new[i].id);
LeadIdContactId.put(Trigger.new[i].id,Trigger.new[i].ConvertedContactId);
//LeadIdAccountId.put(Trigger.new[i].id,Trigger.new[i].ConvertedAccountId);
LeadIdOpportunityId.put(Trigger.new[i].id,Trigger.new[i].ConvertedOpportunityId);

Opportunity oppty = new Opportunity(Id=Trigger.new[i].ConvertedOpportunityId);
oppty.Name=Trigger.new[i].Title;
lstopp.add(oppty);
}
if (Trigger.isBefore) {
set<Id> contactIds = new set<Id>();

for(Lead newLead : Trigger.new)
{
if(newLead.IsConverted && newLead.Contact__c != null)
{
contactIds.add(newLead.Contact__c);
}
}

map<Id, Contact> mapOfIdAndContacts = new map<Id, Contact>([Select Email From Contact Where Id In: contactIds]);

for(Lead newLead : Trigger.new)
{
if(newLead.IsConverted && newLead.Contact__c != null)
{
newLead.Email = mapOfIdAndContacts.get(newLead.Contact__c).Email;
}
}

}
//Update Opporutnity Name with Lead Title
update lstopp;
//Create a list of CustomObject__c to be updated
List<Influencer__c> CustomObjectsToUpdate = new List<Influencer__c>();


List<Influencer__c> customObjectEntries = [select Contact__c, Opportunity__c, Lead__c from Influencer__c where lead__c in :leadIds];


for (Lead lead : [select id,ConvertedContactId,ConvertedOpportunityId from Lead where id in: leadIds]) {
for (Influencer__c CustomObject : customObjectEntries) {
if (CustomObject.Lead__c == lead.Id) {
CustomObject.contact__c = LeadIdContactId.get(lead.id);
CustomObject.opportunity__c = LeadIdOpportunityId.get(lead.id);
CustomObjectsToUpdate.add(CustomObject);
}
}
}


if(CustomObjectsToUpdate.size()>0)
update CustomObjectsToUpdate;

} //End of trigger

@taani.ax1426@taani.ax1426

Thanks for quick reply,

 

But I am getting error: 

Error: Compile Error: unexpected token: '[' at line 31 column 58

 

 

 

And one more thing, this updation of field, should be at the time of creation of new Lead, not after conversion..

 

Regards,

Taani

Laxman RaoLaxman Rao

try this :

 

map<Id, Contact> mapOfIdAndContacts = new map<Id, Contact>([Select Email From Contact Where Id In: contactIds]);

@taani.ax1426@taani.ax1426

It too not working :(

 

Error: Compile Error: Variable does not exist: newLead.Email at line 35 column 1

Laxman RaoLaxman Rao

Can you paste your code.

@taani.ax1426@taani.ax1426

trigger InfluencerTrig on Lead (after update,before update) {
Set<Id> leadIds = new Set<Id>();
Map<Id,Id> LeadIdContactId = new Map<Id,Id>();
//Map<Id,Id> LeadIdAccountId = new Map<Id,Id>();
Map<Id,Id> LeadIdOpportunityId = new Map<Id,Id>();

List<Opportunity> lstopp = new List<Opportunity>();
for (Integer i = 0; i < Trigger.new.size(); i++){
if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
leadIds.add(Trigger.new[i].id);
LeadIdContactId.put(Trigger.new[i].id,Trigger.new[i].ConvertedContactId);
//LeadIdAccountId.put(Trigger.new[i].id,Trigger.new[i].ConvertedAccountId);
LeadIdOpportunityId.put(Trigger.new[i].id,Trigger.new[i].ConvertedOpportunityId);
Opportunity oppty = new Opportunity(Id=Trigger.new[i].ConvertedOpportunityId);
oppty.Name=Trigger.new[i].Title;
lstopp.add(oppty);
}
if (Trigger.isBefore) {
set<Id> contactIds = new set<Id>();

for(Lead newLead : Trigger.new)
{
if(newLead.IsConverted && newLead.Contact__c != null)
{
contactIds.add(newLead.Contact__c);
}
}

map<Id, Contact> mapOfIdAndContacts = new map<Id, Contact>([Select Email From Contact Where Id In: contactIds]);

for(Lead newLead : Trigger.new)
{
if(newLead.IsConverted && newLead.Contact__c != null)
{
newLead.Email = mapOfIdAndContacts.get(newLead.Contact__c).Email;
}
}
}
//Update Opporutnity Name with Lead Title
update lstopp;
//Create a list of CustomObject__c to be updated
List<Influencer__c> CustomObjectsToUpdate = new List<Influencer__c>();


List<Influencer__c> customObjectEntries = [select Contact__c, Opportunity__c, Lead__c from Influencer__c where lead__c in :leadIds];

for (Lead lead : [select id,ConvertedContactId,ConvertedOpportunityId from Lead where id in: leadIds]) {
for (Influencer__c CustomObject : customObjectEntries) {
if (CustomObject.Lead__c == lead.Id) {
CustomObject.contact__c = LeadIdContactId.get(lead.id);
CustomObject.opportunity__c = LeadIdOpportunityId.get(lead.id);
CustomObjectsToUpdate.add(CustomObject);
}
}
}

if(CustomObjectsToUpdate.size()>0)
update CustomObjectsToUpdate;

} //End of trigger

Laxman RaoLaxman Rao

try this 

trigger InfluencerTrig on Lead (after update,before update) {
Set<Id> leadIds = new Set<Id>();
Map<Id,Id> LeadIdContactId = new Map<Id,Id>();
//Map<Id,Id> LeadIdAccountId = new Map<Id,Id>();
Map<Id,Id> LeadIdOpportunityId = new Map<Id,Id>();
List<Opportunity> lstopp = new List<Opportunity>();
for (Integer i = 0; i < Trigger.new.size(); i++){
if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
leadIds.add(Trigger.new[i].id);
LeadIdContactId.put(Trigger.new[i].id,Trigger.new[i].ConvertedContactId);
//LeadIdAccountId.put(Trigger.new[i].id,Trigger.new[i].ConvertedAccountId);
LeadIdOpportunityId.put(Trigger.new[i].id,Trigger.new[i].ConvertedOpportunityId);
Opportunity oppty = new Opportunity(Id=Trigger.new[i].ConvertedOpportunityId);
oppty.Name=Trigger.new[i].Title;
lstopp.add(oppty);
}
if (Trigger.isBefore) {
set<Id> contactIds = new set<Id>();
for(Lead newLead : Trigger.new)
{
if(newLead.IsConverted && newLead.Contact__c != null)
{
contactIds.add(newLead.Contact__c);
}
}
map<Id, Contact> mapOfIdAndContacts = new map<Id, Contact>([Select Email From Contact Where Id In: contactIds]);
for(Lead newLeadObj : Trigger.new)
{
if(newLeadObj.IsConverted && newLeadObj.Contact__c != null)
{
newLeadObj.Email = mapOfIdAndContacts.get(newLeadObj.Contact__c).Email;
}
}
}
//Update Opporutnity Name with Lead Title
update lstopp;
//Create a list of CustomObject__c to be updated
List<Influencer__c> CustomObjectsToUpdate = new List<Influencer__c>();

List<Influencer__c> customObjectEntries = [select Contact__c, Opportunity__c, Lead__c from Influencer__c where lead__c in :leadIds];
for (Lead lead : [select id,ConvertedContactId,ConvertedOpportunityId from Lead where id in: leadIds]) {
for (Influencer__c CustomObject : customObjectEntries) {
if (CustomObject.Lead__c == lead.Id) {
CustomObject.contact__c = LeadIdContactId.get(lead.id);
CustomObject.opportunity__c = LeadIdOpportunityId.get(lead.id);
CustomObjectsToUpdate.add(CustomObject);
}
}
}
if(CustomObjectsToUpdate.size()>0)
update CustomObjectsToUpdate;
} //End of trigger

 

If you get any error, then mark that line as red

@taani.ax1426@taani.ax1426
Error: Compile Error: expecting right curly bracket, found 'EOF' at line 0 column -1

Laxman RaoLaxman Rao

try this:

 

trigger InfluencerTrig on Lead (after update,before update) {
if (Trigger.isBefore)
{
set<Id> contactIds = new set<Id>();
for(Lead newLead : Trigger.new)
{
if(newLead.IsConverted && newLead.Contact__c != null)
{
contactIds.add(newLead.Contact__c);
}
}
map<Id, Contact> mapOfIdAndContacts = new map<Id, Contact>([Select Email From Contact Where Id In: contactIds]);
for(Lead newLeadObj : Trigger.new)
{
if(newLeadObj.IsConverted && newLeadObj.Contact__c != null)
{
newLeadObj.Email = mapOfIdAndContacts.get(newLeadObj.Contact__c).Email;
}
}
}
else if(Trigger.isUpdate) {
Set<Id> leadIds = new Set<Id>();
Map<Id,Id> LeadIdContactId = new Map<Id,Id>();
//Map<Id,Id> LeadIdAccountId = new Map<Id,Id>();
Map<Id,Id> LeadIdOpportunityId = new Map<Id,Id>();
List<Opportunity> lstopp = new List<Opportunity>();
for (Integer i = 0; i < Trigger.new.size(); i++)
{
if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false)
{
leadIds.add(Trigger.new[i].id);
LeadIdContactId.put(Trigger.new[i].id,Trigger.new[i].ConvertedContactId);
//LeadIdAccountId.put(Trigger.new[i].id,Trigger.new[i].ConvertedAccountId);
LeadIdOpportunityId.put(Trigger.new[i].id,Trigger.new[i].ConvertedOpportunityId);
Opportunity oppty = new Opportunity(Id=Trigger.new[i].ConvertedOpportunityId);
oppty.Name=Trigger.new[i].Title;
lstopp.add(oppty);
}
//Update Opporutnity Name with Lead Title
update lstopp;
//Create a list of CustomObject__c to be updated
List<Influencer__c> CustomObjectsToUpdate = new List<Influencer__c>();

List<Influencer__c> customObjectEntries = [select Contact__c, Opportunity__c, Lead__c from Influencer__c where lead__c in :leadIds];
for (Lead lead : [select id,ConvertedContactId,ConvertedOpportunityId from Lead where id in: leadIds]) {
for (Influencer__c CustomObject : customObjectEntries) {
if (CustomObject.Lead__c == lead.Id) {
CustomObject.contact__c = LeadIdContactId.get(lead.id);
CustomObject.opportunity__c = LeadIdOpportunityId.get(lead.id);
CustomObjectsToUpdate.add(CustomObject);
}
}
}
if(CustomObjectsToUpdate.size()>0)
update CustomObjectsToUpdate;
} //End of trigger
}
}

@taani.ax1426@taani.ax1426

It got saved, but not working:(

Laxman RaoLaxman Rao

try this:

 

trigger InfluencerTrig on Lead (after update,before update) {
if (Trigger.isBefore)
{
set<Id> contactIds = new set<Id>();
for(Lead newLead : Trigger.new)
{
if(newLead.Contact__c != null)
{
contactIds.add(newLead.Contact__c);
}
}
map<Id, Contact> mapOfIdAndContacts = new map<Id, Contact>([Select Email From Contact Where Id In: contactIds]);
for(Lead newLeadObj : Trigger.new)
{
if(newLeadObj.IsConverted && newLeadObj.Contact__c != null)
{
newLeadObj.Email = mapOfIdAndContacts.get(newLeadObj.Contact__c).Email;
}
}
}
else if(Trigger.isUpdate) {
Set<Id> leadIds = new Set<Id>();
Map<Id,Id> LeadIdContactId = new Map<Id,Id>();
//Map<Id,Id> LeadIdAccountId = new Map<Id,Id>();
Map<Id,Id> LeadIdOpportunityId = new Map<Id,Id>();
List<Opportunity> lstopp = new List<Opportunity>();
for (Integer i = 0; i < Trigger.new.size(); i++)
{
if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false)
{
leadIds.add(Trigger.new[i].id);
LeadIdContactId.put(Trigger.new[i].id,Trigger.new[i].ConvertedContactId);
//LeadIdAccountId.put(Trigger.new[i].id,Trigger.new[i].ConvertedAccountId);
LeadIdOpportunityId.put(Trigger.new[i].id,Trigger.new[i].ConvertedOpportunityId);
Opportunity oppty = new Opportunity(Id=Trigger.new[i].ConvertedOpportunityId);
oppty.Name=Trigger.new[i].Title;
lstopp.add(oppty);
}
//Update Opporutnity Name with Lead Title
update lstopp;
//Create a list of CustomObject__c to be updated
List<Influencer__c> CustomObjectsToUpdate = new List<Influencer__c>();

List<Influencer__c> customObjectEntries = [select Contact__c, Opportunity__c, Lead__c from Influencer__c where lead__c in :leadIds];
for (Lead lead : [select id,ConvertedContactId,ConvertedOpportunityId from Lead where id in: leadIds]) {
for (Influencer__c CustomObject : customObjectEntries) {
if (CustomObject.Lead__c == lead.Id) {
CustomObject.contact__c = LeadIdContactId.get(lead.id);
CustomObject.opportunity__c = LeadIdOpportunityId.get(lead.id);
CustomObjectsToUpdate.add(CustomObject);
}
}
}
if(CustomObjectsToUpdate.size()>0)
update CustomObjectsToUpdate;
} //End of trigger
}
}

This was selected as the best answer
@taani.ax1426@taani.ax1426

:(..so sorry, but email field on Lead is not get updated...:(..

Laxman RaoLaxman Rao

may I know how u tested?