0

I have the following class with a property called sorToUpdate. I want to access this property to see if a specific Id is in the property. Do I need a specific decorator to do so? I only ask that because I visited the following Link -https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_properties.htm and it seems like in order to make my properties accessible I have to put a get or set method or declare it public:

Here is my class

public with sharing class DynamicSObjectUpdater {

    Map<SObjectType, Map<Id, sObject>> sorToUpdate = new Map<SObjectType, Map<Id, sObject >>();


public SObject getSObject(ID sObjectID)
{
    SObjectType sot = sObjectID.getSobjectType();

    if(!sorToUpdate.containsKey(sot))
    {
        sorToUpdate.put(sot, new Map<Id, SObject>());
    }

    SObject targetSObject = sorToUpdate.get(sot).get(sObjectID);

    if(targetSObject == null)
    {
        targetSObject = sot.newSobject(sObjectID);

        sorToUpdate.get(sot).put(sObjectID, targetSObject);
    }
    return targetSObject;
}

public SObject getUpdateSObject(ID sObjectID, Map<String, Object> values)
{
    SObjectType sot = sObjectID.getSobjectType();

    if(!sorToUpdate.containsKey(sot))
    {
        sorToUpdate.put(sot, new Map<Id, SObject>());
    }

    SObject targetSObject = sorToUpdate.get(sot).get(sObjectID);

    if(targetSObject == null)
    {
        targetSObject = sot.newSobject(sObjectID);

        sorToUpdate.get(sot).put(sObjectID, targetSObject);
    }

    for (String field : values.keySet()) 
    {
        targetSObject.put(field, values.get(field));
    }

    return targetSObject;
}


public void updateSObjects()
{

    List<SObject> sObjectsToUpdate = new List<SObject>();
    if(sorToUpdate.size() > 0)
    {
        for(SObjectType sorType: sorToUpdate.keySet())
        {
            sObjectsToUpdate.addAll( sorToUpdate.get(sorType).values() );
        }
    }
    if(sObjectsToUpdate.size()>0) update sObjectsToUpdate;
}

}

I instantiate an object in code like this:

    DynamicSObjectUpdater sObjectUpdater = new DynamicSObjectUpdater();

And I Want to grab the property sorToUpdate

sObjectUpdater.sorToUpdate
  • You need to specify public in front of that variable – Bryan Anderson Oct 24 '19 at 13:40
  • @BryanAnderson What about the Get and Set decorators? – Matthew Metros Oct 24 '19 at 13:43
  • Unless you want to do something specific when setting or getting a variable, then you don't need to do that. You could have your variable marked as private and then have public get and set methods to access your variable – Bryan Anderson Oct 24 '19 at 13:48
0

Here is your code with my comment above, note the public access modifier to your instance variable sorToUpdate:

public with sharing class DynamicSObjectUpdater {

    public Map<SObjectType, Map<Id, sObject>> sorToUpdate = new Map<SObjectType, Map<Id, sObject >>();


public SObject getSObject(ID sObjectID)
{
    SObjectType sot = sObjectID.getSobjectType();

    if(!sorToUpdate.containsKey(sot))
    {
        sorToUpdate.put(sot, new Map<Id, SObject>());
    }

    SObject targetSObject = sorToUpdate.get(sot).get(sObjectID);

    if(targetSObject == null)
    {
        targetSObject = sot.newSobject(sObjectID);

        sorToUpdate.get(sot).put(sObjectID, targetSObject);
    }
    return targetSObject;
}

public SObject getUpdateSObject(ID sObjectID, Map<String, Object> values)
{
    SObjectType sot = sObjectID.getSobjectType();

    if(!sorToUpdate.containsKey(sot))
    {
        sorToUpdate.put(sot, new Map<Id, SObject>());
    }

    SObject targetSObject = sorToUpdate.get(sot).get(sObjectID);

    if(targetSObject == null)
    {
        targetSObject = sot.newSobject(sObjectID);

        sorToUpdate.get(sot).put(sObjectID, targetSObject);
    }

    for (String field : values.keySet()) 
    {
        targetSObject.put(field, values.get(field));
    }

    return targetSObject;
}


public void updateSObjects()
{

    List<SObject> sObjectsToUpdate = new List<SObject>();
    if(sorToUpdate.size() > 0)
    {
        for(SObjectType sorType: sorToUpdate.keySet())
        {
            sObjectsToUpdate.addAll( sorToUpdate.get(sorType).values() );
        }
    }
    if(sObjectsToUpdate.size()>0) update sObjectsToUpdate;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.