[SalesForce] VF and Apex: Save data to Staging Object Pending Approval

Every step is a learning process for me. I've gotten as far as extracting the data from the Master object onto a VF page for the users to edit and then do an upsert statement to save the updated records back to the Master Object.

What I would like to do is to save the updated records to a Staging object and await there for approval. The staging object is identical in field names to the Master object. How can I redirect the save to the staging object instead of the Master object? Thanks

Controller:

public class ControllerPartsUpdate {

public String OPN_v{get;set;}
public String BU_v{get;set;}
public String Root_v{get;set;}

// wrapper classes for the Master being managed
public List<MasterKey1Wrapper> MasterWrappers {get; set;}
// the unique record key master value (Design Activities)
public Integer mainKey {get; set;}

public List<SelectOption> GetOPNOptions(){

// You could populate this using a query for accounts
    List<SelectOption> OPNOptions = new List<SelectOption>{
        new SelectOption('Part1', 'Part1'),

    };

    return OPNOptions;
}

public List<SelectOption> GetBUOptions(){
// You could populate this using a query for accounts
    List<SelectOption> BUOptions = new List<SelectOption>{
        new SelectOption('BU1', 'BU1'),
    };

    return BUOptions;
}

public List<SelectOption> GetRootPartOptions(){
// You could populate this using a query for accounts
    List<SelectOption> RootPartOptions = new List<SelectOption>{
        new SelectOption('RootPart1', 'RootPart1')
    };

    return RootPartOptions;
}

public Void GO()
{
    mainKey=1;
    Masterwrappers=new List<MasterKey1Wrapper>();


    List<Catalog_Master__c> Master_Data=[select Name, Business_Unit__c,         Root_Part_Number__c  from Catalog_Master__c Where Name =:OPN_v and  Business_Unit__c=:BU_v and  Root_Part_Number__c=:Root_v Limit 5];
    for (Catalog_Master__c Master : Master_Data)
    {
        Masterwrappers.add(new MasterKey1Wrapper(mainKey++, Master));
    }   

}

// save the users changes (Stagging)
public PageReference SaveMaster()
{
    PageReference resultMaster=null;
    Boolean errorMaster=false;
    List<Catalog_Master__c> toUpsertMaster=new List<Catalog_Master__c>();

    for (MasterKey1Wrapper Masterwrapper : MasterWrappers)
    {

            if (String.valueof(Masterwrapper.Master.Name) == Null)
                MasterWrapper.Master.Name.addError('Required');

                toUpsertMaster.add(Masterwrapper.Master);

    }

    if (!ApexPages.hasMessages(ApexPages.Severity.Error))
    {

        upsert toUpsertMaster;

    }

    return resultMaster;
} 

}

Wrapper Class:

public With Sharing class MasterKey1Wrapper
{    
   Public Integer Key1{get;set;}
   Public Catalog_Master__c Master{get;set;}

   Public MasterKey1Wrapper(Integer inKey1, Catalog_Master__c inMaster)
   {
    Key1=inKey1;
    Master=inMaster;
   }
}

Visualforce Page:

<apex:page controller="ControllerPartsUpdate">

<apex:form >
    <apex:pageBlock title="Catalog Update Module" mode="edit">
        <apex:pageBlockSection title="Selections" columns="3">                
            <apex:selectList value="{!OPN_v}" label="OPN" required="True">
               <apex:selectOptions value="{!OPNOptions}"/>
            </apex:selectList>                
            <apex:selectList value="{!BU_v}" Label="Business Unit" required="true">
               <apex:selectOptions value="{!BUOptions}"/>
            </apex:selectList>                
            <apex:selectList value="{!Root_v}" Label="Root Part" required="true">
               <apex:selectOptions value="{!RootPartOptions}"/>
            </apex:selectList>               
        </apex:pageBlockSection>
    </apex:pageBlock>  

    <apex:commandButton value="Go!" action="{!Go}" rerender="out" status="status"/>

     <apex:pageBlock title="Catalog" mode="edit" id="out">
        <apex:pageBlockSection Title="Your Outputs">

            <apex:pageBlockTable value="{!MasterWrappers}" var="MasterWrap">
                <apex:column style="width:10%" headerValue="">
                  <apex:inputCheckbox value="{!MasterWrap.selected}"/>
                </apex:column>
                <apex:column style="width:25%" >
                  <apex:facet name="header">
                    <apex:outputText styleclass="requiredHeader" value="{!$ObjectType.Catalog_Master__c.fields.Name.label}" />
                  </apex:facet>
                  <apex:inputField value="{!MasterWrap.Master.Name}" required="false" />
                </apex:column>                    
                <apex:column style="width:25%" >
                  <apex:facet name="header">
                    <apex:outputText styleclass="requiredHeader" value="{!$ObjectType.Catalog_Master__c.fields.Business_Unit__c.label}" />
                  </apex:facet>
                  <apex:inputField value="{!MasterWrap.Master.Business_Unit__c}" required="false" />
                </apex:column>
                <apex:column style="width:25%" >
                  <apex:facet name="header">
                    <apex:outputText styleclass="requiredHeader" value="{!$ObjectType.Catalog_Master__c.fields.Root_Part_Number__c.label}" />
                  </apex:facet>
                  <apex:inputField value="{!MasterWrap.Master.Root_Part_Number__c}" required="false" />
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlockSection>

        <apex:commandButton value="Save" action="{!SaveMaster}" rerender="Out"/> 

    </apex:pageBlock>

</apex:form>

Best Answer

Create a record in the staging object and I am assuming you want to set it for approval once the record is created and relate it to the master.

To Do : In your savemaster() method where you upsert toUpsertMaster; check if this upsert went through by changing upsert toUpsertMaster; to Database.UpsertResult[] results = Database.upsert(toUpsertMaster);

List<staging__c> stagerecordstoinsert = new List<staging__c>();
Database.UpsertResult[] results = Database.upsert(toUpsertMaster);
for (Database.UpsertResult res : results) {
            if (res.isSuccess()) {
                if (res.isCreated()) {
                   // create your staging record
                        staging__c stage = new staging__c(
                        **Id** = res.getId());
                        stagerecordstoinsert.add(stage);
                } else {
                    System.debug('Updated record ID ' + res.getId() + '.');
                }
            }
            else {
                if (res.getErrors().size() > 0) {
                    System.debug(res.getErrors()[0].getMessage());
                }
            }
        } //outside the upserresult for loop.

        database.saveresult**[]** SR = database.insert(stagerecordstoinsert );
Related Topic