You need to sign in to do that
Don't have an account?
@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
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
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
Could you please help me with code:(
thanks
Hi,
Try this Before-Insert trigger for opportunity object (not tested though)
Let me know if it works.
Regards,
Hengky
Getting error:
Error: Compile Error: Illegal assignment from LIST<Lead> to MAP<Id,Lead> at line 4 column 1
Yeah. Was trying to assign a list to a map. Fixed.
Regards,
Hengky
so what can be done now?
Regards,
taani
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;
}
}
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
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
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
try this :
map<Id, Contact> mapOfIdAndContacts = new map<Id, Contact>([Select Email From Contact Where Id In: contactIds]);
It too not working :(
Can you paste your 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) {
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
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
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
}
}
It got saved, but not working:(
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
}
}
:(..so sorry, but email field on Lead is not get updated...:(..
may I know how u tested?