• Frank van Meegen 13
  • NEWBIE
  • 30 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 7
    Replies
Hi all,

I have been struggling to insert a list of records in Salesforce via APEX with populating a lookup field. Each item in the list will have a different parent. How can I map the Id of the specific parent to each child item without doing a SOQL for each child item?

I have a retrieved a list of values from a JSON string called strResponse and parse it via a JSON2APEX class. This list will have 2 items Code and Value.

The code value is also available as an external Id on the parent object ParentObject__c.Code__c that is related via ChildObject__c .ParentObject__c

If I insert the following code I get an error that the lookup field does not contain a valid Id. How can I populate the ParentObject__c  field with an Id of the matching parent record?
ChildObject__c [] Childs = new List<ChildObject__c>();
String strResponse = httpResponse.getBody();
List<JSON2Apex>  delegationMap = (List<JSON2Apex>)JSON.deserialize(strResponse, List<JSON2Apex>.class);
                
                for(JSON2Apex cp : delegationMap) {                 
         
                    ChildObject__c Child = new ChildObject__c (ParentObject__c = cp.Code,                                        
                                                               ValueField__c = cp.Value);                 
                 
                    Childs.add(Child);                  
                    
                }
                insert Childs;

 
For callouts to an external app from Salesforce I need to add tokens to the REST endpoint url.
 
request.setEndpoint('https://www.externalapp.com/?token=XXXXXXXX&software_token=YYYYYYYY');

I was wondering what the best practice is to store these tokens in Salesforce. These tokens will be different for every salesforce instance that will use the code to callout to this external app. Also I am curious how I should change my code to retrieve the stored tokens.
With the following class I have managed to succesfully create and post a JSON string from Salesforce to an external system:
 
public class WorkOrderAppPost {
  @future (callout=true)
    public static void postWorkOrder() {
Case c = [SELECT Id,
CaseNumber,
type,
WorkorderApp__Payment_Method__c,
account.name,
account.id,
account.ShippingStreet,
account.ShippingPostalCode,
account.ShippingCity,
account.BillingStreet,
account.BillingPostalCode,
account.BillingCity,
contact.id,
Contact.Name
 from Case Limit 1] ;  
    JSONGenerator gen = JSON.createGenerator(true);    
	gen.writeStartObject();      
	gen.writeStringField('WorkorderNo', c.Id);
	gen.writeStringField('ExternProjectNr',c.CaseNumber);
	gen.writeStringField('CustomerName',c.account.name);
	gen.writeStringField('CustomerDebtorNr',c.account.id);
	gen.writeStringField('CustomerStreet',c.account.ShippingStreet);
 	gen.writeStringField('CustomerZIP',c.account.ShippingPostalCode);
	gen.writeStringField('CustomerCity',c.account.ShippingCity);
	gen.writeStringField('CustomerContactPerson',c.Contact.Name);
	gen.writeStringField('CustomerNameInvoice',c.account.name);
	gen.writeStringField('CustomerDebtorNrInvoice',c.account.id);
	gen.writeStringField('CustomerStreetInvoice',c.account.BillingStreet);
	gen.writeStringField('CustomerZIPInvoice',c.account.BillingPostalCode);
    gen.writeStringField('CustomerCityInvoice',c.account.BillingCity);
	gen.writeStringField('CustomerContactPersonInvoice',c.Contact.Name);
	gen.writeStringField('TypeOfWork',c.type);
	gen.writeStringField('PaymentMethod',c.WorkorderApp__Payment_Method__c);        
	gen.writeEndObject();    
	String jsonS = gen.getAsString();
System.debug('jsonMaterials'+jsonS);

// Sending the http body with JSON 
        
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://www.externalsystem.com');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setbody(jsonS);
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() != 200) {
    System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
} else {
    System.debug(response.getBody());

}

        
        
    }
}
The external system responds with the following code when succesfull:
 
{"code":200,"messages":[],"response":[{"workorder_no":"5000Y00000FtqLDQAZ","row_id":597665}]}

I would like to parse the row_id value (597665) into the case record that has been posted to the external system in the field "WorkorderApp__WorkOrder_Id__c" How can I achieve this goal?
Thanks to the Trailhead module regarding APEX Rest callout I know how to post "mighty moose" to an endpoint ;-)

I am wondering how I can create and send a JSON string based on Salesforce fields. For example I want to send a case to an external system via a REST Post method with the following field mapping:
Ticket Number = CaseNumber
Ticket Status = Status
Ticket Priority = Priority

How would I change the request.setBody('{"name":"mighty moose"}'); line to succesfully generate the required JSON code for the external system?
Hi all,

I have been struggling to insert a list of records in Salesforce via APEX with populating a lookup field. Each item in the list will have a different parent. How can I map the Id of the specific parent to each child item without doing a SOQL for each child item?

I have a retrieved a list of values from a JSON string called strResponse and parse it via a JSON2APEX class. This list will have 2 items Code and Value.

The code value is also available as an external Id on the parent object ParentObject__c.Code__c that is related via ChildObject__c .ParentObject__c

If I insert the following code I get an error that the lookup field does not contain a valid Id. How can I populate the ParentObject__c  field with an Id of the matching parent record?
ChildObject__c [] Childs = new List<ChildObject__c>();
String strResponse = httpResponse.getBody();
List<JSON2Apex>  delegationMap = (List<JSON2Apex>)JSON.deserialize(strResponse, List<JSON2Apex>.class);
                
                for(JSON2Apex cp : delegationMap) {                 
         
                    ChildObject__c Child = new ChildObject__c (ParentObject__c = cp.Code,                                        
                                                               ValueField__c = cp.Value);                 
                 
                    Childs.add(Child);                  
                    
                }
                insert Childs;

 
For callouts to an external app from Salesforce I need to add tokens to the REST endpoint url.
 
request.setEndpoint('https://www.externalapp.com/?token=XXXXXXXX&software_token=YYYYYYYY');

I was wondering what the best practice is to store these tokens in Salesforce. These tokens will be different for every salesforce instance that will use the code to callout to this external app. Also I am curious how I should change my code to retrieve the stored tokens.
Thanks to the Trailhead module regarding APEX Rest callout I know how to post "mighty moose" to an endpoint ;-)

I am wondering how I can create and send a JSON string based on Salesforce fields. For example I want to send a case to an external system via a REST Post method with the following field mapping:
Ticket Number = CaseNumber
Ticket Status = Status
Ticket Priority = Priority

How would I change the request.setBody('{"name":"mighty moose"}'); line to succesfully generate the required JSON code for the external system?