• Sudeep Singh
  • NEWBIE
  • 479 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 89
    Questions
  • 201
    Replies

Can we Implement this scenario :- 

An Custom object will be created where Name, Email, DOB, Address and Mobile number will be filled. When It will save Username and Password will be generated and displayed in that same record page and Username, Password be mailed to the given email address.

Using LWC a site will be developed which will open to scan a QR code. To open that page user needs to put the username and password that is sent through email. He/She will scan the QR code and after scanning in another custom object Scanning details will be stored like Time, Date, Scanned by - Username/Name, Location.

 

Is this scenario is possible, if yes then what all steps needs to be followed up.

Batch Class:- 

 

/* Class Type :- Batch
* Date :- 
* Description :- Creates a new order if Contract Status is Activated after one month and update Next Order Date
* Author :- 
*/ 
global class AutoSubscriptionForOrderCreation implements Database.Batchable<sObject>,schedulable,database.stateful {
    Public string errmsg = '';
    Public integer OrderCount = 0;
    Public void execute(SchedulableContext sc) 
    { 
        database.executebatch(new AutoSubscriptionForOrderCreation());
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
        Date orderCreationDate = system.today().addDays(3);
        //system.debug('output'+[SELECT Id,Status,Next_Order_Date__c,Service__r.id,AccountId,ShippingAddress,BillingAddress,Insurance__c From Contract Where Status = 'Activated' And Next_Order_Date__c = TODAY]);
        if(Test.isRunningTest()){
            return Database.getQueryLocator([SELECT Id,Status,Next_Order_Date__c,Service__r.id,AccountId,ShippingAddress,BillingAddress,Insurance__c,Group_ID__c,Policy_ID__c,Insurance_Provider__c,BillingState,BillingCity,BillingStreet,BillingCountry,BillingPostalCode,BillingLatitude,BillingLongitude,ShippingStreet,ShippingState,ShippingCity,ShippingCountry,ShippingPostalCode,ShippingLatitude,ShippingLongitude,CustomerSignedId From Contract]);
        }else{
            //System.debug('This is executing');
           return Database.getQueryLocator([SELECT Id,Status,Next_Order_Date__c,Service__r.id,AccountId,ShippingAddress,BillingAddress,Insurance__c,Group_ID__c,Policy_ID__c,Insurance_Provider__c,BillingState,BillingCity,BillingStreet,BillingCountry,BillingPostalCode,BillingLatitude,BillingLongitude,ShippingStreet,ShippingState,ShippingCity,ShippingCountry,ShippingPostalCode,ShippingLatitude,ShippingLongitude,CustomerSignedId From Contract Where Status = 'Activated' And Next_Order_Date__c =: orderCreationDate]); 
        }
    }
    
    global void execute(Database.BatchableContext bc, List<Contract> contractlst) {
        try{
            
        
        List<Order> orderlsttoinsert = new List<Order>();
        List<OrderItem> orderItemsToInsert = new List<OrderItem>();
        List<Contract> contractupdatelst = new List<Contract>();
        List<Order> ordertoupdate= new List<Order>();
        map<Id, Id> contractProdMap = New map<Id, Id>();
        if(contractlst.size() > 0){
            
            Pricebook2 priceBook = [select id, name from Pricebook2 where isStandard = true limit 1];
            List<PriceBookEntry> priceBookEntryList = [SELECT Product2.Id, Product2.Name, UnitPrice FROM PriceBookEntry WHERE Pricebook2Id =:priceBook.id];            
            //System.debug('priceBookEntryList'+priceBookEntryList);
            //Set <Id> accid  = new set <Id>();
            //List
            for(Contract con: contractlst){
                //Insert Orders
                Order orderObj = new Order();
                orderObj.AccountId = con.AccountId;
                orderObj.Status = 'Draft';
                orderObj.ContractId = con.Id;
                orderObj.EffectiveDate = System.today();
                orderObj.Pricebook2Id =  priceBook.id;
                orderObj.Insurance__c =  con.Insurance__c;
                orderObj.Insurance_Group_Number__c = con.Group_ID__c;
                orderObj.Insurance_ID__c = con.Policy_ID__c;
                orderObj.Insurance_Name__c = con.Insurance_Provider__c;
                orderObj.Type = 'Subscription Order';
                orderObj.BillingStreet = con.BillingStreet;
                orderObj.BillingState = con.BillingState;
                orderObj.BillingCity = con.BillingCity;
                orderObj.BillingCountry = con.BillingCountry;
                orderObj.BillingPostalCode = con.BillingPostalCode;
                orderObj.BillingLatitude = con.BillingLatitude;
                orderObj.BillingLongitude = con.BillingLongitude;
                orderObj.ShippingStreet = con.ShippingStreet;
                orderObj.ShippingState = con.ShippingState;
                orderObj.ShippingCity = con.ShippingCity;
                orderObj.ShippingCountry = con.ShippingCountry;
                orderObj.ShippingPostalCode = con.ShippingPostalCode;
                orderObj.ShippingLatitude = con.ShippingLatitude;
                orderObj.ShippingLongitude = con.ShippingLongitude;
                //orderObj.ShipToContactId = con.account.PersonContactId;
                //orderObj.BillToContactId = con.account.PersonContactId;
                orderObj.EffectiveDate = con.Next_Order_Date__c;
                orderlsttoinsert.add(orderObj);
                
                //Create Contract and Product Map
                //System.debug('');
                for(PriceBookEntry pBookEntry : priceBookEntryList){
                    if(pBookEntry.Product2.Id == con.Service__r.Id){
                        contractProdMap.put(con.Id, pBookEntry.Id);
                        //System.debug('running');
                    }
                }
                
                // Update order date to next month's nearest Wednesday
                Date nextOrderDate = ((System.today().addMonths(1)).toStartOfWeek().addDays(3));
                con.Next_Order_Date__c = nextOrderDate;
                contractupdatelst.add(con);
                
            }
            if(orderlsttoinsert.size() > 0){
                insert orderlsttoinsert; 
                OrderCount = OrderCount+orderlsttoinsert.size();
                           
                for(Order O: orderlsttoinsert){
                    //System.debug('contractProdMap.get(O.ContractId)'+contractProdMap.get(O.ContractId));
                    //Insert Order Items
                    OrderItem oi = new OrderItem();
                    oi.OrderId = O.id;
                    oi.Quantity = 1;
                    oi.UnitPrice = 1;
                    oi.Product2id = O.Contract.Service__c;
                    oi.PricebookEntryId = contractProdMap.get(O.ContractId);
                    oi.sstation__Currency_Type__c = 'USD';
                    orderItemsToInsert.add(oi);
                }    
            }
            if(orderItemsToInsert.size() > 0){
                insert orderItemsToInsert;
            }
            if(contractupdatelst.size() > 0){
                update contractupdatelst;
            }
            if(orderlsttoinsert.size() > 0){
            for(Order Order: orderlsttoinsert){
                Order.Status='Order Placed';
                ordertoupdate.add(Order);
            } 
            update ordertoupdate; 
                
        }   
        }
        }catch (exception e){
            errmsg = e.getMessage();
            //System.debug('errmsg'+errmsg);
            Batch_Message__c batch = new Batch_Message__c();
            batch.Batch_Status__c = 'Failed';
            batch.Failed_Description__c = errmsg;
            insert batch;
        }
    }    
    global void finish(Database.BatchableContext bc) {
        if (errmsg == ''){
            Batch_Message__c batch = new Batch_Message__c();
            batch.Batch_Status__c = 'Success';  
            batch.Order_Count__c = OrderCount;
            insert batch;
       
        }        
    }
}

 

Test class:- 

