You need to sign in to do that
Don't have an account?
bobby7
I have a requirment where i am inserting a lead with status equals 'Full submission'. Now When the lead is created i want to insert a Account and also a Opportunity With some field mappings Using Trigger in both Isert and Update conditions.
Here is my code to map the fields and insert a Account and also opportunity whenever the status is Full Submission on lead. The lead should be automatically converted .
Here is my code .
trigger TriggerToConvertLEadAndCreateActAndOppo on Lead (after insert, after update) {
list<Account> lstAccountUpdate = new list<Account>();
list<Opportunity> lstOppUpdate = new list<Opportunity>();
set<id> Accountids = new set<id>();
if( trigger.isInsert){
for(Lead Objlead : trigger.new){
Account objacc;
Opportunity opp;
Database.LeadConvertResult lcr;
list<LeadStatus> lstconvertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true limit 1];
if((Objlead.isConverted == false)&& (Objlead.Status == 'Full Submission'))
{
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(Objlead.Id);
//lc.setConvertedStatus('Converted');
string strstatus='';
if(lstconvertStatus != null && lstconvertStatus.size()>0 ){
strstatus = lstconvertStatus[0].MasterLabel ;
}
lc.setConvertedStatus(strstatus);
lcr = Database.convertLead(lc);
Accountids.add(lcr.getAccountId());
system.debug(':::::strstatus'+strstatus);
system.debug(':::::lc'+lc);
system.debug(':::::Accountids'+Accountids);
}
system.debug(':::::::::::Objlead.isConverted'+Objlead.isConverted);
if((Objlead.isConverted)){
objacc = new Account(id=lcr.getAccountId(),
Phone = Objlead.MobilePhone,
Name = Objlead.Company);
}
lstAccountUpdate.add(objacc);
system.debug(':::::lstAccountUpdate'+lstAccountUpdate);
}
if(lstAccountUpdate != null && lstAccountUpdate.size()>0)
update lstAccountUpdate;
}
if(trigger.isUpdate ){
for(Lead objL:trigger.new){
Account objacc;
if(objL.Status == 'Full Submission' && trigger.oldMap.get(objL.id).Status != trigger.newmap.get(objL.id).Status && trigger.oldmap.get(ObjL.Id).Status != 'Full Submission'){
Database.LeadConvert lc = new Database.Leadconvert();
lc.setLeadId(objL.id);
lc.setConvertedStatus('Converted');
if((objL.isConverted && objL.ConvertedAccountId != null)){
objacc = new Account(id=objL.ConvertedAccountId,
Phone = objL.MobilePhone,
Name = objL.Name);
}
lstAccountUpdate.add(objacc);
}
}
if(lstAccountUpdate != null && lstAccountUpdate.size()>0)
update lstAccountUpdate;
}
}
I am not entering into this snippet Where i am mapping some fields from lead to account. Here is the snippet.
system.debug(':::::::::::Objlead.isConverted'+Objlead.isConverted);
if((Objlead.isConverted)){
objacc = new Account(id=lcr.getAccountId(),
Phone = Objlead.MobilePhone,
Name = Objlead.Company);
}
The above debug is always showing me as false. But i am converting the lead above this. Can you get me some working code.
Here is my code .
trigger TriggerToConvertLEadAndCreateActAndOppo on Lead (after insert, after update) {
list<Account> lstAccountUpdate = new list<Account>();
list<Opportunity> lstOppUpdate = new list<Opportunity>();
set<id> Accountids = new set<id>();
if( trigger.isInsert){
for(Lead Objlead : trigger.new){
Account objacc;
Opportunity opp;
Database.LeadConvertResult lcr;
list<LeadStatus> lstconvertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true limit 1];
if((Objlead.isConverted == false)&& (Objlead.Status == 'Full Submission'))
{
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(Objlead.Id);
//lc.setConvertedStatus('Converted');
string strstatus='';
if(lstconvertStatus != null && lstconvertStatus.size()>0 ){
strstatus = lstconvertStatus[0].MasterLabel ;
}
lc.setConvertedStatus(strstatus);
lcr = Database.convertLead(lc);
Accountids.add(lcr.getAccountId());
system.debug(':::::strstatus'+strstatus);
system.debug(':::::lc'+lc);
system.debug(':::::Accountids'+Accountids);
}
system.debug(':::::::::::Objlead.isConverted'+Objlead.isConverted);
if((Objlead.isConverted)){
objacc = new Account(id=lcr.getAccountId(),
Phone = Objlead.MobilePhone,
Name = Objlead.Company);
}
lstAccountUpdate.add(objacc);
system.debug(':::::lstAccountUpdate'+lstAccountUpdate);
}
if(lstAccountUpdate != null && lstAccountUpdate.size()>0)
update lstAccountUpdate;
}
if(trigger.isUpdate ){
for(Lead objL:trigger.new){
Account objacc;
if(objL.Status == 'Full Submission' && trigger.oldMap.get(objL.id).Status != trigger.newmap.get(objL.id).Status && trigger.oldmap.get(ObjL.Id).Status != 'Full Submission'){
Database.LeadConvert lc = new Database.Leadconvert();
lc.setLeadId(objL.id);
lc.setConvertedStatus('Converted');
if((objL.isConverted && objL.ConvertedAccountId != null)){
objacc = new Account(id=objL.ConvertedAccountId,
Phone = objL.MobilePhone,
Name = objL.Name);
}
lstAccountUpdate.add(objacc);
}
}
if(lstAccountUpdate != null && lstAccountUpdate.size()>0)
update lstAccountUpdate;
}
}
I am not entering into this snippet Where i am mapping some fields from lead to account. Here is the snippet.
system.debug(':::::::::::Objlead.isConverted'+Objlead.isConverted);
if((Objlead.isConverted)){
objacc = new Account(id=lcr.getAccountId(),
Phone = Objlead.MobilePhone,
Name = Objlead.Company);
}
The above debug is always showing me as false. But i am converting the lead above this. Can you get me some working code.
I've refined your trigger code....Your code was not bulkified and there were a lot of redundant statements...
Hopefully it should work, let me know if you have any problems..