• Edgar Macachor
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 6
    Replies
Hi help guys!!! Can you check my code for lead coversion to account, contact and order(a custom object), it is converted once it is qualified but the problem is after the lead conversion it created two order record. My code is below:
trigger TriggerOnConvertLeads on Lead (after insert, after update) {
    map<Id, Lead> mapNewLead = trigger.newMap; 
    List<Order__c> recordsToInsert = new List<Order__c>();
    List<lead> leadList = trigger.new;
    Set<ID> leadIds= new Set<ID>();
    for(lead leadRecord : leadList) {
        if (leadRecord.Status == 'Qualified' && leadRecord.isConverted == false){
             
            Order__c  odr = new Order__c();
            // populate new record fields here
            recordsToInsert.add(odr);
            
             leadIds.add(leadRecord.Id);
        }
    //system.debug('size'+ leadIds.size());
    if(leadIds.size()  > 0){
              ConvertLeads.LeadAssign(leadIds);
        insert recordsToInsert;
    	}
	}
}
 
Public class ConvertLeads
{
   
    public static void LeadAssign(Set<Id> LeadIds)
    {
       LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus 
       WHERE  IsConverted=true Limit 1];
      
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead : LeadIds){
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId ( currentlead );                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                Leadconvert.setDoNotCreateOpportunity(TRUE); 
                //you can remove this line if you want to create an opportunity during  conversion 
                MassLeadconvert.add(Leadconvert);
        }
        
        if (!MassLeadconvert.isEmpty()) {
     List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
     List<Order__c> recordsToInsert = new List<Order__c>();
		for(Database.LeadConvertResult objlcr :lcr ){
     		if(objlcr.isSuccess()){
          		Order__c odr = new Order__c();
          		// populate new record fields here
          		odr.Contact__c = objlcr.getContactId();
          		odr.Account__c = objlcr.getAccountId();
           		recordsToInsert.add(odr);
     }
}

if(recordsToInsert.size() > 0)
      insert recordsToInsert;
        }}}
HI guys help!! I have to create a validation rule that the Contact Lookup should be under the same Account and now I have created a formula but I think it doesn't work right.
So here's my code:
AND( CreatedBy.Contact.AccountId = Contact__c,
     Account__r.Id  = Account__c,
     Account__r.Id <> CreatedBy.Contact.AccountId)


Can you help me find what is wrong about my formula.
Hi help guys!!! Can you check my code for lead coversion to account, contact and order(a custom object), it is converted once it is qualified but the problem is after the lead conversion it created two order record. My code is below:
trigger TriggerOnConvertLeads on Lead (after insert, after update) {
    map<Id, Lead> mapNewLead = trigger.newMap; 
    List<Order__c> recordsToInsert = new List<Order__c>();
    List<lead> leadList = trigger.new;
    Set<ID> leadIds= new Set<ID>();
    for(lead leadRecord : leadList) {
        if (leadRecord.Status == 'Qualified' && leadRecord.isConverted == false){
             
            Order__c  odr = new Order__c();
            // populate new record fields here
            recordsToInsert.add(odr);
            
             leadIds.add(leadRecord.Id);
        }
    //system.debug('size'+ leadIds.size());
    if(leadIds.size()  > 0){
              ConvertLeads.LeadAssign(leadIds);
        insert recordsToInsert;
    	}
	}
}
 
Public class ConvertLeads
{
   
    public static void LeadAssign(Set<Id> LeadIds)
    {
       LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus 
       WHERE  IsConverted=true Limit 1];
      
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead : LeadIds){
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId ( currentlead );                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                Leadconvert.setDoNotCreateOpportunity(TRUE); 
                //you can remove this line if you want to create an opportunity during  conversion 
                MassLeadconvert.add(Leadconvert);
        }
        
        if (!MassLeadconvert.isEmpty()) {
     List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
     List<Order__c> recordsToInsert = new List<Order__c>();
		for(Database.LeadConvertResult objlcr :lcr ){
     		if(objlcr.isSuccess()){
          		Order__c odr = new Order__c();
          		// populate new record fields here
          		odr.Contact__c = objlcr.getContactId();
          		odr.Account__c = objlcr.getAccountId();
           		recordsToInsert.add(odr);
     }
}

if(recordsToInsert.size() > 0)
      insert recordsToInsert;
        }}}
HI guys help!! I have to create a validation rule that the Contact Lookup should be under the same Account and now I have created a formula but I think it doesn't work right.
So here's my code:
AND( CreatedBy.Contact.AccountId = Contact__c,
     Account__r.Id  = Account__c,
     Account__r.Id <> CreatedBy.Contact.AccountId)


Can you help me find what is wrong about my formula.