@isTest
public class Test_AutoSubscriptionForOrderCreation {
    @isTest
    static void testMethod1()
            {
        String recordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
        Account acc= new Account(
        RecordTypeID=recordTypeId ,
        FirstName='Test FName',
        LastName='Test LName',
        PersonMailingStreet='test@yahoo.com',
        PersonMailingPostalCode='12345',
        PersonMailingCity='SFO',
        PersonEmail='test@yahoo.com',
        PersonHomePhone='1234567',
        PersonMobilePhone='12345678'
        );
        insert acc;
                
        Contact con = new Contact();
        //con.AccountId = acc.Id;
        con.LastName = 'Test';
        con.Birthdate = system.today();
        insert con;
                
        Product2 op = new Product2();
        op.Name = 'Test';
        op.IsActive = true;
        op.ProductCode = 'Test';
        insert op;
                
        Pricebook2 standardPricebook = new Pricebook2(
        Id = Test.getStandardPricebookId(),
        IsActive = true
        );
   
        update standardPricebook;   
        PricebookEntry standardPrice = new PricebookEntry();
        standardPrice.Pricebook2Id = standardPricebook.id;
        standardPrice.Product2Id = op.Id;
        standardPrice.UnitPrice = 100;
        standardPrice.IsActive = true;
        standardPrice.UseStandardPrice = false;
        insert standardPrice;
                
        Contract cont = new Contract();
        cont.AccountId= acc.id;
        cont.StartDate=date.today();
        cont.ContractTerm=2;
        cont.Status='Draft';
        cont.Next_Order_Date__c = Date.today();
        cont.Pricebook2Id = standardPricebook.Id;
        cont.Service__c = op.Id;
        //cont.Insurance__c = mp.Id;
        cont.BillingStreet = 'test';
        cont.BillingPostalCode = '759128';
        cont.BillingState = 'test';
        cont.BillingCity = 'test';
        cont.BillingCountry = 'test';
        cont.BillingLatitude = 20.296059;
        cont.BillingLongitude = 85.824539;
        cont.ShippingCity = 'test';
        cont.ShippingCountry = 'test';
        cont.ShippingPostalCode = '777777';
        cont.ShippingLatitude = 20.296059;
        cont.ShippingLongitude = 85.824539;
        cont.Insurance__c = 'Medicaid';
        cont.Insurance_Provider__c = 'Medicaid';
        cont.Group_ID__c = '1234GID';
        cont.Policy_ID__c = 'PID1234';
        cont.Policy_Holder_First_Name__c = 'Test';
        cont.Policy_Holder_Last_Name__c = 'Test';
        //cont.Service__c = op.Id;
        insert cont;
                
        Order o = new Order();
        o.AccountId = acc.Id;
        o.ContractId = cont.Id;
        o.Status = 'Draft';
        o.EffectiveDate = system.today();
        o.BillToContactId = con.Id;
        o.ShipToContactId = con.Id;
        o.Insurance__c = cont.Insurance__c;
        o.Insurance_Name__c = cont.Insurance_Provider__c;
        o.Insurance_ID__c = cont.Policy_ID__c;
        o.Insurance_Group_Number__c = cont.Group_ID__c;
        insert o;
                
        OrderItem oi = new OrderItem();
        oi.sstation__Currency_Type__c = 'USD';
        oi.OrderId = o.Id;
        oi.Quantity = 20.4;
        oi.UnitPrice = 100;
        oi.PricebookEntryId = standardPrice.Id;
        insert oi;
                
        Contract contr = new Contract();
        contr.Status = 'Activated';
        contr.Id = cont.Id;
        contr.Next_Order_Date__c = date.today()+3;
        update contr;
                
                {
            Order od = new Order(
                AccountId = acc.Id,
                ContractId = cont.Id,
                EffectiveDate = System.today(),
                Status = 'Draft',
                BillToContactId = con.Id,
                ShipToContactId = con.Id,
                PriceBook2Id = Test.getStandardPricebookId()  
            );
            System.assert(True, 'ErrorMessage');
            
             }
                
        Test.startTest();
        AutoSubscriptionForOrderCreation obj = new AutoSubscriptionForOrderCreation();
        String CRON_EXP = '0 0 0 3 9 ? 2042';
        system.schedule('Test status Check9', CRON_EXP, obj );
        Test.stopTest();
 
}
}

When am removing this two line coverage is 91% but when i am including this two line thne coverage is 60%
orderObj.ShipToContactId = con.account.PersonContactId;
orderObj.BillToContactId = con.account.PersonContactId;

Hi,

I had written a class which will convert lead to Contract in between it will create Account and Address.
I dont want to create Contact but it is getting created.
I filled data with lead first and Last name. 
Company field in lead is mandatory which is not in use for my requirement.

Here is the code I tried : 

