• Pugalagiri J
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 2
    Replies
I am working on framework (triggerFactory) for change data capture (subscriped via trigger),
This framework will be generic framework and should be used by all objects when enabled for change data capture. Inorder to make it generic we are planning to create a wrapper class and include all fields of trigger.new in wrapper class.

1, In case of UPDATE, we have change fields and passing only the changed fields to wrapper class as sObject
2, In case of INSERT, we dont have a parameter to identify what all fields are inserted. Either need to wrap all fields or only wrap only inserted fields.

Is there anyway to fetch all fields in change event(trigger.new) and pass to wrapper class as sobject ?

example: (new Account created)
Event will be as below (trigger.new):

AccountchangeEvent
{
Id=000GFHuy76,
ReplayId=000AQMhhg67,
ChangeEentHeader=eventbus.ChaneEventHeader [getchangeFields=(), getCommitNum=43,.....],
Division=null,
FirstName=null,
LastName=null,
Name='TestAccount',
BillingCity=null
}

In Need to create sobject in wrapper class as below
Account:{Division=null,
FirstName=null,
LastName=null,
Name='TestAccount',
BillingCity=null}

Manually fething field one by one and passing to wrapper class is not a good solution. Is there any way all fields in event can be read and passed to wrapper ?
I am currently working on change data capture and exploring merged capture events (i.,e mutiple update actions will be merged and available in single event) refer: https://developer.salesforce.com/docs/atlas.en-us.change_data_capture.meta/change_data_capture/cdc_subscribe_merged_events.htm

When testing merge events by updating 3 records with same fields, events are merged into 1 event but 1 event have separate parameter for actual field updated and lastmodifieddate. Is it expected behaviour of change capture event ?

Example: 
1, Updating Field Status in Object Account for 3 records using DML
2, Single event created and captured in trigger
3, Trigger.new is as below

(AccountChangeEvent
{
Id=1CDRcbppo9;
getChangeFeilds=(LastModifiedDate);
Sequence=1;
getRecordIds('00012HJKu','000HDyi78','00BHFDuu7')
},
AccountChangeEvent
{
Id=1XVRcpqqG9;
getChangeFeilds=(Status);
Sequence=1;
getRecordIds('00012HJKu','000HDyi78','00BHFDuu7')
}
)

But I am expecting as below,

(AccountChangeEvent
{
Id=1CDRcbppo9;
getChangeFeilds=(Status,LastModifiedDate);
Sequence=1;
getRecordIds('00012HJKu','000HDyi78','00BHFDuu7')
}
)

Is it the expected behaviour or bug ?


 
In Change Event Trigger, in case of 'UPDATE' operation, updated/changed field names are available in ChangeEvent parameter as below

(Trigger.new).ChangeEventHeader => 
ChangeEventHeader[etChangeFields=(field1,field2,field3)]

Using this info, what all fields are updated and assocaited values can be fetched.

But in case of Change Event Trigger 'CREATE' operation, I doesn't see any tag/parameter with list of fields (Field Name) inserted.
Is there anyother we can identify list of fields which are inserted.

Note: Trigger.New have all field name and values (i.e.,null and values) but I need to know only the fields which are inserted so that those values alone can be fetched.
Is it possible to capture before field values in changes event trigger ?
As per the documentation, change event triger is asynchronous and invokes after record insert. 
We have a sceanrio to capture previous fiels values in change event trigger.
In Change Event Trigger, in case of 'UPDATE' operation, updated/changed field names are available in ChangeEvent parameter as below

(Trigger.new).ChangeEventHeader => 
ChangeEventHeader[etChangeFields=(field1,field2,field3)]

Using this info, what all fields are updated and assocaited values can be fetched.

But in case of Change Event Trigger 'CREATE' operation, I doesn't see any tag/parameter with list of fields (Field Name) inserted.
Is there anyother we can identify list of fields which are inserted.

Note: Trigger.New have all field name and values (i.e.,null and values) but I need to know only the fields which are inserted so that those values alone can be fetched.
Hi,

I have written the below classes as part of the trailhead challenge for Apex REST callouts.

The class -

public class AnimalLocator {
  
  public static String getAnimalNameById(Integer id) {
    
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+id);
    request.setMethod('GET');
    
    HttpResponse response = http.send(request);
    List<Object> animals; 
    String returnValue; 
    
    // parse the JSON response
    if (response.getStatusCode() == 200) {
      Map<String, Object> result = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
      animals = (List<Object>) result.get('animals');
      System.debug(animals);
    }
    
    if (animals.size() > 0 && animals != NULL && id < animals.size()) {
      returnValue = (String) animals.get(id);
    }
    
    return returnValue;
  } 
    
}

Mock Response Class - 

@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
     // Implement this interface method
    global HTTPResponse respond(HTTPRequest request) {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"animals": ["majestic badger", "fluffy bunny", "scary bear", "chicken", "mighty moose"]}');
        response.setStatusCode(200);
        return response; 
    }
}

Test Class - 

@isTest
private class AnimalLocatorTest{
    @isTest static void AnimalLocatorMock1() {
        Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
        string result = AnimalLocator.getAnimalNameById(3);
        String expectedResult = 'chicken';
        System.assertEquals(result,expectedResult );
    }
}

I have got 100% code coverage for the main class. But when I check the challenge I get 

Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.NullPointerException: Attempt to de-reference a null object

Please tell me if any changes to the code is required. It's really frustrating as I am stuck from past 3-4 days with this unit.

Thanks & Regards,

Abhiram Sheshadri