1

I'm trying to find out how to compare fields values for changes. I have for custom fields that I need to know if they have changed before the user clicks on Save to run a logic before saving. This is for an custom controller. The idea is to use this for logic like these:

  1. If 4 customField__c are blank and were previously blank, hasError = true.
  2. If 1 customField__c was not previously blank and change, hasError = false.
    etc ...

I have been reading a bit about Dynamic Apex and specially the Schema.DescribeSObjectResult that it looks like the way to go. I was hopping that someone could put here a simple example to help me understand this. Thanks in advanced.!

1
//Code for save button. It will be action method for the save button.
public pagereference customSave(){
    Boolean hasError = false;

    //Below code Gives the old record values as the new values are not been saved yet
    CustomObject__c obj = [select customField1__c,customField2__c from CustomObject__c where id =:yourRecord.id];
    if((obj.customField1__c==null || obj.customField1__c=='') && (yourRecord.customField1__c==null || yourRecord.customField1__c==''))
         hasError = true;
    if(hasError){
         ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Your Error Message');
         ApexPages.addMessage(myMsg);
    }
    else
         upsert yourRecord;
}
|improve this answer|||||
1

Value changes of records can be easily managed in triggers since they have the context to compare what we need. I don't know about a way to do such a comparison with DescribeSObjectResult. As far as I know describeSObjects() returns DescribeSObjectResult typeof array which gives us the meta data information, not anything about specific records(values).

If you really need to check the field changes in a controller you can use something like @snehakem's logic.

public class ctrl{
    //Record binds with the UI
    MyObject__c editRecord = [select Id, customField1__c,customField2__c from MyObject__c where id =:yourRecord.id];

    public void saveRecord(){
        Boolean hasError = false;

        //Original record
        MyObject__c originalRecord = [select customField1__c,customField2__c from MyObject__c where id =:yourRecord.id];

        //Compare fields of original record against the record which you bind with the UI
        //I'm sorry I'm not that much clear your business logic, so add those as you need
        if(editRecord.customField1__c=='' && editRecord.customField2__c=='' && editRecord.customField3__c=='' && editRecord.customField4__c==''){
            if(originalRecord.customField1__c=='' && originalRecord.customField2__c=='' && originalRecord.customField3__c=='' && originalRecord.customField4__c==''){
                hasError = true;
                // your stuffs
                // Please add conditions as per your business since I'm not clear those
            }
        }
        if(!hasError){
            upsert editRecord; 
        }
    }
}
|improve this answer|||||
  • 1
    snehakem, highfive thank you guys. Both answers helped, but I have to say that the snehakem's approach was the best option for my intentions. – Carlos Naranjo Apr 7 '14 at 9:28

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.