0

So my code basically looks like below

public with sharing class test_exists {

    Public Map<Id, Case> cases;

    public static void start_creation(Map<String, Account> acc){
        cases = New Map<Id, Case>();
        for (String s: acc.keyset()){
            Account c_acc = acc.get(s);
            Case new_case = new Case(AccountId = c_acc.Id, Name=c_acc.Name);
            cases.put(c_acc.Id, new_case);
        }
    }
}

I'm getting two errors

  1. Variable does not exist: cases

  2. Method does not exist or incorrect signature: void.put - and I've chalked this up to cases not actually being initialized and thus it's still null.

I've also tried

public with sharing class test_exists {

    Public Map<Id, Case> cases{get;set;}

    public test_exists(){
        cases = New Map<Id, Case>();
    }

    public static void start_creation(Map<String, Account> acc){
        for (String s: acc.keyset()){
            Account c_acc = acc.get(s);
            Case new_case = new Case(AccountId = c_acc.Id, Name=c_acc.Name);
            cases.put(c_acc.Id, new_case);
        }
    }
}

but I'm still getting the same error of "Method does not exist or incorrect signature: void.put"

Might the fact that I'm calling New within the constructor be the source of the problem? I've done similar things such as assigning to a previously declared variable without problems.

Edit:

The class above is being called from a trigger which has access to a bunch of other classes.

I removed the static keyword as suggested below and now from within the trigger code there is an error of 'Non static method cannot be referenced from a static context: void test_exists.start_creation(Map)'

Does that mean that acc as is being passed in has to be a static?

2
  • You seem to have an incomplete understanding of what the static keyword actually does. Without further explanation of what you're trying to accomplish with this class of yours, I doubt that a static method is what you want. Please go into more detail about what exactly it is you're trying to accomplish in your trigger.
    – Derek F
    Commented Nov 17, 2017 at 14:55
  • @user49458, if the below answer helped solve your original question, please consider accepting it and open new post for any follow up questions. Thanks
    – glls
    Commented Nov 25, 2017 at 18:26

1 Answer 1

4

Remove static keyword from your function as non-static properties cannot be used in a static method.


update for additional question.

Change your code so that it returns a map:

public with sharing class test_exists {

    public static void start_creation(Map<String, Account> acc){
        Map<Id, Case> cases = New Map<Id, Case>();
        for (String s: acc.keyset()){
            Account c_acc = acc.get(s);
            Case new_case = new Case(AccountId = c_acc.Id, Name=c_acc.Name);
            cases.put(c_acc.Id, new_case);
        }
        return cases;
    }
}

It may be static or not, it would only change how the method is being called.

2
  • thank you for your help! I've updated my question as it led me to another problem and I think I didn't give enough context initially.
    – IanQ
    Commented Nov 17, 2017 at 14:42
  • 1
    Be aware that changing the question changes the context. kindly avoid asking multiple questions in single post. Refer to updated answer.
    – Raul
    Commented Nov 17, 2017 at 14:51

You must log in to answer this question.

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