public class AutoConvertLead {
@InvocableMethod
public static void assignLeads(List<Id> LeadIds) { 
    try{ 
        Map<String,Date> NameDOBMap = new Map<String,Date>();
        List<String> leadNames = new List<String>();
        List<Date> leadDOBs = new List<Date>();
        List<Database.LeadConvert> massLeadConvert = new List<Database.LeadConvert>();
        List<Account> accountsToUpdate = new List<Account>();
        List<Account> accountsToInsert = new List<Account>();
        List<Contract> contractsToInsert = new List<Contract>();
        List<Address__c> addressesToInsert = new List<Address__c>();
        //List<MemberPlan> memberplanToInsert = new List<MemberPlan>();
        Map<String,Account> nameDOBToAccountMap = new Map<String,Account>();
        Map<String,Account> newCreatedAccounts = new Map<String,Account>();
        Map<ID,Id> leadProductMap = new Map<Id,Id>();
        Map<ID,Id> leadHealthcareFacilityMap = new Map<ID, Id>();
        
        //Id personAccountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
        LeadStatus cLeadStatus = [SELECT Id,MasterLabel FROM LeadStatus WHERE isConverted = true LIMIT 1];  
        
       // List<Lead> leads = [SELECT Id,Name,LastName,FirstName,Email,Birthdate__c,Service__c,Ethnicity__c, Race__c,Gender__c FROM Lead WHERE Id IN :LeadIds];
       Map<Id,Lead> leadmap= new Map<Id,Lead>([SELECT Id,Name,LastName,FirstName,Email,Signature__c,Signature_Date_Time__c,Birthdate__c,Service__c,Ethnicity__c,MobilePhone,Phone,Race__c,Gender__c,City,Country,State,Street,PostalCode,Address_Type__c,Group_ID__c,Insurance_Provider__c,Is_Policy_Holder__c,Policy_Holder_First_Name__c,Policy_Holder_Last_Name__c,Policy_ID__c,Insurance__c FROM Lead WHERE Id IN :LeadIds]);
       // for(Lead lead: leads) {
        for(Lead lead: leadmap.values()) {
            leadNames.add(String.valueOf(lead.Name));
            leadDOBs.add(lead.Birthdate__c);
            NameDOBMap.put(lead.Name, lead.Birthdate__c);
            leadProductMap.put(lead.Id,lead.Service__c);
            //leadHealthcareFacilityMap.put(lead.Id,lead.Depot_Name__c);
        }
        
        Map<Id, Account> existingAccounts = new Map<Id, Account>([SELECT Id, Birthdate__c, Name, Gender__c FROM Account WHERE Name IN :NameDOBMap.KeySet() AND Birthdate__c IN :NameDOBMap.values()]);
        
        for(account acc: existingAccounts.values()){
            
        }
        System.debug('Check 1'+existingAccounts);
        for(Account accc: existingAccounts.values()){
            nameDOBToAccountMap.put(accc.Name+accc.Birthdate__c, accc);
        }
        
       // for(Lead ld : leads){
        for(Lead ld: leadmap.values()) {
            if(!nameDOBToAccountMap.containsKey(ld.Name+ld.Birthdate__c)){
                Account existingAccount = new Account();
                existingAccount.Name = ld.Name;
                existingAccount.First_Name__c = ld.LastName;
                existingAccount.First_Name__c = ld.FirstName;
                existingAccount.Email__c = ld.Email;
                existingAccount.Gender__c = ld.Gender__c;
                //existingAccount.RecordTypeId =  personAccountRecordTypeId;
                existingAccount.Ethnicity__c = ld.Ethnicity__c;
                existingAccount.Race__c = ld.Race__c;
                existingAccount.Gender__c = ld.Gender__c;
                existingAccount.Birthdate__c = ld.Birthdate__c;
                existingAccount.Mobile__c = ld.MobilePhone;
                existingAccount.Mobile__c = ld.Phone;
                existingAccount.Insurance_Options__c = ld.Insurance__c;
                existingAccount.Insurance_Provider__c = ld.Insurance_Provider__c;
                existingAccount.Policy_Holder_First_Name__c = ld.Policy_Holder_First_Name__c;
                existingAccount.Policy_Holder_Last_Name__c = ld.Policy_Holder_Last_Name__c;
                existingAccount.Group_Number__c = ld.Group_ID__c;
                existingAccount.Member_ID__c = ld.Policy_ID__c;
                existingAccount.Is_Policy_Holder__c = ld.Is_Policy_Holder__c;
                //existingAccount.Address_Line_2__c = ld.Address_Line_2__c;
                accountsToInsert.add(existingAccount);
            }  
        }
        
        if(accountsToInsert.size()>0){
            insert accountsToInsert;
        }
        system.debug('accountsToInsert'+accountsToInsert);
        
        for(account acc: accountsToInsert){
            newCreatedAccounts.put(acc.First_Name__c+acc.Last_Name__c, acc);
        }
        system.debug('newCreatedAccounts'+newCreatedAccounts);
        //for(Lead lead : leads){
            for(Lead lead : leadmap.values()){
            System.debug('Check 2');
            Database.LeadConvert LeadConvert = new Database.LeadConvert();
            LeadConvert.setLeadId(lead.Id);
            LeadConvert.setConvertedStatus(cLeadStatus.MasterLabel);
            LeadConvert.setDoNotCreateOpportunity(True);  
            
            if(lead.Name != null && lead.Birthdate__c != null){                    
                if(nameDOBToAccountMap.containsKey(lead.Name+lead.Birthdate__c) && nameDOBToAccountMap.get(lead.Name+lead.Birthdate__c)!=Null){
                    system.debug('existingAccount==>'+nameDOBToAccountMap.get(lead.Name+lead.Birthdate__c));
                    Account existingAccount = new Account();
                    existingAccount.Id = nameDOBToAccountMap.get(lead.Name+lead.Birthdate__c).id;
                    system.debug('existingAccount'+existingAccount);
                    existingAccount.Ethnicity__c = lead.Ethnicity__c;
                    existingAccount.Race__c = lead.Race__c;
                    existingAccount.Gender__c = lead.Gender__c;
                    existingAccount.Mobile__c = lead.MobilePhone;
                    existingAccount.Email__c = lead.Email;
                    existingAccount.Mobile__c = lead.Phone;
                    existingAccount.Mobile__c = lead.MobilePhone;
                    //existingAccount.Address_Line_2__c = lead.Address_Line_2__c;
                    existingAccount.BillingCountry = lead.Country;
                    existingAccount.BillingPostalCode = lead.PostalCode;
                    existingAccount.BillingState = lead.State;
                    existingAccount.BillingStreet = lead.Street;
                    existingAccount.BillingCity = lead.City;
                    existingAccount.Insurance_Options__c = lead.Insurance__c;
                    existingAccount.Insurance_Provider__c = lead.Insurance_Provider__c;
                    existingAccount.Policy_Holder_First_Name__c = lead.Policy_Holder_First_Name__c;
                    existingAccount.Policy_Holder_Last_Name__c = lead.Policy_Holder_Last_Name__c;
                    existingAccount.Group_Number__c = lead.Group_ID__c;
                    existingAccount.Member_ID__c = lead.Policy_ID__c;
                    existingAccount.Is_Policy_Holder__c = lead.Is_Policy_Holder__c;
                    accountsToUpdate.add(existingAccount);
                    LeadConvert.setAccountId(existingAccount.Id);
                }
                else{
                    system.debug('lead.Name+lead.Birthdate__c'+lead.FirstName+lead.LastName);
                    if(newCreatedAccounts.containskey(lead.FirstName+lead.LastName) && newCreatedAccounts.get(lead.FirstName+lead.LastName)!= Null){
                        LeadConvert.setAccountId(newCreatedAccounts.get(lead.FirstName+lead.LastName).Id);
                    }  
                }
                System.debug('LeadConvert.getAccountId()'+LeadConvert.getAccountId()); 
                massLeadConvert.add(LeadConvert);
                system.debug('massLeadConvert'+massLeadConvert);
            }
        }
        
        if(!accountsToUpdate.isEmpty()){
            system.debug('accountsToUpdate'+accountsToUpdate);
            update accountsToUpdate;
            system.debug('accountsToUpdate'+accountsToUpdate);
            
        }
        
        if(!massLeadConvert.isEmpty()){
            system.debug('massLeadConvert'+massLeadConvert);
            List<Database.LeadConvertResult> lcr = Database.convertLead(massLeadConvert);
            for(Database.LeadConvertResult leadConvertResult : lcr) {
                system.debug('leadConvertResult'+leadConvertResult);
                system.debug('accountid'+existingAccounts.containskey(leadConvertResult.accountid));
                if(leadConvertResult.isSuccess()  ) {
                    Contract contract = new Contract();
                    contract.Status = 'Draft';
                    contract.AccountId = leadConvertResult.getAccountId();
                    contract.StartDate = system.today();
                    contract.ContractTerm = 12;
                    if(leadProductMap.containskey(leadConvertResult.getLeadId()) && leadProductMap.get(leadConvertResult.getLeadId())!=Null){
                        contract.Service__c = leadProductMap.get(leadConvertResult.getLeadId());
                    }
                    //if(leadHealthcareFacilityMap.containskey(leadConvertResult.getLeadId()) && leadHealthcareFacilityMap.get(leadConvertResult.getLeadId())!=Null ){
                        //contract.Depot_Name__c = leadHealthcareFacilityMap.get(leadConvertResult.getLeadId());
                    
                    if(leadmap.containskey(leadConvertResult.getLeadId())  ) {
                        contract.BillingCity= leadmap.get(leadConvertResult.getLeadId()).City;
                        contract.BillingState= leadmap.get(leadConvertResult.getLeadId()).State;
                        contract.BillingCountry = leadmap.get(leadConvertResult.getLeadId()).Country;
                        contract.BillingStreet=leadmap.get(leadConvertResult.getLeadId()).Street;
                        contract.BillingPostalCode=leadmap.get(leadConvertResult.getLeadId()).PostalCode;
                        contract.ShippingCity = leadmap.get(leadConvertResult.getLeadId()).City;
                        contract.ShippingState = leadmap.get(leadConvertResult.getLeadId()).State;
                        contract.ShippingCountry = leadmap.get(leadConvertResult.getLeadId()).Country;
                        contract.ShippingStreet = leadmap.get(leadConvertResult.getLeadId()).Street;
                        contract.ShippingPostalCode = leadmap.get(leadConvertResult.getLeadId()).PostalCode;
                        contract.Signature__c = leadmap.get(leadConvertResult.getLeadId()).Signature__c;
                        contract.Signature_Date_Time__c = leadmap.get(leadConvertResult.getLeadId()).Signature_Date_Time__c;
                        contract.Is_Policy_Holder__c = leadmap.get(leadConvertResult.getLeadId()).Is_Policy_Holder__c;
                        contract.Insurance__c = leadmap.get(leadConvertResult.getLeadId()).Insurance__c;
                        contract.Insurance_Provider__c = leadmap.get(leadConvertResult.getLeadId()).Insurance_Provider__c;
                        contract.Policy_Holder_First_Name__c = leadmap.get(leadConvertResult.getLeadId()).Policy_Holder_First_Name__c;
                        contract.Policy_Holder_Last_Name__c = leadmap.get(leadConvertResult.getLeadId()).Policy_Holder_Last_Name__c;
                        contract.Policy_ID__c = leadmap.get(leadConvertResult.getLeadId()).Policy_ID__c;
                        contract.Group_ID__c = leadmap.get(leadConvertResult.getLeadId()).Group_ID__c;
 
                        
                        //contract.Address_Line_2__c = leadmap.get(leadConvertResult.getLeadId()).Address_Line_2__c; 
                        
                    }
                        Address__c Address = new Address__c();
                        Address.Account__c = leadConvertResult.getAccountId();
                        Address.Address_Type__c = leadmap.get(leadConvertResult.getLeadId()).Address_Type__c;
                        Address.Name = leadmap.get(leadConvertResult.getLeadId()).Street;
                        //Address.Street__c = leadmap.get(leadConvertResult.getLeadId()).Street;
                        Address.City__c = leadmap.get(leadConvertResult.getLeadId()).City;
                        Address.State__c = leadmap.get(leadConvertResult.getLeadId()).State;
                        Address.Zip_Code__c = leadmap.get(leadConvertResult.getLeadId()).PostalCode;
                        Address.Country__c = leadmap.get(leadConvertResult.getLeadId()).Country;
                        //Address.Landmark__c = leadmap.get(leadConvertResult.getLeadId()).City;
                    
                        //MemberPlan member = new MemberPlan();
                        //member.MemberId = leadConvertResult.getAccountId();
                        //member.Is_Policy_Holder__c = leadmap.get(leadConvertResult.getLeadId()).Is_Policy_Holder__c;
                        //member.Name = leadmap.get(leadConvertResult.getLeadId()).Insurance_Name__c;
                        //member.Insurance_Options__c = leadmap.get(leadConvertResult.getLeadId()).Insurance__c;
                        //member.MemberNumber = leadmap.get(leadConvertResult.getLeadId()).Policy_ID__c;
                        //member.GroupNumber = leadmap.get(leadConvertResult.getLeadId()).Group_ID__c;
                        //member.Policy_Holder_First_Name__c = leadmap.get(leadConvertResult.getLeadId()).Policy_Holder_First_Name__c;
                        //member.Policy_Holder_Last_Name__c = leadmap.get(leadConvertResult.getLeadId()).Policy_Holder_Last_Name__c;
                    contractsToInsert.add(contract);
                    //memberplanToInsert.add(member);
                    addressesToInsert.add(Address);
                }
            }
        }        
        if(!contractsToInsert.isEmpty()){
            insert contractsToInsert;
        }
        //if(!memberplanToInsert.isEmpty()){
            //insert memberplanToInsert;
        
        if(!addressesToInsert.isEmpty()){
            insert addressesToInsert;
        }
    }catch(Exception e){
        System.debug('Error: '+e.getMessage()+ 'Line Number'+e.getLineNumber());
    }
}
}

I need to create account with the lead first and last name

