Activity Forums Salesforce® Discussions Need help with syntex

  • Need help with syntex

  • Yash Pareek

    Member
    October 24, 2021 at 6:36 am


    @isTest
    private class RemindOppyOwnersTest {
    // Dummy CRON expression: midnight on March 15.
    // Because this is a test, job executes
    // immediately after Test.stopTest().
    public static String CRON_EXP = '0 0 0 15 3 ? 2022';
    static testmethod void testScheduledJob() {
    // Create some out of date Opportunity records
    List<Opportunity> opptys = new List<Opportunity>();
    Date closeDate = Date.today().addDays(-7);
    for (Integer i=0; i<10; i++) {
    Opportunity o = new Opportunity(
    Name = 'Opportunity ' + i,
    CloseDate = closeDate,
    StageName = 'Prospecting'
    );
    opptys.add(o);
    }
    insert opptys;
    // Get the IDs of the opportunities we just inserted
    Map<Id, Opportunity> opptyMap = new Map<Id, Opportunity>(opptys);
    List<Id> opptyIds = new List<Id>(opptyMap.keySet());
    Test.startTest();
    // Schedule the test job
    String jobId = System.schedule('ScheduledApexTest',
    CRON_EXP,
    new RemindOpptyOwners());
    // Verify the scheduled job has not run yet.
    List<Task> lt = [SELECT Id
    FROM Task
    WHERE WhatId IN :opptyIds];
    System.assertEquals(0, lt.size(), 'Tasks exist before job has run');
    // Stopping the test will run the job synchronously
    Test.stopTest();
    // Now that the scheduled job has executed,
    // check that our tasks were created
    lt = [SELECT Id
    FROM Task
    WHERE WhatId IN :opptyIds];
    System.assertEquals(opptyIds.size(),
    lt.size(),
    'Tasks were not created');
    }
    }
    Hi all, need help with the highlighted syntex I am new to salesforce, keyset() return the keys in set so why and how List is used here
    Map<Id, Opportunity> opptyMap = new Map<Id, Opportunity>(opptys);
    List<Id> opptyIds = new List<Id>(opptyMap.keySet());

  • Prafull

    Member
    October 24, 2021 at 4:00 pm

    Hi yash,
    In Apex Map<Id,SObject_ApI_Name> will take each record of entered sobject api name by making id as unique key field.
    So in your code you're inserting a list of opportunities, after that adding that list into map means you're making map of opportunities with id as unique field.
    Now map keySet method returns set of id as uniqness feature of map in Apex.
    In Apex map have unique key with dupliy values. You can also use map.values() method to take list of records.
    For more info follow below offical document.
    https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_map.htm

  • Yash Pareek

    Member
    October 25, 2021 at 12:26 pm

    Hello Prafull,
    Good Evening
    Thank you so much for responding for my query. But my doubt here is that keySet() returns a set and here it is being passed in a list constructor so is is converting the set to List?
    Set<String> sStrings = oppty.Map.keySet();
    List<Id> opptyIds= = new List<Id>(sStrings);
    Is is what is really happening? A set is being converted to list?

  • Prafull

    Member
    October 31, 2021 at 3:48 am

    Hi yash,

    You can convert set to list or list to set. In your scenario you're assigning all the values of set to newly created list.

    For more information follow below article

    Convert List to Set And Set to List in Apex

    https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_list.htm

Viewing 1 - 4 of 4 posts

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos