Apex 从触发器中提取字段以创建自定义 JSON
Apex extracting fields from a trigger to create a custom JSON
我已经为订单对象创建了一个触发器,只希望从该对象中提取特定字段以创建自定义 JSON 数据结构。我已经尝试将我需要的字段从对象保存到局部变量中,将它们放入列表中然后序列化它们但输出是错误的。缺少大括号,并且没有在正确的位置添加逗号。请在下面参考我的代码。
trigger ActiveOrder on Order (after insert, after update)
{
private String ordAccountId;
private String ordBillingStreet;
private String ordBillingCity;
private String ordBillingState;
private String ordBillingPostalCode;
private String ordBillingCountry;
private String ordDescription;
private Double ordTotalAmount;
private String ordOrderNumber;
private String ordShipToContact;
private String ordShipToMethod;
private String ordShippingStreet;
private String ordShippingCity;
private String ordShippingState;
private String ordShippingPostalCode;
private String ordShippingCountry;
private String ordBillToAttn;
private String ordCustomerPO;
private String ordShipToAttn;
for(Order o : Trigger.New)
{
If(o.Status == 'Activated')
{
ordAccountId = o.AccountId;
ordBillingStreet = o.BillingStreet;
ordBillingCity = o.BillingCity;
ordBillingState = o.BillingState;
ordBillingPostalCode = o.BillingPostalCode;
ordBillingCountry = o.BillingCountry;
ordDescription = o.Description;
ordTotalAmount = o.TotalAmount;
ordOrderNumber = o.OrderNumber;
ordShipToContact = o.Ship_To_Name__c;
ordShippingStreet = o.ShippingStreet;
ordShippingCity = o.ShippingCity;
ordShippingState = o.ShippingState;
ordShippingPostalCode = o.ShippingPostalCode;
ordShippingCountry = o.ShippingCountry;
ordBillToAttn = o.BillToAttn__c;
ordCustomerPO = o.Customer_PO__c;
ordShipToAttn = o.Ship_to_Attention__c;
List<String> ordInvoice = new List<String>();
ordInvoice.add('Account Id: ' + ordAccountId + ', '
+ 'Billing Street: ' + ordBillingStreet + ', '
+ 'Billing City: ' + ordBillingCity + ', '
+ 'Billing State: ' + ordBillingState + ', '
+ 'Billing Postal Code: ' + ordBillingPostalCode + ', '
+ 'Billing Country: ' + ordBillingCountry + ', '
+ 'Description: ' + ordDescription + ', '
+ 'Total Amount: ' + ordTotalAmount + ', '
+ 'Order Number: ' + ordOrderNumber + ', '
+ 'Ship To Contact: ' + ordShipToContact + ', '
+ 'Shippin Street: ' + ordShippingStreet + ', '
+ 'Shipping City: ' + ordShippingCity + ', '
+ 'Shipping State: ' + ordShippingState + ', '
+ 'Shipping Postal Code: ' + ordShippingPostalCode + ', '
+ 'Shipping Country: ' + ordShippingCountry + ', '
+ 'Bill To Attn: ' + ordBillToAttn + ', '
+ 'Customer PO: ' + ordCustomerPO + ', '
+ 'Ship To Attn: ' + ordShipToAttn);
System.debug('Pre JSON: ' + ordInvoice);
String ordInvoiceJSON = JSON.serialize(ordInvoice);
System.debug('Post JSON: ' + ordInvoice);
System.debug('Post JSON: ' + ordInvoicePostJSON);
}
}
}
您正在序列化一个具有一个值的列表,该值是一个长字符串,其中包含您的所有数据您几乎已经自己序列化了订单。在您的情况下,您应该尝试使用 Map 来构建您的对象,然后对其进行序列化。例如
Map<String,String> orderMap = new Map<String,String>();
orderMap.put('Account Id', o.accountId);
orderMap.put('Billing Street',o.billingStreet);
//...etc
String jsonStr = JSON.serialize(orderMap);
System.debug(jsonStr);
试一试,或者如果您只想让所有字段都具有值和更少的代码,您可以使用方法 sObject.getPopulatedFieldsAsMap()。可以像 JSON.serialize(o.getPopulatedFieldsAsMap());
一样简单
希望对您有所帮助
我已经为订单对象创建了一个触发器,只希望从该对象中提取特定字段以创建自定义 JSON 数据结构。我已经尝试将我需要的字段从对象保存到局部变量中,将它们放入列表中然后序列化它们但输出是错误的。缺少大括号,并且没有在正确的位置添加逗号。请在下面参考我的代码。
trigger ActiveOrder on Order (after insert, after update)
{
private String ordAccountId;
private String ordBillingStreet;
private String ordBillingCity;
private String ordBillingState;
private String ordBillingPostalCode;
private String ordBillingCountry;
private String ordDescription;
private Double ordTotalAmount;
private String ordOrderNumber;
private String ordShipToContact;
private String ordShipToMethod;
private String ordShippingStreet;
private String ordShippingCity;
private String ordShippingState;
private String ordShippingPostalCode;
private String ordShippingCountry;
private String ordBillToAttn;
private String ordCustomerPO;
private String ordShipToAttn;
for(Order o : Trigger.New)
{
If(o.Status == 'Activated')
{
ordAccountId = o.AccountId;
ordBillingStreet = o.BillingStreet;
ordBillingCity = o.BillingCity;
ordBillingState = o.BillingState;
ordBillingPostalCode = o.BillingPostalCode;
ordBillingCountry = o.BillingCountry;
ordDescription = o.Description;
ordTotalAmount = o.TotalAmount;
ordOrderNumber = o.OrderNumber;
ordShipToContact = o.Ship_To_Name__c;
ordShippingStreet = o.ShippingStreet;
ordShippingCity = o.ShippingCity;
ordShippingState = o.ShippingState;
ordShippingPostalCode = o.ShippingPostalCode;
ordShippingCountry = o.ShippingCountry;
ordBillToAttn = o.BillToAttn__c;
ordCustomerPO = o.Customer_PO__c;
ordShipToAttn = o.Ship_to_Attention__c;
List<String> ordInvoice = new List<String>();
ordInvoice.add('Account Id: ' + ordAccountId + ', '
+ 'Billing Street: ' + ordBillingStreet + ', '
+ 'Billing City: ' + ordBillingCity + ', '
+ 'Billing State: ' + ordBillingState + ', '
+ 'Billing Postal Code: ' + ordBillingPostalCode + ', '
+ 'Billing Country: ' + ordBillingCountry + ', '
+ 'Description: ' + ordDescription + ', '
+ 'Total Amount: ' + ordTotalAmount + ', '
+ 'Order Number: ' + ordOrderNumber + ', '
+ 'Ship To Contact: ' + ordShipToContact + ', '
+ 'Shippin Street: ' + ordShippingStreet + ', '
+ 'Shipping City: ' + ordShippingCity + ', '
+ 'Shipping State: ' + ordShippingState + ', '
+ 'Shipping Postal Code: ' + ordShippingPostalCode + ', '
+ 'Shipping Country: ' + ordShippingCountry + ', '
+ 'Bill To Attn: ' + ordBillToAttn + ', '
+ 'Customer PO: ' + ordCustomerPO + ', '
+ 'Ship To Attn: ' + ordShipToAttn);
System.debug('Pre JSON: ' + ordInvoice);
String ordInvoiceJSON = JSON.serialize(ordInvoice);
System.debug('Post JSON: ' + ordInvoice);
System.debug('Post JSON: ' + ordInvoicePostJSON);
}
}
}
您正在序列化一个具有一个值的列表,该值是一个长字符串,其中包含您的所有数据您几乎已经自己序列化了订单。在您的情况下,您应该尝试使用 Map 来构建您的对象,然后对其进行序列化。例如
Map<String,String> orderMap = new Map<String,String>();
orderMap.put('Account Id', o.accountId);
orderMap.put('Billing Street',o.billingStreet);
//...etc
String jsonStr = JSON.serialize(orderMap);
System.debug(jsonStr);
试一试,或者如果您只想让所有字段都具有值和更少的代码,您可以使用方法 sObject.getPopulatedFieldsAsMap()。可以像 JSON.serialize(o.getPopulatedFieldsAsMap());
希望对您有所帮助