In lead I have four fields :- 
First Name
Last Name
Shipping First Name
Shipping Last name

While creating record we need to give input for all the fields. I need to do one automation that, when a user click on checkbox "Same as First and Last name" then Shipping First Name and Shipping last name must be automatically write the values of First name and Last name.

Summary :- 

First Name =  Test
Last Name = Check
Clicked on Same as First name and last name checkbox
Shipping first name  = test
Shipping Last name = check

Thanks
I want to cancel contract and its order.

A custom object Cancellation Request is there and it is having First name, last Name, Email, birthdate. Data will be coming from an intak form from Marketing cloud. 
When form submission will be done the data will be coming to "Cancellation Request " Obj and it will check wether any account is there or not with those same  First name, last Name, Email, birthdate. If there then it will cancel the contract and order.

There is no relation between Cancellation Request Obj and Account, Contracts and Orders. 

how we can achieve this 

Thanks
Hi,

Currently am converting Lead to Contract, while converting it creates account, Memberplan, Address record also. 
Now I need to do it in Batch so that it will convert all the leads at a single time. Batch should run according the condition of class am using currently.
How to achieve this ?

Thank you

Hi,

Currently am converting Lead to Contract, while converting it creates account, Memberplan, Address record also. 

Now I need to do it in Batch so that it will convert all the leads at a single time. Batch should run according the condition of class am using currently.

How to achieve this ?

Thanks

I had created a record-triggered flow for lead. When lead is created a SMS will be sent to Lead's Mobile number. 
now it is going well and fine. 

But the SMS should go once not repeatedly. Till the lead exist the SMS goes to that lead's mobile number. How to prevent it ?
 
Hi,
I added one documents in Notes and Attachment in Account object. In the activity tab in email section when I click on send then it should send email attaching the latest document in Note & Attachment.

How we can acheive this ?
User-added image
 
In MemberPlan if a insurance is marked as primary then it should get updated in the Account lookup custom field (Insurance) with that primary marked lookup.

Primary is the custom field in MemberPlan standard object
Insurance is lookup with MemberPlan.

Any MemberPlan which is marked primary that must get displayed in the Insurance field of Account

Thanks
I have a  batch class which is running everyday and checks if the contract status is Activated or not if activated then it creates the order and its products.

1. Get the everyday logs on batch runs and number of order created and its status.

2. In case batch did not run due to any issue(exceptions)  that also needs to be notified.

Can I get any help how to achieve on this.

Thanks
Hi,

I am adding a progress indicator in one of my requirement referred from this link :- https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/components_config_for_flow_screens_stages.htm

It is not getting progress perfectly.
Green color is not coming on the first stage. I had attached a file for reference
User-added image

Thanks
I have written a batch which is creating order and order item when contract status activated.
When the order is created the status field is showing "Draft" which is giving default value as we cannot change anything while creating order.

So, I need to set that Status to "Order Placed" instead of "Draft".

Here is the Batch :- 

/* Class Type :- Batch
* Date :- 
* Description :- Creates a new order if Contract Status is Activated after one month and update Next Order Date
* Author :- 
*/ 
global class AutoSubscriptionForOrderCreation implements Database.Batchable<sObject>,schedulable {
    Public void execute(SchedulableContext sc) 
    { 
        database.executebatch(new AutoSubscriptionForOrderCreation());
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
        Date orderCreationDate = system.today().addDays(3);
        //system.debug('output'+[SELECT Id,Status,Next_Order_Date__c,Service__r.id,AccountId,ShippingAddress,BillingAddress,Insurance__c From Contract Where Status = 'Activated' And Next_Order_Date__c = TODAY]);
        if(Test.isRunningTest()){
            return Database.getQueryLocator([SELECT Id,Status,Next_Order_Date__c,Service__r.id,AccountId,account.PersonContactId,ShippingAddress,BillingAddress,Insurance__c,BillingState,BillingCity,BillingStreet,BillingCountry,BillingPostalCode,BillingLatitude,BillingLongitude,ShippingStreet,ShippingState,ShippingCity,ShippingCountry,ShippingPostalCode,ShippingLatitude,ShippingLongitude,CustomerSignedId From Contract]);
        }else{
           return Database.getQueryLocator([SELECT Id,Status,Next_Order_Date__c,Service__r.id,AccountId,account.PersonContactId,ShippingAddress,BillingAddress,Insurance__c,BillingState,BillingCity,BillingStreet,BillingCountry,BillingPostalCode,BillingLatitude,BillingLongitude,ShippingStreet,ShippingState,ShippingCity,ShippingCountry,ShippingPostalCode,ShippingLatitude,ShippingLongitude,CustomerSignedId From Contract Where Status = 'Activated' And Next_Order_Date__c =: orderCreationDate]); 
        }
    }
    
    global void execute(Database.BatchableContext bc, List<Contract> contractlst) {
        List<Order> orderlsttoinsert = new List<Order>();
        List<OrderItem> orderItemsToInsert = new List<OrderItem>();
        List<Contract> contractupdatelst = new List<Contract>();
        map<Id, Id> contractProdMap = New map<Id, Id>();
        if(contractlst.size() > 0){
            
            Pricebook2 priceBook = [select id, name from Pricebook2 where isStandard = true limit 1];
            List<PriceBookEntry> priceBookEntryList = [SELECT Product2.Id, Product2.Name, UnitPrice FROM PriceBookEntry WHERE Pricebook2Id =:priceBook.id];            
            //Set <Id> accid  = new set <Id>();
            //List
            for(Contract con: contractlst){
                //Insert Orders
                Order orderObj = new Order();
                orderObj.AccountId = con.AccountId;
                orderObj.Status = 'Draft';
                orderObj.ContractId = con.Id;
                orderObj.EffectiveDate = System.today();
                orderObj.Pricebook2Id =  priceBook.id;
                orderObj.Insurance__c =  con.Insurance__c;
                orderObj.Type = 'Subscription Order';
                orderObj.BillingStreet = con.BillingStreet;
                orderObj.BillingState = con.BillingState;
                orderObj.BillingCity = con.BillingCity;
                orderObj.BillingCountry = con.BillingCountry;
                orderObj.BillingPostalCode = con.BillingPostalCode;
                orderObj.BillingLatitude = con.BillingLatitude;
                orderObj.BillingLongitude = con.BillingLongitude;
                orderObj.ShippingStreet = con.ShippingStreet;
                orderObj.ShippingState = con.ShippingState;
                orderObj.ShippingCity = con.ShippingCity;
                orderObj.ShippingCountry = con.ShippingCountry;
                orderObj.ShippingPostalCode = con.ShippingPostalCode;
                orderObj.ShippingLatitude = con.ShippingLatitude;
                orderObj.ShippingLongitude = con.ShippingLongitude;
                orderObj.ShipToContactId =con.account.PersonContactId; 
                orderObj.BillToContactId = con.account.PersonContactId;
                orderObj.EffectiveDate = con.Next_Order_Date__c;
                orderlsttoinsert.add(orderObj);
                
                //Create Contract and Product Map
                for(PriceBookEntry pBookEntry : priceBookEntryList){
                    if(pBookEntry.Product2.Id == con.Service__r.Id){
                        contractProdMap.put(con.Id, pBookEntry.Id);
                    }
                }
                
                // Update order date to next month's nearest Wednesday
                Date nextOrderDate = ((System.today().addMonths(1)).toStartOfWeek().addDays(3));
                con.Next_Order_Date__c = nextOrderDate;
                contractupdatelst.add(con);
                
            }
            if(orderlsttoinsert.size() > 0){
                insert orderlsttoinsert;               
                for(Order O: orderlsttoinsert){
                    //Insert Order Items
                    OrderItem oi = new OrderItem();
                    oi.OrderId = O.id;
                    oi.Quantity = 1;
                    oi.UnitPrice = 1;
                    oi.Product2id = O.Contract.Service__c;
                    oi.PricebookEntryId = contractProdMap.get(O.ContractId);
                    oi.sstation__Currency_Type__c = 'USD';
                    orderItemsToInsert.add(oi);
                }    
            }
            if(orderItemsToInsert.size() > 0){
                insert orderItemsToInsert;
            }
            if(contractupdatelst.size() > 0){
                update contractupdatelst;
            }
        }
    }    
    global void finish(Database.BatchableContext bc) {
        
    }
}
I have a lookup field in Lead object and Contract Object.
Field Name -  Service 
Lookup with Product.

I need to auto-populate only one product in those lookup field in Lead as well as Contract before creatin record.

When we click on new button at that moment only the lookup field must have "Value" product.

Thanks
How to set a value as default value in a lookup field?

 
Class :- 

