-3

I have less experience in coding. I have a requirement to send Salesforce record to External system whenever we create a record in SF, create an incident in External system. I wrote a class and a trigger and calling the method in trigger from class to POST. When I execute the code in anonymous window , it throws me error "System.QueryException: List has more than 1 row for assignment to SObject". The code is executing in trigger when i call method however it is passing only one record to external system not the one which we create in salesforce. Any help is appreciated. I think I'm doing something wrong on my Soql query because it is not picking new Record and loops on with one record in salesforce. Can someone help me correct this? I know code is little messy. Posting both apex class and trigger.Thank you!

public class RadarUpdate {
@future (callout=true)
public static void postcallout(string id) {   
Patient_Satisfaction__c c = [select id, Name, Reporter_First_Name__c, 
Reporter_Last_Name__c, Reporter_Phone__c, Description_of_Feedback__c from 
Patient_Satisfaction__c limit 1] ;  // working when Limit 1 clause is added //
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeObjectField('First name ', c.Reporter_First_Name__c);   //Salesforce to External system
gen.writeObjectField('Last name',c.Reporter_Last_Name__c);       //Salesforce to External system
gen.writeObjectField('Phone',c.Reporter_Phone__c);               //Salesforce to External system
gen.writeObjectField('description',c.Description_of_Feedback__c);//Salesforce to External system
gen.writeObjectField('Name',c.Name);         //External system required field
gen.writeObjectField('incident_group_id',7387);//External system requiredfield
gen.writeEndObject();
String jsonS = gen.getAsString(); 
System.debug('jsonMaterials'+jsonS);
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.radarfirst.com/incidents');
request.setMethod('POST');
request.setHeader('Content-Type','application/json;charset=UTF-8');
request.setHeader('User-agent', 'Salesforce-integration-client');
request.setHeader('Authorization','Bearer 123');
request.setBody(jsonS);
// Set the body as a JSON object
HttpResponse response = http.send(request);
if (response.getStatusCode() != 201) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
}
}
}


trigger RadarFirst on Patient_Satisfaction__c (before insert, before update) {
for (Patient_Satisfaction__c cc : Trigger.new) {
if (cc.Referred_to_Privacy__c = true) {
RadarUpdate.postcallout(cc.id);
}
}
}
  • Please explain more what you mean by "I'm not able to conctenate Salesforce fields along with external system required field in Json format. ". – Keith C Apr 9 at 10:49
  • I can see that the list 'PrIds' is empty and is being used in the WHERE condition of the SOQL used to populate the second list 'PrRecord'. You are then running a for loop on this second list which is empty. I guess if you fixed, then your JSON should come out as expected. – arut Apr 9 at 11:11
  • Thank you for your replies. The json is not coming out as expected jsonMaterials{ "Patient satisfaction" : [ ], "incident_group_id" : 7387, "Name" : "TestAPI" }. Patienrt satisfaction is empty. Not sure where exactly the problem is – Salman Shaik Apr 9 at 15:09
  • Review queue: this is not a JSON problem. It's an Apex logic issue. – David Reed Apr 12 at 4:12
0
public static void postcallout(string id) {   
Patient_Satisfaction__c c = [select id, Name, Reporter_First_Name__c, 
Reporter_Last_Name__c, Reporter_Phone__c, Description_of_Feedback__c from 
Patient_Satisfaction__c limit 1] ;  // working when Limit 1 clause is added //

Your method takes an Id, which should by typed as an Id, that is, Id recordId, but it doesn't use it. You need to write a filter in your query to select a specific record by matching its Id. You are currently querying some arbitrary record that has no connection to the requested Id.

The exception you're seeing is a very common one that is thrown any time a SOQL query is assigned to an sObject variable but does not return exactly one result. If you search SFSE, you'll find numerous clear examples and explanations of this issue.

The Trailhead module Apex Basics and Database demonstrates how to write filtered SOQL queries using Apex variables.

| improve this answer | |
  • Thank you, it still throws me system exception error. I guess I'm doing something wrong in filtering SOQL query. I want to pull the latest inserted record and insert it in to external system. I will go through SFSE to understand more about this. – Salman Shaik Apr 10 at 7:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.