• Steven Keker 3
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies
New to but eagerly learning the dev side. I was able to manipulate this resource (https://developer.salesforce.com/forums?id=906F00000008yjWIAQ) for my purposes and it works to a T in sandbox. After hours of research and SF documentation, I am having trouble writing a test class in order to get this into prod. Any help is much appreciated!

We have a custom object Events. I am linking Leads to Events using a junction object. When the Lead gets converted, the junction object is automatically populated with the newly created Account. Here is the code:

trigger PopulateEventLead on Lead (after update) {
    Map<Id,Id> AccountonEL = new Map<Id, Id>();
    For(Lead L : Trigger.new)
    {
        if(L.isConverted && L.convertedAccountID !=null)
        {
            AccountonEL.put(L.iD, L.ConvertedAccountId);
        }
    }
    
    List<Event_Lead_Junction__c> fbs = [select Lead__c, Account__c from Event_Lead_Junction__c  where lead__c in :AccountonEL.keyset()];
    for (Event_Lead_Junction__c ELJ : fbs)
    {
        ELJ.Account__c = AccountonEL.get(ELJ.Lead__c);
    }
    
    update fbs;
}
 
Quite new to Apex here. I manipulated this code found on the forum to create a custom object which will be scheduled to run on the 1st of the month once I can get it to prod. I am running into an issue with the test class:
1) I have an Problem: at Line 0 -> duplicate value found: duplicates value on record with id:
2) The test class appears to save, but when I close it and reopen, all I get is a brand new class

Here is my record create code:
global class CreateParentMRR implements Database.Batchable<SObject>, Database.Stateful{
    //implement start method
    global Database.QueryLocator start(Database.BatchableContext bc) {
        //gather all Parent Accounts that will have a Parent MRR created
        String query = 'Select Id, Name, Most_Recent_Parent_MRR__c, Is_a_Parent__c from Account where Is_a_Parent__c = true';
        //check to see if the batch is being called from a test class; if record count returned by the query is over 200, the test will fail.
        if(Test.isRunningTest())
            query += 'limit 200';
        return Database.getQueryLocator(query);
    }
    //implement execute method
    global void execute(Database.BatchableContext bc, List<Account> originalrecords){
        //loop through the records from the query, create the new Parent MRRs and store them in a list
        List<Parent_MRR__c> newrecords = new List<Parent_MRR__c>();
        
        for(Account O1 :originalrecords){ //this is the loop
            Parent_MRR__c O2 = new Parent_MRR__c(); //sets up a new record to be created
            //prepopulate fields
            O2.Name = O1.Name;
            O2.Parent_Account__c = O1.ID;
            O2.Last_Month_Parent_MRR__c = O1.Most_Recent_Parent_MRR__c;
            newrecords.add(O2); //add records to the list
        }
        
        //check to see if there are any records to insert; if so, create them
        if(!newrecords.isEmpty())
            database.insert(newrecords,false);
        //newrecords is the list of records put in during the for loop. The 'false' says if one record dails to insert, still insert the rest - all of nothing
    }
    
    //implement finish method
    global void finish(Database.BatchableContext bc) {}
}


And the scheduler:
global class Schedule_CreateParentMRR implements Schedulable{
    global static void execute(SchedulableContext SC){
        //runs the batch
        CreateParentMRR CPM = new CreateParentMRR();
        ID idBatch = Database.executeBatch(CPM);
    }
}


And the test:
@isTest(SeeAllData=true)
public class Schedule_CreateProductMRR {

    static testMethod void myTest(){
        CreateProductMRR CPrM = new CreateProductMRR();
        test.startTest();
        ID idBatch = Database.executeBatch(CPrM);
        test.stopTest();
        system.assert(idBatch != null);
    }
}


Here is the resource I used: https://success.salesforce.com/answers?id=90630000000CqRCAA0


I am usually quite resourceful when it comes to figure things out but can't seem to get this one. Everything works perfect in my sandbox; frustrating I can't move it to prod. Any help is much appreciated. Thanks!
 
