0

There is a standard field in the Lead called Lead Status where I need to set it to 'closed' if there is account with the same company name and website. So I am checking programatically the values in the company and website fields and if there is account with the same name the one in the lead in the company or the same website I am trying to set the status of the lead to closed.

Example: lead company = A and website = B >>> account name = A and website = B >>> change lead status to closed.

or

lead company = A; lead website = B;

If(company.equals(account.name)) || (website.equals(account.website)){ lead.leadStatus = 'closed'; }

Could you please advise how to achieve that in Salesforce.

1
  • Do you want the lead status to change as soon as a Lead is created and has a similar account? Or you want to perform some function on lead first and then check if it has a similar account and close it ?
    – Ruchi
    Aug 12, 2015 at 9:28

2 Answers 2

0

If you do not want to close the Lead as soon as it is created then you can write a batch class similar to below to obtain your mentioned functionality.

Global Class LeadStatusUpdate Implements Database.Batchable<sObject>
{
Private String Query = 'Select Id,Company,Website from Lead where Status != Closed ';

//Start Method which retrieves the records of Lead
Global Database.QueryLocator Start(Database.BatchableContext BC)  
{
    Return Database.GetQueryLocator(query);
}


 Global Void Execute(Database.BatchableContext BC, List<sObject> scope) 
{
    List<Lead> UpdateLead = (List<Lead>)scope;

    Set<String> CompanyNames = new Set<String>(); // Set of "Company" Names
    Set<String> WebsiteNames = new Set<String>(); // Set of "Website" Names

    for(Lead getLeadrecords : UpdateLead) 
    { 
        CompanyNames.add(getLeadrecords.Company);  //Adding Price "Company" Names to the set 
        WebsiteNames.add(getLeadrecords.Website);  //Adding the "Website" Names to the set 
    }

    List<Account> Accounts = [Select Id,Company__c,Website from Account where Company__c =: CompanyNames and Website =: WebsiteNames];

    List<Lead> LeadsToUpdate = new List<Lead>();
    for(Lead L : UpdateLead)
    {
        for(Account A : Accounts)
        {
            if((L.Company == A.Company__c) && (L.Website == A.Website))
            {
                L.Status = 'Closed';
                LeadsToUpdate.add(L);
            }
        }
    }
    Update LeadsToUpdate;
}

Global Void Finish(Database.BatchableContext BC)
{
}}

Same can also be done with trigger.

Hope this helps!

Thanks!

1
  • n.b. Global access modifier is not required for batch methods unless building managed packages; public will suffice
    – cropredy
    Aug 12, 2015 at 16:48
0
trigger testTrigger on Account (before insert, before update) {

    List<String> company = new List<String>();
    List<String> website = new List<String>();
    for(Lead com : trigger.new){
        company.add(com.Company);
        website.add(com.Website);
    }
    List<Account> accList = [SELECT ID,Name,Name,Website from Account Where Name IN : company OR Website IN : website];

    for(Lead com : trigger.new){
        for(Account acc : accList){
            if(com.Company.equals(acc.Name) && com.Website.equals(acc.Website)){
                com.Status = 'Closed - Converted';
            }
        }
    }
}

Check is there any way you can optimize last for loop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .