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
Leena ReddyLeena Reddy 

lead mapping

i have a custom field in lead and opportunity as AGE__C.and age__c field in lead is mapped to account.but now i need the same age__c field to be mapped to opportunity also...how it can be possible..how can i write a trigger for this scenario????

Vinit_KumarVinit_Kumar

I am still not clear about your requirement.Could you please be more specific.

Leena ReddyLeena Reddy

Hi vinit.Actually  i have a custom field in lead,account,opportunity as AGE__c.now i need that age field value should be populated in acc,con,opp when ever the lead is converted and updated.

Vinit_KumarVinit_Kumar

You need to create an update Trigger on Lead and need to use ConvertAccountId and ConvertedOpportunityId which would be generated in Lead conversion.Please go through the below link to know more about these fields :-

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_lead.htm

chowdary marellachowdary marella

 

Can it be possible.

 

trigger LeadConvert on Lead (after insert,after update) {
//Bulkified
List<String> LeadNames = new List<String>{};
contact c=new contact();
Account ac=new Account();
opportunity op=new opportunity();
for(Lead myLead: Trigger.new){
if(myLead.isconverted==false) {
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(myLead.Id);
lc.convertedStatus = 'Closed - Converted';

c.Roll_No__c=myLead.Roll_No__c;
ac.Roll_No__c=myLead.Roll_No__c;
op.roll_no__c= myLead.Roll_No__c;
//Database.ConvertLead(lc,true);
//lc.setDoNotCreateOpportunity(true);
Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());

}
}
}

Vinit_KumarVinit_Kumar

This seems fine to me other than the below line :-

 

lc.convertedStatus = 'Closed - Converted';

 

I won't recommend you to hard code the value rather than you should query as follow :-

 

LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);

 

so that in future if you change the ConvertedStatus,it won't break :)

vishal@forcevishal@force

Hello,

 

Can't you use the standard field mapping on lead convert?

 

Lead Custom Field Mapping
 
chowdary marellachowdary marella

then How can we map multiple fields ?? trigger needs to play 

vishal@forcevishal@force

It's only one field getting mapped to all the objects.

vishal@forcevishal@force

Ok, my bad. I didn't understand what you wanted to say.

 

We can have one - to - one field mapping and here it is one - to -three. So trigger will have to come into picture.

 

Thanks for clarifying it :)

chowdary marellachowdary marella

I tried this,but not mapping..sumthng went wrong..make changes to this

 

trigger LeadConvert on Lead (after insert,after update) {
//Bulkified
List<String> LeadNames = new List<String>{};
contact c=new contact();
Account ac=new Account();
opportunity op=new opportunity();

for(Lead myLead: Trigger.new){
if(myLead.isconverted==false) {
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(myLead.Id);
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);

c.Roll_No__c=myLead.Roll_No__c;
ac.Roll_No__c=myLead.Roll_No__c;
op.roll_no__c= myLead.Roll_No__c;

list<account> acc =[select id,Roll_No__c from account where id in:trigger.new.getaccountid()];
system.debug('@@@@@@acc'+acc);

//Database.ConvertLead(lc,true);
//lc.setDoNotCreateOpportunity(true);
Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());

}
}
}

Vinit_KumarVinit_Kumar

Hi,

 

Try below :-

 

trigger LeadConvert on Lead (after update) {

List<Account> accList = new List<Account>();
List<Contact> conList = new List<Contact>();
List<Opportunity> oppList = new List<Opportunity>();

//Bulkified
for (Lead lead : Trigger.new) {
if (lead.isConverted == false) //to prevent recursion
{

Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(lead.Id);

LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);


Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());


Account acc = new Account(id=lcr.AccountId,Roll_No__c=lead.Roll_No__c);
accList.add(acc);

Contact con = new Contact(id=lcr.ContactId,Roll_No__c=lead.Roll_No__c);
conList.add(con);

Opportunity opp = new Opportunity(id=lcr.OpportunityId,Roll_No__c=lead.Roll_No__c);
oppList.add(opp);

}
}

update accList;
update conList
update oppList;
}