function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RPKRPK 

Create trigger and generate XML

I want to create a trigger when a new record is inserted. When this trigger is fired, it should create an XML file of that record.
Amit Shingavi 8Amit Shingavi 8
Hey RPK,

There is no standard way to convert sObject to XML, You need to write a custom logic to prepare this file. Follow below steps -
1.Iterate over Trigger.new
2.Convert a sObject to Map<String,Object> by serializing and deserializing the sObject record from Trigger.new list
/* @param sObjInstance : is one of the instance of sObject from the Trigger.new*/
Map<String,Object> objMap = (Map<String,Object>)JSON.deserializeUntyped(JSON.serialize(sObjInstance));
system.debug('objMap====\n'+objMap);
3.Now iterate over objMap and create a XML String
4.Insert the attachment assigning parentId as sObject instance's Id and set a body as Blob.valueOf(<xmlString>)

Do let me know if you need any more help.

Thanks,
Amit Shingavi


 
RPKRPK
Alternatively, how to add the new record values to a LIST object and return this LIST to an APEX class method as parameter?
Amit Shingavi 8Amit Shingavi 8
Can you elaborate your requirement here.

Whenever you will use trigger, the Trigger.new will give you the LIST with manipulated data.
RPKRPK
The requirement is that we are creating a .NET middleware. The .NET REST API will be consumed in Salesforce.

Whenever any record is changed or added in Salesforce, the updated record should be sent to our .NET API as XML.

As you said there is no straight-forward way to create XML, so I am thinking of a Trigger based approach.

The Trigger will return List object to an APEX class. This APEX class will invoke the API and pass LIST object as parameter to the API.

The XML conversion will be done in .NET itself.

Please advise.
DevADSDevADS
It would be better if you keep logic for XML conversion in Salesforce because if you do it through APIs then It will impact on performance and It is not a necessary callout.

Prepare HTTP Request at Salesforce end and only invoke API call for service integration.

How to invoke APIs from Salesforce?
=> Checkout @future annotation in Trigger.

Happy Coding!
 
kadiam Raju imanuelkadiam Raju imanuel
Hi Amit,

Can you please help me with iterating ObjMap and forming XMLString.

Thanks,