Newer Version Available

This content describes an older version of this product. View Latest

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.
    1// Get the TPM_Promotion instance
    2TPM_Promotion promotion = (TPM_Promotion) args.get('promotion');
    3
    4// Update the Promotion__c SObject field
    5Promotion__c myPromo = (Promotion__c) promotion.getPromotion().getRecord();
    6myPromo.Slogan_Language_1__c = 'My New Slogan';
    7
    8// Update the First Tactic__c Group Text
    9Tactic__c myTactic = (Tactic__c) promotion.getTactics()[0].getRecord();
    10myTactic.Group_Text__c = 'My New group text';
  2. Delete a Managed Package SObject record.
    1// Get the TPM_Promotion instance
    2TPM_Promotion promotion = (TPM_Promotion) args.get('promotion');
    3
    4// Delete the first Tactic__c SObject
    5TPM_Promotion.Record tacticRecordWrapper = promotion.getTactics()[0];
    6if (!tacticRecordWrapper.isDeleted()) { // We can also check if the record is new with isNew()
    7    tacticRecordWrapper.setIsDeleted(true);
    8}
  3. Add custom SObject to Promotion Save.
    You can add additional SObjects to be saved during the promotion SObjects database commit.
    1// Get the TPM_Promotion instance
    2TPM_Promotion promotion = (TPM_Promotion) args.get('Promotion');
    3
    4// Append a new SObject to be saved
    5MyCustomSObject__c mySObject = new MyCustomSObject__c();
    6TPM_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.
    1// Get the TPM_Promotion instance
    2TPM_Promotion promotion = (TPM_Promotion) args.get('Promotion');
    3
    4// Append a new SObject to be saved
    5MyCustomSObject__c mySObject = new MyCustomSObject__c();
    6TPM_Promotion.Record myRecordWrapper = promotion.append(mySObject);
    7
    8// Add relationship to the Promotion__c record
    9promotion.addRelationship(
    10    myRecordWrapper,
    11    MyCustomSObject__c.Promotion__c,
    12    promotion.getPromotion()
    13);
    14
    15// Append an additional Child SObject
    16MyChildSObject__c myChildSObject = new MyChildSObject__c();
    17TPM_Promotion.Record myChildRecordWrapper = promotion.append(myChildSObject);
    18
    19// Relate the child object to our custom object
    20promotion.addRelationship(
    21    myChildRecordWrapper,
    22    MyChildSObject__c.MyCustomSObject__c,
    23    myRecordWrapper
    24);
  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.

    This state is serialized. Deserialize it before being able to access the data.

    Note

    1// Get the TPM_Promotion instance
    2TPM_Promotion promotion = (TPM_Promotion) args.get('promotion');
    3
    4// Get the custom data serialized data
    5String payloadString = (String) args.get('customState');
    6
    7// Deserialize it to be able to access the data
    8MyCustomStructure payload = (MyCustomStructure) JSON.deserialize(payloadString, MyCustomStructure.class);
    9
    10// Add the retrieved records to be saved on the promotion transaction
    11if (payload.myRecords != null) {
    12    for (MyRecord__c mr: payload.myRecords) {
    13        // Add the sobject record to the list of objects to insert or update
    14        TPM_Promotion.Record = wrapper = promotion.append(mr);
    15        // Relate it to the promotion (if needed)
    16        promotion.addRelationship(
    17            wrapper,
    18            MyRecord__c.Promotion__c,
    19            promotion.getPromotion()
    20        );
    21    }
    22}