public class AutoConvertLead {
@InvocableMethod
public static void assignLeads(List<Id> LeadIds) { 
    try{ 
        Map<String,Date> NameDOBMap = new Map<String,Date>();
        List<String> leadNames = new List<String>();
        List<Date> leadDOBs = new List<Date>();
        List<Database.LeadConvert> massLeadConvert = new List<Database.LeadConvert>();
        List<Account> accountsToUpdate = new List<Account>();
        List<Account> accountsToInsert = new List<Account>();
        List<Contract> contractsToInsert = new List<Contract>();
        Map<String,Account> nameDOBToAccountMap = new Map<String,Account>();
        Map<String,Account> newCreatedAccounts = new Map<String,Account>();
        Map<ID,Id> leadProductMap = new Map<Id,Id>();
        
        Id personAccountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
        LeadStatus cLeadStatus = [SELECT Id,MasterLabel FROM LeadStatus WHERE isConverted = true LIMIT 1];  
        
       // List<Lead> leads = [SELECT Id,Name,LastName,FirstName,Email,Date_Of_Birth__c,Service__c,Ethnicity__c, Race__c,Gender__c FROM Lead WHERE Id IN :LeadIds];
       Map<Id,Lead> leadmap= new Map<Id,Lead>([SELECT Id,Name,LastName,FirstName,Email,Date_Of_Birth__c,Service__c,Ethnicity__c,MobilePhone, Race__c,Gender__c,City,Country,State,Street,PostalCode FROM Lead WHERE Id IN :LeadIds]);
       // for(Lead lead: leads) {
        for(Lead lead: leadmap.values()) {
            leadNames.add(String.valueOf(lead.Name));
            leadDOBs.add(lead.Date_Of_Birth__c);
            NameDOBMap.put(lead.Name, lead.Date_Of_Birth__c);
            leadProductMap.put(lead.Id,lead.Service__c);
        }
        
        Map<Id, Account> existingAccounts = new Map<Id, Account>([SELECT Id, PersonBirthdate, Name, PersonGender FROM Account WHERE Name IN :NameDOBMap.KeySet() AND PersonBirthdate IN :NameDOBMap.values()]);
        
        for(account acc: existingAccounts.values()){
            
        }
        //System.debug('Check 1'+existingAccounts);
        for(Account accc: existingAccounts.values()){
            nameDOBToAccountMap.put(accc.Name+accc.PersonBirthdate, accc);
        }
        
       // for(Lead ld : leads){
        for(Lead ld: leadmap.values()) {
            if(!nameDOBToAccountMap.containsKey(ld.Name+ld.Date_Of_Birth__c)){
                Account existingAccount = new Account();
                existingAccount.LastName = ld.LastName;
                existingAccount.FirstName = ld.FirstName;
                existingAccount.PersonEmail = ld.Email;
                existingAccount.PersonGender = ld.Gender__c;
                existingAccount.RecordTypeId =  personAccountRecordTypeId;
                existingAccount.Ethnicity__pc = ld.Ethnicity__c;
                existingAccount.Race__pc = ld.Race__c;
                existingAccount.PersonGender = ld.Gender__c;
                existingAccount.PersonBirthdate = ld.Date_Of_Birth__c;
                existingAccount.PersonHomePhone = ld.MobilePhone;
                accountsToInsert.add(existingAccount);
            }  
        }
        
        if(accountsToInsert.size()>0){
            insert accountsToInsert;
        }
        //system.debug('accountsToInsert'+accountsToInsert);
        
        for(account acc: accountsToInsert){
            newCreatedAccounts.put(acc.FirstName+acc.LastName, acc);
        }
        //system.debug('newCreatedAccounts'+newCreatedAccounts);
        //for(Lead lead : leads){
            for(Lead lead : leadmap.values()){
            //System.debug('Check 2');
            Database.LeadConvert LeadConvert = new Database.LeadConvert();
            LeadConvert.setLeadId(lead.Id);
            LeadConvert.setConvertedStatus(cLeadStatus.MasterLabel);
            LeadConvert.setDoNotCreateOpportunity(false);
            
            if(lead.Name != null && lead.Date_Of_Birth__c != null){                    
                if(nameDOBToAccountMap.containsKey(lead.Name+lead.Date_Of_Birth__c) && nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c)!=Null){
                    //system.debug('existingAccount==>'+nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c));
                    Account existingAccount = new Account();
                    existingAccount.Id = nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c).id;
                    //system.debug('existingAccount'+existingAccount);
                    existingAccount.Ethnicity__pc = lead.Ethnicity__c;
                    existingAccount.Race__pc = lead.Race__c;
                    existingAccount.PersonGender = lead.Gender__c;
                    existingAccount.PersonHomePhone = lead.MobilePhone;
                    accountsToUpdate.add(existingAccount);
                    LeadConvert.setAccountId(existingAccount.Id);
                }
                else{
                    //system.debug('lead.Name+lead.Date_Of_Birth__c'+lead.FirstName+lead.LastName);
                    if(newCreatedAccounts.containskey(lead.FirstName+lead.LastName) && newCreatedAccounts.get(lead.FirstName+lead.LastName)!= Null){
                        LeadConvert.setAccountId(newCreatedAccounts.get(lead.FirstName+lead.LastName).Id);
                    }  
                }
                //System.debug('LeadConvert.getAccountId()'+LeadConvert.getAccountId()); 
                massLeadConvert.add(LeadConvert);
                //system.debug('massLeadConvert'+massLeadConvert);
            }
        }
        
        if(!accountsToUpdate.isEmpty()){
            //system.debug('accountsToUpdate'+accountsToUpdate);
            update accountsToUpdate;
            //system.debug('accountsToUpdate'+accountsToUpdate);
            
        }
        
        if(!massLeadConvert.isEmpty()){
            //system.debug('massLeadConvert'+massLeadConvert);
            List<Database.LeadConvertResult> lcr = Database.convertLead(massLeadConvert);
            for(Database.LeadConvertResult leadConvertResult : lcr) {
                //system.debug('leadConvertResult'+leadConvertResult);
                //system.debug('accountid'+existingAccounts.containskey(leadConvertResult.accountid));
                if(leadConvertResult.isSuccess() && !existingAccounts.containskey(leadConvertResult.accountid) ) {
                    Contract contract = new Contract();
                    contract.Status = 'Draft';
                    contract.AccountId = leadConvertResult.getAccountId();
                    contract.StartDate = system.today();
                    contract.ContractTerm = 12;
                    if(leadProductMap.containskey(leadConvertResult.getLeadId()) && leadProductMap.get(leadConvertResult.getLeadId())!=Null){
                        contract.Service__c = leadProductMap.get(leadConvertResult.getLeadId());
                    }
                    if(leadmap.containskey(leadConvertResult.getLeadId())  ) {
                            
                        contract.BillingCity= leadmap.get(leadConvertResult.getLeadId()).City;
                        contract.BillingState= leadmap.get(leadConvertResult.getLeadId()).State;
                        contract.BillingCountry = leadmap.get(leadConvertResult.getLeadId()).Country;
                        contract.BillingStreet=leadmap.get(leadConvertResult.getLeadId()).Street;
                        contract.BillingPostalCode=leadmap.get(leadConvertResult.getLeadId()).PostalCode;
                        contract.ShippingCity = leadmap.get(leadConvertResult.getLeadId()).City;
                        contract.ShippingState = leadmap.get(leadConvertResult.getLeadId()).State;
                        contract.ShippingCountry = leadmap.get(leadConvertResult.getLeadId()).Country;
                        contract.ShippingStreet = leadmap.get(leadConvertResult.getLeadId()).Street;
                        contract.ShippingPostalCode = leadmap.get(leadConvertResult.getLeadId()).PostalCode;

                    }
                    contractsToInsert.add(contract);
                }
            }
        }
        
        if(!contractsToInsert.isEmpty()){
            insert contractsToInsert;
        }
    }catch(Exception e){
        //System.debug('Error: '+e.getMessage()+ 'Line Number'+e.getLineNumber());
    }
}
}


Test Class:- 

