You need to sign in to do that
Don't have an account?
Manjunath SC 18
trigger to auto convert lead into account and contact
Hello Guys
i am new to the trigges and apex development, so here is my requirement - There is checkbox Called "Private__c" in account object
Once locked, if a new Lead is created with this Private Locked Account which already has a Allocated Owner, then a new Contact should be created for this Account under the allocated Account owner name. Lead will not be created.
And If a new lead is added without a Private Account checkbox being checked then allow the Lead to be created with Lead creator as the Lead owner.
so far i have tried to design a trigger, but its not meeting up my goal
i am new to the trigges and apex development, so here is my requirement - There is checkbox Called "Private__c" in account object
Once locked, if a new Lead is created with this Private Locked Account which already has a Allocated Owner, then a new Contact should be created for this Account under the allocated Account owner name. Lead will not be created.
And If a new lead is added without a Private Account checkbox being checked then allow the Lead to be created with Lead creator as the Lead owner.
so far i have tried to design a trigger, but its not meeting up my goal
trigger AutoConvertLeadAccount on Lead (after insert) { Set<String> leademails = new Set<String>(); List<Lead> newLeads = new List<Lead>(); // Get all the new leads for(Lead l : system.trigger.new){ newLeads.add(l); leademails.add(l.email); } /* Make some maps of account and email addresses */ List<Account> AccountList = [select Id, Website, OwnerId from Account where Website IN: leademails]; Map<ID, String> peAccounts = new Map<ID, String>(); Map<ID, ID> peAccountsOwner = new Map<ID, ID>(); if(AccountList.size()>0){ // Generic map for preventing loss of ids for(Account a : AccountList){ peAccounts.put(a.id, a.Website); peAccountsOwner.put(a.id, a.OwnerId); } // We will need this to get the id from the email address Map<String, ID> peAccountsFlipped = new Map<String, ID>(); for(ID i : peAccounts.keyset()){ peAccountsFlipped.put(peAccounts.get(i), i); } /* System Conversion Requirements */ leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1]; List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>(); Database.LeadConvert lc = new Database.LeadConvert(); /* Configuring Payload */ for (Lead nl : newleads) { lc = new Database.LeadConvert(); lc.setLeadId(nl.id); lc.setOverwriteLeadSource(false); lc.setConvertedStatus(convertStatus.MasterLabel); // Check to see if account already exists if(!peAccounts.isEmpty()){ if(peAccountsFlipped.get(nl.email)!=null){ lc.setAccountId(peAccountsFlipped.get(nl.email)); lc.setOwnerId(peAccountsOwner.get(peAccountsFlipped.get(nl.email))); lc.setDoNotCreateOpportunity(true); } } else { // In the event an account doesn't exist lc.setOwnerId(nl.OwnerId); lc.setDoNotCreateOpportunity(false); lc.setOpportunityName(nl.Name); } leadConverts.add(lc); } // Fire Payload Database.LeadConvertResult[] lcr = Database.convertLead(leadConverts); System.debug(LoggingLevel.INFO, lcr); } }
try below code.
Trigger:
Test Class:
Mark it best if it solves your problem.