3

I'm creating my first VF page and I'm unable to upsert a new record to my object. Can someone help to review my codes for errors. Everything is fine except that it is not saving. Thanks

When the button Go is pressed, it displaces the existing first name and last name.

public Void GO()
{
    mainKey=1;
    addItemCount=1;

    wrappers=new List<TestingObjectKeyWrapper>();


    // get the first 10 Design Activities
    List<Testing_Object__c> Designs=[select ID, External_ID__c, First_Name__c,  Last_Name__c From Testing_Object__c Where First_Name__c =:First_Name_v and Last_Name__c =:Last_Name_v Limit 10];
    for (Testing_Object__c Des : Designs)
    {
        wrappers.add(new TestingObjectKeyWrapper(mainKey++, Des));
    }


}

My wrapper class:

public With Sharing class TestingObjectKeyWrapper
{
Public Integer Key{get;set;}
Public Testing_Object__c Des{get;set;}

Public TestingObjectKeyWrapper(Integer inKey, Testing_Object__C inDes)
{
    Key=inKey;
    Des=inDes;
}
}

So, the user can choose to add addition first name and last name (New record). Once pressed save, it should upsert the new record.

// save the users work
public PageReference save()
{
    PageReference result=null;
    Boolean error=false;
    List<Testing_Object__c> toUpsert=new List<Testing_Object__c>();

    // make sure that if any data has been entered, the
    // last name is present as this is required
    for (TestingObjectKeyWrapper wrapper : wrappers)
    {
        if ((String.valueof(wrapper.Des.First_Name__c) == Null) || 
            (String.valueof(wrapper.Des.Last_Name__c) == Null))
        {
            if (String.valueof(wrapper.Des.First_Name__c) == Null)
            {
                wrapper.Des.First_Name__c.addError('Required');
                error=true;
            }
            if (String.valueof(wrapper.Des.Last_Name__c) == Null)
            {
                wrapper.Des.Last_Name__c.addError('Required');
                error=true;
            }
            else
                toUpsert.add(wrapper.Des);
        }
    }

    if (!error)
    {
        delete toDelete;
        upsert toUpsert;

        // return the user to the Design Activities tab
        //result=new PageReference('/a0M/o');
    }
}

1 Answer 1

4

I corrected the formatting a little in your answer and revealed the bug...

Bug: The first if in your loop checks for either the first or last name as being null. Then proceeds to raise an error on either if null. Your else statement, aimed at adding the record to the upsert collection should there be no errors is in the wrong place, as it is only being invoked if infact either first or last name is null and the last name is not null.

Propsoed Fix: Looking at your desired behaviour you don't want any upserts to occur if any records on the page are in error, thus it does not really matter what you do or do not add to the upsert list. There is actually a way to determine if you have marked records in error through ApexPages.hasMessages.

    for (TestingObjectKeyWrapper wrapper : wrappers)
    {
         if (String.valueof(wrapper.Des.First_Name__c) == Null)
             wrapper.Des.First_Name__c.addError('Required');
         if (String.valueof(wrapper.Des.Last_Name__c) == Null)
             wrapper.Des.Last_Name__c.addError('Required');
         toUpsert.add(wrapper.Des);
    }

    if (!ApexPages.hasMessages(ApexPages.Severity.Error))
    {
        delete toDelete;
        upsert toUpsert;

        // return the user to the Design Activities tab
        // result=new PageReference('/a0M/o');
    }

Redirecting to Tabs: Also watch out when hard coding the object prefix for your Design Activities tab, as this will change from one environment to another. A better way to code this is to retrieve the prefix dynamically like so...

new PageReference('/'+Design_Activities__c.sObjectType.getDescribe().getKeyPrefix()+'/o');
2
  • 1
    getKeyPrefix() is super clean. I've been using a utility method I created years ago that basically does the same thing, with extra code that's now clearly unnecessary. Thanks for the tip.
    – Adam
    Commented Nov 9, 2013 at 20:22
  • Oh Yes!! Finally, I can see the updates in my object. Thank you very much.
    – Caky
    Commented Nov 10, 2013 at 18:45

You must log in to answer this question.

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