@isTest
public class Test_AutoConvertLead {
    @isTest
    static void testAutoConvertLead()
    {
        String recordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
        Account acc= new Account(
        RecordTypeID=recordTypeId ,
        FirstName='Test FName',
        LastName='Test LName',
        PersonBirthdate = date.valueOf('1998-02-02') ,
        PersonGender = 'Male',
        PersonMailingStreet='test@yahoo.com',
        PersonMailingPostalCode='12345',
        PersonMailingCity='SFO',
        PersonEmail='test@yahoo.com',
        PersonHomePhone='1234567',
        PersonMobilePhone='12345678'   
        );        
        insert acc;
        
        Product2 p = new Product2();
        p.Name = 'Covid Test Kit';
        p.sstation__Msrp__c = 1;
        insert p;
        
        Lead ld = new lead();
        ld.LastName = 'test';
        ld.FirstName = 'class';
        ld.Email = 'test@gmail.com';
        ld.Date_Of_Birth__c = date.valueOf('1998-02-02') ;
        ld.Race__c = 'Asian';
        ld.Ethnicity__c = 'Hispanic or Latino';
        ld.Gender__c = 'Male';
        ld.Status = 'New';
        ld.Shipping_First_Name__c = 'test';
        ld.Shipping_Last_Name__c = 'class';
        ld.City = 'test';
        ld.Country = 'tes';
        ld.Street = 'test';
        ld.State = 'test';
        ld.PostalCode = '000000';
        ld.Service__c = p.Id;
        ld.MobilePhone = '9999999999';
        ld.Phone = '8888888888';
        insert ld;
        
        Account accc = new Account();
        accc.LastName = ld.LastName;
        accc.FirstName = ld.FirstName;
        accc.PersonEmail = ld.Email;
        //accc.PersonBirthdate = ld.Date_Of_Birth__c-3;
        accc.Race__pc = ld.Race__c;
        accc.Ethnicity__pc = ld.Ethnicity__c;
        accc.PersonGender = ld.Gender__c;
        accc.BillingCity = ld.City;
        accc.BillingCountry = ld.Country;
        accc.BillingPostalCode = ld.PostalCode;
        accc.BillingState = ld.State;
        accc.BillingStreet = ld.Street;
        accc.PersonHomePhone = ld.Phone;
        accc.PersonMobilePhone = ld.MobilePhone;
        insert accc;
        
  
        Contract cont = new Contract();
        cont.AccountId= accc.id;
        cont.StartDate=date.today();
        cont.ContractTerm=2;
        cont.Status='Draft';
        //cont.Pricebook2Id = standardPricebook.Id;
        //cont.Service__c = op.Id;
        //cont.Insurance__c = mp.Id;
        cont.BillingStreet = 'test';
        cont.BillingPostalCode = '759128';
        cont.BillingState = 'test';
        cont.BillingCity = 'test';
        cont.BillingCountry = 'test';
        cont.BillingLatitude = 20.296059;
        cont.BillingLongitude = 85.824539;
        cont.ShippingCity = 'test';
        cont.ShippingCountry = 'test';
        cont.ShippingPostalCode = '777777';
        cont.ShippingLatitude = 20.296059;
        cont.ShippingLongitude = 85.824539;
        cont.Service__c = p.Id;
        insert cont;
        
      
        {

            System.assert(True, 'ErrorMessage');
            
             }
        
        
        Test.startTest();
        Lead l = new Lead(LastName = 'Test Lead',
                     Company = 'Test Company',Race__c = 'Asian',Ethnicity__c = 'Hispanic or Latino'
                     );
        insert l;
        
        
        AutoConvertLead obj = new AutoConvertLead();
        //String CRON_EXP = '0 0 0 3 9 ? 2042';
        //system.schedule('Test status Check9', CRON_EXP, obj );
        AutoConvertLead.assignLeads(new List<String>{l.id});
        Test.stopTest();
        
  
        
        // For Schedulable AutoSubscriptionForOrderCreation
        
        
    }
          @isTest
    static void testAutoConvertLead2()
    {
        String recordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
        Account acc= new Account(
        RecordTypeID=recordTypeId ,
        FirstName='Lead',
        LastName='Test',
        PersonBirthdate = date.valueOf('1998-02-02') ,
        PersonGender = 'Male',
        PersonMailingStreet='test@yahoo.com',
        PersonMailingPostalCode='12345',
        PersonMailingCity='SFO',
        PersonEmail='test@yahoo.com',
        PersonHomePhone='1234567',
        PersonMobilePhone='12345678',
        BillingStreet = 'Test',
        BillingCity = 'Test',
        BillingState = 'Test',
        BillingPostalCode = 'Test',
        BillingCountry = 'Test'
        );        
        insert acc;
        Lead l2 = new Lead(LastName = 'Test', FirstName = 'Lead',
                     Company = 'Test Company',Race__c = 'Asian',Ethnicity__c = 'Hispanic or Latino', Date_Of_Birth__c = date.valueOf('1998-02-02')
                     );
        insert l2;
        AutoConvertLead.assignLeads(new List<String>{l2.id});
    }
    
}

Code coverage is only 74%

Code :- 

public class AutoConvertLead {
    @InvocableMethod
    public static void assignLeads(List<Id> LeadIds) { 
        try{ 
            Map<String,Date> NameDOBMap = new Map<String,Date>();
            List<String> leadNames = new List<String>();
            List<Date> leadDOBs = new List<Date>();
            List<Database.LeadConvert> massLeadConvert = new List<Database.LeadConvert>();
            List<Account> accountsToUpdate = new List<Account>();
            List<Account> accountsToInsert = new List<Account>();
            List<Contract> contractsToInsert = new List<Contract>();
            Map<String,Account> nameDOBToAccountMap = new Map<String,Account>();
            Map<String,Account> newCreatedAccounts = new Map<String,Account>();
            Map<ID,Id> leadProductMap = new Map<Id,Id>();
            
            Id personAccountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
            LeadStatus cLeadStatus = [SELECT Id,MasterLabel FROM LeadStatus WHERE isConverted = true LIMIT 1];  
            
            List<Lead> leads = [SELECT Id,Name,LastName,FirstName,Email,Date_Of_Birth__c,Service__c,Ethnicity__c, Race__c,Gender__c FROM Lead WHERE Id IN :LeadIds];
            
            for(Lead lead: leads) {
                leadNames.add(String.valueOf(lead.Name));
                leadDOBs.add(lead.Date_Of_Birth__c);
                NameDOBMap.put(lead.Name, lead.Date_Of_Birth__c);
                leadProductMap.put(lead.Id,lead.Service__c);
            }
            
            Map<Id, Account> existingAccounts = new Map<Id, Account>([SELECT Id, PersonBirthdate, Name, PersonGender FROM Account WHERE Name IN :NameDOBMap.KeySet() AND PersonBirthdate IN :NameDOBMap.values()]);
            
            for(account acc: existingAccounts.values()){
                
            }
            //System.debug('Check 1'+existingAccounts);
            for(Account accc: existingAccounts.values()){
                nameDOBToAccountMap.put(accc.Name+accc.PersonBirthdate, accc);
            }
            
            for(Lead ld : leads){
                if(!nameDOBToAccountMap.containsKey(ld.Name+ld.Date_Of_Birth__c)){
                    Account existingAccount = new Account();
                    existingAccount.LastName = ld.LastName;
                    existingAccount.FirstName = ld.FirstName;
                    existingAccount.PersonEmail = ld.Email;
                    existingAccount.PersonGender = ld.Gender__c;
                    existingAccount.RecordTypeId =  personAccountRecordTypeId;
                    existingAccount.Ethnicity__pc = ld.Ethnicity__c;
                    existingAccount.Race__pc = ld.Race__c;
                    existingAccount.PersonGender = ld.Gender__c;
                    existingAccount.PersonBirthdate = ld.Date_Of_Birth__c;
                    accountsToInsert.add(existingAccount);
                }  
            }
            
            if(accountsToInsert.size()>0){
                insert accountsToInsert;
            }
            //system.debug('accountsToInsert'+accountsToInsert);
            
            for(account acc: accountsToInsert){
                newCreatedAccounts.put(acc.FirstName+acc.LastName, acc);
            }
            //system.debug('newCreatedAccounts'+newCreatedAccounts);
            for(Lead lead : leads){
                //System.debug('Check 2');
                Database.LeadConvert LeadConvert = new Database.LeadConvert();
                LeadConvert.setLeadId(lead.Id);
                LeadConvert.setConvertedStatus(cLeadStatus.MasterLabel);
                LeadConvert.setDoNotCreateOpportunity(false);
                
                if(lead.Name != null && lead.Date_Of_Birth__c != null){                    
                    if(nameDOBToAccountMap.containsKey(lead.Name+lead.Date_Of_Birth__c) && nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c)!=Null){
                        //system.debug('existingAccount==>'+nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c));
                        Account existingAccount = new Account();
                        existingAccount.Id = nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c).id;
                        //system.debug('existingAccount'+existingAccount);
                        existingAccount.Ethnicity__pc = lead.Ethnicity__c;
                        existingAccount.Race__pc = lead.Race__c;
                        existingAccount.PersonGender = lead.Gender__c;
                        accountsToUpdate.add(existingAccount);
                        LeadConvert.setAccountId(existingAccount.Id);
                    }
                    else{
                        //system.debug('lead.Name+lead.Date_Of_Birth__c'+lead.FirstName+lead.LastName);
                        if(newCreatedAccounts.containskey(lead.FirstName+lead.LastName) && newCreatedAccounts.get(lead.FirstName+lead.LastName)!= Null){
                            LeadConvert.setAccountId(newCreatedAccounts.get(lead.FirstName+lead.LastName).Id);
                        }  
                    }
                    //System.debug('LeadConvert.getAccountId()'+LeadConvert.getAccountId()); 
                    massLeadConvert.add(LeadConvert);
                    //system.debug('massLeadConvert'+massLeadConvert);
                }
            }
            
