---
schema_version: "1"
id: 14c64a39dc5b145e86c2ed442bba988dc0592c2586b3ac9b6e22d0802b740efd
title: Apex Save Process Customization
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/retail_api/apex_save_customization.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.retail_api.meta/retail_api/apex_save_customization.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: ab03ba9dca878d1ba0842892af424b53de1130120c9df138e45e9cdf5534d70c
description: This section provides information on the different workflow process to customize the save process using APEX calls.
---
# Apex Save Process Customization

This section provides information on the different workflow process to customize the save process using APEX calls.

1. Change a Managed Package SObject field value.
    
    // Get the TPM\_Promotion instance
    TPM\_Promotion promotion = (TPM\_Promotion) args.get('promotion');
    
    // Update the Promotion\_\_c SObject field
    Promotion\_\_c myPromo = (Promotion\_\_c) promotion.getPromotion().getRecord();
    myPromo.Slogan\_Language\_1\_\_c = 'My New Slogan';
    
    // Update the First Tactic\_\_c Group Text
    Tactic\_\_c myTactic = (Tactic\_\_c) promotion.getTactics()\[0\].getRecord();
    myTactic.Group\_Text\_\_c = 'My New group text';
2. Delete a Managed Package SObject record.
    
    // Get the TPM\_Promotion instance
    TPM\_Promotion promotion = (TPM\_Promotion) args.get('promotion');
    
    // Delete the first Tactic\_\_c SObject
    TPM\_Promotion.Record tacticRecordWrapper = promotion.getTactics()\[0\];
    if (!tacticRecordWrapper.isDeleted()) { // We can also check if the record is new with isNew()
        tacticRecordWrapper.setIsDeleted(true);
    }
3. Add custom SObject to Promotion Save.
    
    You can add additional SObjects to be saved during the promotion SObjects database commit.
    
    // Get the TPM\_Promotion instance
    TPM\_Promotion promotion = (TPM\_Promotion) args.get('Promotion');
    
    // Append a new SObject to be saved
    MyCustomSObject\_\_c mySObject = new MyCustomSObject\_\_c();
    TPM\_Promotion.Record myRecordWrapper = promotion.append(mySObject);
    
    The TPM\_Promotion class ensures all promotion related SObjects are committed on an all or none fashion while encapsulating all operations in a transaction. Since the hook will be called from multiple places in multiple TPM processes, the best practice is to use the provided methods for handling custom records and relationships to ensure no incomplete or invalid data is committed. The SObject write order for your custom sobject records are determined by the order of the append calls.
4. Add custom SObject to Save with Relationship.
    
    // Get the TPM\_Promotion instance
    TPM\_Promotion promotion = (TPM\_Promotion) args.get('Promotion');
    
    // Append a new SObject to be saved
    MyCustomSObject\_\_c mySObject = new MyCustomSObject\_\_c();
    TPM\_Promotion.Record myRecordWrapper = promotion.append(mySObject);
    
    // Add relationship to the Promotion\_\_c record
    promotion.addRelationship(
        myRecordWrapper,
        MyCustomSObject\_\_c.Promotion\_\_c,
        promotion.getPromotion()
    );
    
    // Append an additional Child SObject
    MyChildSObject\_\_c myChildSObject = new MyChildSObject\_\_c();
    TPM\_Promotion.Record myChildRecordWrapper = promotion.append(myChildSObject);
    
    // Relate the child object to our custom object
    promotion.addRelationship(
        myChildRecordWrapper,
        MyChildSObject\_\_c.MyCustomSObject\_\_c,
        myRecordWrapper
    );
5. Save Custom Data in TPM UI.
    
    Save the custom data that was sent for save along with the Promotion data.In your callable class, you can access the custom state that was set as part of the promotion state.
    
    ![Note](https://developer.salesforce.com/docs/atlas.en-us.retail_api.meta/retail_api/images/icon_note.png)
    
    #### Note
    
    This state is serialized. Deserialize it before being able to access the data.
    
    // Get the TPM\_Promotion instance
    TPM\_Promotion promotion = (TPM\_Promotion) args.get('promotion');
    
    // Get the custom data serialized data
    String payloadString = (String) args.get('customState');
    
    // Deserialize it to be able to access the data
    MyCustomStructure payload = (MyCustomStructure) JSON.deserialize(payloadString, MyCustomStructure.class);
    
    // Add the retrieved records to be saved on the promotion transaction
    if (payload.myRecords != null) {
        for (MyRecord\_\_c mr: payload.myRecords) {
            // Add the sobject record to the list of objects to insert or update
            TPM\_Promotion.Record = wrapper = promotion.append(mr);
            // Relate it to the promotion (if needed)
            promotion.addRelationship(
                wrapper,
                MyRecord\_\_c.Promotion\_\_c,
                promotion.getPromotion()
            );
        }
    }