New to but eagerly learning the dev side. I was able to manipulate this resource (https://developer.salesforce.com/forums?id=906F00000008yjWIAQ) for my purposes and it works to a T in sandbox. After hours of research and SF documentation, I am having trouble writing a test class in order to get this into prod. Any help is much appreciated!

We have a custom object Events. I am linking Leads to Events using a junction object. When the Lead gets converted, the junction object is automatically populated with the newly created Account. Here is the code:

trigger PopulateEventLead on Lead (after update) {
    Map<Id,Id> AccountonEL = new Map<Id, Id>();
    For(Lead L : Trigger.new)
    {
        if(L.isConverted && L.convertedAccountID !=null)
        {
            AccountonEL.put(L.iD, L.ConvertedAccountId);
        }
    }
    
    List<Event_Lead_Junction__c> fbs = [select Lead__c, Account__c from Event_Lead_Junction__c  where lead__c in :AccountonEL.keyset()];
    for (Event_Lead_Junction__c ELJ : fbs)
    {
        ELJ.Account__c = AccountonEL.get(ELJ.Lead__c);
    }
    
    update fbs;
}
 
Quite new to Apex here. I manipulated this code found on the forum to create a custom object which will be scheduled to run on the 1st of the month once I can get it to prod. I am running into an issue with the test class:
1) I have an Problem: at Line 0 -> duplicate value found: duplicates value on record with id:
2) The test class appears to save, but when I close it and reopen, all I get is a brand new class

Here is my record create code:
global class CreateParentMRR implements Database.Batchable<SObject>, Database.Stateful{
    //implement start method
    global Database.QueryLocator start(Database.BatchableContext bc) {
        //gather all Parent Accounts that will have a Parent MRR created
        String query = 'Select Id, Name, Most_Recent_Parent_MRR__c, Is_a_Parent__c from Account where Is_a_Parent__c = true';
        //check to see if the batch is being called from a test class; if record count returned by the query is over 200, the test will fail.
        if(Test.isRunningTest())
            query += 'limit 200';
        return Database.getQueryLocator(query);
    }
    //implement execute method
    global void execute(Database.BatchableContext bc, List<Account> originalrecords){
        //loop through the records from the query, create the new Parent MRRs and store them in a list
        List<Parent_MRR__c> newrecords = new List<Parent_MRR__c>();
        
        for(Account O1 :originalrecords){ //this is the loop
            Parent_MRR__c O2 = new Parent_MRR__c(); //sets up a new record to be created
            //prepopulate fields
            O2.Name = O1.Name;
            O2.Parent_Account__c = O1.ID;
            O2.Last_Month_Parent_MRR__c = O1.Most_Recent_Parent_MRR__c;
            newrecords.add(O2); //add records to the list
        }
        
        //check to see if there are any records to insert; if so, create them
        if(!newrecords.isEmpty())
            database.insert(newrecords,false);
        //newrecords is the list of records put in during the for loop. The 'false' says if one record dails to insert, still insert the rest - all of nothing
    }
    
    //implement finish method
    global void finish(Database.BatchableContext bc) {}
}


And the scheduler:
global class Schedule_CreateParentMRR implements Schedulable{
    global static void execute(SchedulableContext SC){
        //runs the batch
        CreateParentMRR CPM = new CreateParentMRR();
        ID idBatch = Database.executeBatch(CPM);
    }
}


And the test:
@isTest(SeeAllData=true)
public class Schedule_CreateProductMRR {

    static testMethod void myTest(){
        CreateProductMRR CPrM = new CreateProductMRR();
        test.startTest();
        ID idBatch = Database.executeBatch(CPrM);
        test.stopTest();
        system.assert(idBatch != null);
    }
}


Here is the resource I used: https://success.salesforce.com/answers?id=90630000000CqRCAA0


I am usually quite resourceful when it comes to figure things out but can't seem to get this one. Everything works perfect in my sandbox; frustrating I can't move it to prod. Any help is much appreciated. Thanks!
 

Hello,

 

I have a problem... I have a custom object that has a lookup to the Leads object.  (Leads >> Feedback)

 

Upon converting the lead, I would like the like the feedback object to attach to the newly created opportunity.  How would I attempt to do this? The feedback object also has a lookup to an opportunity.  Any help is appreciated.

 

There's a similar thread here but doesn't help much: http://boards.developerforce.com/t5/General-Development/Converting-Lead-lookup-relationship-w-custom-object/m-p/69891

 

Thank you-

 

Kenny