            if(!accountsToUpdate.isEmpty()){
                //system.debug('accountsToUpdate'+accountsToUpdate);
                update accountsToUpdate;
                //system.debug('accountsToUpdate'+accountsToUpdate);
                
            }
            
            if(!massLeadConvert.isEmpty()){
                //system.debug('massLeadConvert'+massLeadConvert);
                List<Database.LeadConvertResult> lcr = Database.convertLead(massLeadConvert);
                for(Database.LeadConvertResult leadConvertResult : lcr) {
                    //system.debug('leadConvertResult'+leadConvertResult);
                    //system.debug('accountid'+existingAccounts.containskey(leadConvertResult.accountid));
                    if(leadConvertResult.isSuccess() && !existingAccounts.containskey(leadConvertResult.accountid) ) {
                        Contract contract = new Contract();
                        contract.Status = 'Draft';
                        contract.AccountId = leadConvertResult.getAccountId();
                        contract.StartDate = system.today();
                        contract.ContractTerm = 12;
                        if(leadProductMap.containskey(leadConvertResult.getLeadId()) && leadProductMap.get(leadConvertResult.getLeadId())!=Null){
                            contract.Service__c = leadProductMap.get(leadConvertResult.getLeadId());
                        }
                        contractsToInsert.add(contract);
                    }
                }
            }
            
            if(!contractsToInsert.isEmpty()){
                insert contractsToInsert;
            }
        }catch(Exception e){
            //System.debug('Error: '+e.getMessage()+ 'Line Number'+e.getLineNumber());
        }
    }
}

 

In this code I need to modify that Address field of lead should get fetched in Contract's Billing and Shipping address.

In this conversion Lead's address is coming to Account's Billing address but not coming to Account's Shipping address.

How to achieve this"

Thanks

I have a field Next order date in contract
Written a batch class to creaet automatically order if the contract is Activated. When the Contract status is made activated at that time the Next order Date will display the immediate wednesday which is handled by flow.

I need to do an setup that when Order status will be Order Dispatched then the Next Order Date should display Order Start Date + 30 days or Order Start Date + 30 days  (immediate wednesday).

Is it possible by flow If yes then how, if no then how we can achieve this.

Thanks
My test class is running but not covering 

Class :- 
public class AutoConvertLead {
    @InvocableMethod
    public static void assignLeads(List<Id> LeadIds) { 
        try{ 
            Map<String,Date> NameDOBMap = new Map<String,Date>();
            List<String> leadNames = new List<String>();
            List<Date> leadDOBs = new List<Date>();
            List<Database.LeadConvert> massLeadConvert = new List<Database.LeadConvert>();
            List<Account> accountsToUpdate = new List<Account>();
            List<Account> accountsToInsert = new List<Account>();
            List<Contract> contractsToInsert = new List<Contract>();
            Map<String,Account> nameDOBToAccountMap = new Map<String,Account>();
            Map<String,Account> newCreatedAccounts = new Map<String,Account>();
            Map<ID,Id> leadProductMap = new Map<Id,Id>();
            
            Id personAccountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
            LeadStatus cLeadStatus = [SELECT Id,MasterLabel FROM LeadStatus WHERE isConverted = true LIMIT 1];  
            
            List<Lead> leads = [SELECT Id,Name,LastName,FirstName,Email,Date_Of_Birth__c,Service__c,Ethnicity__c, Race__c,Gender__c FROM Lead WHERE Id IN :LeadIds];
            
            for(Lead lead: leads) {
                leadNames.add(String.valueOf(lead.Name));
                leadDOBs.add(lead.Date_Of_Birth__c);
                NameDOBMap.put(lead.Name, lead.Date_Of_Birth__c);
                leadProductMap.put(lead.Id,lead.Service__c);
            }
            
            Map<Id, Account> existingAccounts = new Map<Id, Account>([SELECT Id, PersonBirthdate, Name, PersonGender FROM Account WHERE Name IN :NameDOBMap.KeySet() AND PersonBirthdate IN :NameDOBMap.values()]);
            
            for(account acc: existingAccounts.values()){
                
            }
            //System.debug('Check 1'+existingAccounts);
            for(Account accc: existingAccounts.values()){
                nameDOBToAccountMap.put(accc.Name+accc.PersonBirthdate, accc);
            }
            
            for(Lead ld : leads){
                if(!nameDOBToAccountMap.containsKey(ld.Name+ld.Date_Of_Birth__c)){
                    Account existingAccount = new Account();
                    existingAccount.LastName = ld.LastName;
                    existingAccount.FirstName = ld.FirstName;
                    existingAccount.PersonEmail = ld.Email;
                    existingAccount.PersonGender = ld.Gender__c;
                    existingAccount.RecordTypeId =  personAccountRecordTypeId;
                    existingAccount.Ethnicity__pc = ld.Ethnicity__c;
                    existingAccount.Race__pc = ld.Race__c;
                    existingAccount.PersonGender = ld.Gender__c;
                    existingAccount.PersonBirthdate = ld.Date_Of_Birth__c;
                    accountsToInsert.add(existingAccount);
                }  
            }
            
            if(accountsToInsert.size()>0){
                insert accountsToInsert;
            }
            //system.debug('accountsToInsert'+accountsToInsert);
            
            for(account acc: accountsToInsert){
                newCreatedAccounts.put(acc.FirstName+acc.LastName, acc);
            }
            //system.debug('newCreatedAccounts'+newCreatedAccounts);
            for(Lead lead : leads){
                //System.debug('Check 2');
                Database.LeadConvert LeadConvert = new Database.LeadConvert();
                LeadConvert.setLeadId(lead.Id);
                LeadConvert.setConvertedStatus(cLeadStatus.MasterLabel);
                LeadConvert.setDoNotCreateOpportunity(false);
                
                if(lead.Name != null && lead.Date_Of_Birth__c != null){                    
                    if(nameDOBToAccountMap.containsKey(lead.Name+lead.Date_Of_Birth__c) && nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c)!=Null){
                        //system.debug('existingAccount==>'+nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c));
                        Account existingAccount = new Account();
                        existingAccount.Id = nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c).id;
                        //system.debug('existingAccount'+existingAccount);
                        existingAccount.Ethnicity__pc = lead.Ethnicity__c;
                        existingAccount.Race__pc = lead.Race__c;
                        existingAccount.PersonGender = lead.Gender__c;
                        accountsToUpdate.add(existingAccount);
                        LeadConvert.setAccountId(existingAccount.Id);
                    }
                    else{
                        //system.debug('lead.Name+lead.Date_Of_Birth__c'+lead.FirstName+lead.LastName);
                        if(newCreatedAccounts.containskey(lead.FirstName+lead.LastName) && newCreatedAccounts.get(lead.FirstName+lead.LastName)!= Null){
                            LeadConvert.setAccountId(newCreatedAccounts.get(lead.FirstName+lead.LastName).Id);
                        }  
                    }
                    //System.debug('LeadConvert.getAccountId()'+LeadConvert.getAccountId()); 
                    massLeadConvert.add(LeadConvert);
                    //system.debug('massLeadConvert'+massLeadConvert);
                }
            }
            
            if(!accountsToUpdate.isEmpty()){
                //system.debug('accountsToUpdate'+accountsToUpdate);
                update accountsToUpdate;
                //system.debug('accountsToUpdate'+accountsToUpdate);
                
            }
            
            if(!massLeadConvert.isEmpty()){
                //system.debug('massLeadConvert'+massLeadConvert);
                List<Database.LeadConvertResult> lcr = Database.convertLead(massLeadConvert);
                for(Database.LeadConvertResult leadConvertResult : lcr) {
                    //system.debug('leadConvertResult'+leadConvertResult);
                    //system.debug('accountid'+existingAccounts.containskey(leadConvertResult.accountid));
                    if(leadConvertResult.isSuccess() && !existingAccounts.containskey(leadConvertResult.accountid) ) {
                        Contract contract = new Contract();
                        contract.Status = 'Draft';
                        contract.AccountId = leadConvertResult.getAccountId();
                        contract.StartDate = system.today();
                        contract.ContractTerm = 12;
                        if(leadProductMap.containskey(leadConvertResult.getLeadId()) && leadProductMap.get(leadConvertResult.getLeadId())!=Null){
                            contract.Service__c = leadProductMap.get(leadConvertResult.getLeadId());
                        }
                        contractsToInsert.add(contract);
                    }
                }
            }
            
            if(!contractsToInsert.isEmpty()){
                insert contractsToInsert;
            }
        }catch(Exception e){
            //System.debug('Error: '+e.getMessage()+ 'Line Number'+e.getLineNumber());
        }
    }
}

Test Class :- 
 
@isTest
public class Test_AutoConvertLead {
    @isTest
    static void testAutoConvertLead()
    {
        String recordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
        Account acc= new Account(
        RecordTypeID=recordTypeId ,
        FirstName='Test FName',
        LastName='Test LName',
        PersonBirthdate = system.today(),
        PersonGender = 'Male',
        PersonMailingStreet='test@yahoo.com',
        PersonMailingPostalCode='12345',
        PersonMailingCity='SFO',
        PersonEmail='test@yahoo.com',
        PersonHomePhone='1234567',
        PersonMobilePhone='12345678'
        );        
        insert acc;
        
        Product2 p = new Product2();
        p.Name = 'product';
        p.sstation__Msrp__c = 1;
        insert p;
        
        Lead ld = new lead();
        ld.LastName = 'test';
        ld.FirstName = 'class';
        ld.Email = 'test@gmail.com';
        ld.Date_Of_Birth__c = system.today();
        ld.Race__c = 'Asian';
        ld.Ethnicity__c = 'Hispanic or Latino';
        ld.Gender__c = 'Male';
        ld.Status = 'New';
        ld.Shipping_First_Name__c = 'test';
        ld.Shipping_Last_Name__c = 'class';
        ld.City = 'test';
        ld.Country = 'tes';
        ld.Street = 'test';
        ld.State = 'test';
        ld.PostalCode = '000000';
        ld.Service__c = p.Id;
        insert ld;
        
        Account accc = new Account();
        accc.LastName = ld.LastName;
        accc.FirstName = ld.FirstName;
        accc.PersonEmail = ld.Email;
        //accc.PersonBirthdate = ld.Date_Of_Birth__c-3;
        accc.Race__pc = ld.Race__c;
        accc.Ethnicity__pc = ld.Ethnicity__c;
        accc.PersonGender = ld.Gender__c;
        accc.BillingCity = ld.City;
        accc.BillingCountry = ld.Country;
        accc.BillingPostalCode = ld.PostalCode;
        accc.BillingState = ld.State;
        accc.BillingStreet = ld.Street;
        insert accc;
        
  
        Contract cont = new Contract();
        cont.AccountId= accc.id;
        cont.StartDate=date.today();
        cont.ContractTerm=2;
        cont.Status='Draft';
        //cont.Pricebook2Id = standardPricebook.Id;
        //cont.Service__c = op.Id;
        //cont.Insurance__c = mp.Id;
        cont.BillingStreet = 'test';
        cont.BillingPostalCode = '759128';
        cont.BillingState = 'test';
        cont.BillingCity = 'test';
        cont.BillingCountry = 'test';
        cont.BillingLatitude = 20.296059;
        cont.BillingLongitude = 85.824539;
        cont.ShippingCity = 'test';
        cont.ShippingCountry = 'test';
        cont.ShippingPostalCode = '777777';
        cont.ShippingLatitude = 20.296059;
        cont.ShippingLongitude = 85.824539;
        cont.Service__c = p.Id;
        insert cont;
        
      
        {

            System.assert(True, 'ErrorMessage');
            
             }
        
        
        Test.startTest();
        Lead l = new Lead(LastName = 'Test Lead',
                     Company = 'Test Company',Race__c = 'Asian',Ethnicity__c = 'Hispanic or Latino'
                     );
        insert l;
        
        AutoConvertLead obj = new AutoConvertLead();
        //String CRON_EXP = '0 0 0 3 9 ? 2042';
        //system.schedule('Test status Check9', CRON_EXP, obj );
        
        Test.stopTest();
        
        
        // For Schedulable AutoSubscriptionForOrderCreation
        
        
    }

}
public class AutoConvertLead {
    @InvocableMethod
    public static void assignLeads(List<Id> LeadIds) {
        try{
            LeadStatus cLeadStatus = [SELECT Id,MasterLabel FROM LeadStatus WHERE isConverted = true LIMIT 1];
            List<Lead> leads = [SELECT Id,Name,Date_Of_Birth__c,Ethnicity__c, Race__c FROM Lead WHERE Id IN :LeadIds];
            Map<String,Date> NameDOBMap = new Map<String,Date>();
            List<String> leadNames = new List<String>();
            List<Date> leadDOBs = new List<Date>();
            for(Lead lead: leads) {
                System.debug('Check 1'+lead.Name);
                leadNames.add(String.valueOf(lead.Name));
                leadDOBs.add(lead.Date_Of_Birth__c);
                NameDOBMap.put(lead.Name, lead.Date_Of_Birth__c);
            }
            Map<Id, Account> existingAccounts = new Map<Id, Account>([SELECT Id, PersonBirthdate, Name FROM Account WHERE Name IN :NameDOBMap.KeySet() AND PersonBirthdate IN :NameDOBMap.values()]);
            List<Database.LeadConvert> massLeadConvert = new List<Database.LeadConvert>();
            for(Lead lead : leads){
                System.debug('Check 2'+lead.Name);
                Database.LeadConvert LeadConvert = new Database.LeadConvert();
                LeadConvert.setLeadId(lead.Id);
                LeadConvert.setConvertedStatus(cLeadStatus.MasterLabel);
                if(lead.Name != null && lead.Date_Of_Birth__c != null){
                    System.debug('Check 3'+lead.Name);
                    if(NameDOBMap.containsKey(lead.Name) && lead.Date_Of_Birth__c==NameDOBMap.get(lead.Name)){
                        System.debug('Check 4'+lead.Name);
                        Account existingAccount = existingAccounts.get(lead.Id);
                        existingAccount.Ethnicity__pc = lead.Ethnicity__c;
                        existingAccount.Race__pc = lead.Race__c;
                        update existingAccount;
                        LeadConvert.setAccountId(existingAccount.Id);
                    } else {
                        LeadConvert.setDoNotCreateOpportunity(false);
                    }
                    massLeadConvert.add(LeadConvert);
                }else{
                    LeadConvert.setDoNotCreateOpportunity(false);
                }
            }
            if(!massLeadConvert.isEmpty()){
                System.debug('Check 5'+lead.Name);
                List<Database.LeadConvertResult> lcr = Database.convertLead(massLeadConvert);
                for(Database.LeadConvertResult leadConvertResult : lcr) {
                    if(leadConvertResult.isSuccess()) {
                        Contract contract = new Contract();
                        contract.Status = 'Draft';
                        contract.AccountId = leadConvertResult.getAccountId();
                        contract.StartDate = system.today();
                        contract.Description = 'New Contract';
                        insert contract;
                    }
                }
            }
        }catch(Exception e){
            //System.debug('Error: '+e);
        }
    }
}
Lead is not conveting to Contract
In lead I have four fields :- 
First Name
Last Name
Shipping First Name
Shipping Last name

While creating record we need to give input for all the fields. I need to do one automation that, when a user click on checkbox "Same as First and Last name" then Shipping First Name and Shipping last name must be automatically write the values of First name and Last name.

Summary :- 

First Name =  Test
Last Name = Check
Clicked on Same as First name and last name checkbox
Shipping first name  = test
Shipping Last name = check

Thanks
I want to cancel contract and its order.

A custom object Cancellation Request is there and it is having First name, last Name, Email, birthdate. Data will be coming from an intak form from Marketing cloud. 
When form submission will be done the data will be coming to "Cancellation Request " Obj and it will check wether any account is there or not with those same  First name, last Name, Email, birthdate. If there then it will cancel the contract and order.

There is no relation between Cancellation Request Obj and Account, Contracts and Orders. 

how we can achieve this 

Thanks

Hi,

Currently am converting Lead to Contract, while converting it creates account, Memberplan, Address record also. 

Now I need to do it in Batch so that it will convert all the leads at a single time. Batch should run according the condition of class am using currently.

How to achieve this ?

Thanks

I had created a record-triggered flow for lead. When lead is created a SMS will be sent to Lead's Mobile number. 
now it is going well and fine. 

But the SMS should go once not repeatedly. Till the lead exist the SMS goes to that lead's mobile number. How to prevent it ?
 
Hi,
I added one documents in Notes and Attachment in Account object. In the activity tab in email section when I click on send then it should send email attaching the latest document in Note & Attachment.

How we can acheive this ?
User-added image
 
In MemberPlan if a insurance is marked as primary then it should get updated in the Account lookup custom field (Insurance) with that primary marked lookup.

Primary is the custom field in MemberPlan standard object
Insurance is lookup with MemberPlan.

Any MemberPlan which is marked primary that must get displayed in the Insurance field of Account

Thanks
I have a  batch class which is running everyday and checks if the contract status is Activated or not if activated then it creates the order and its products.

1. Get the everyday logs on batch runs and number of order created and its status.

2. In case batch did not run due to any issue(exceptions)  that also needs to be notified.

Can I get any help how to achieve on this.

Thanks
Hi,

I am adding a progress indicator in one of my requirement referred from this link :- https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/components_config_for_flow_screens_stages.htm

It is not getting progress perfectly.
Green color is not coming on the first stage. I had attached a file for reference
User-added image

Thanks