You need to sign in to do that
Don't have an account?

I am facing this issue - List has more than 1 row for assignment to SObject
I am getting the below error when I am doing bulk update.
List has more than 1 row for assignment to SObject
on this line of my code -
Account acc = [SELECT Email__c FROM Account WHERE Id IN (SELECT AccountId from Opportunity WHERE Id =:Ids)];
My Apex class -
@InvocableMethod
public static void calculate(List<Id> Ids){
System.debug('--Id : '+Ids);
if(!System.isFuture()) {
CallculateCalloutAsync(Ids);
}
}
@future(callout=true)
public static void CallculateCalloutAsync(List<Id> Ids){
Account acc = [SELECT Email__c FROM Account WHERE Id IN (SELECT AccountId from Opportunity WHERE Id =:Ids)];
HTTP http = new HTTP();
HTTPRequest request = new HTTPRequest();
List<Amount__c> listCS = Amount__c.getall().values();
if (listCS != null && listCS.size() > 0) {
String endp = listCS[0].endpoint__c+acc.Email__c;
String auth = listCS[0].auth__c;
String authorizationHeader = 'Basic '+ auth;
request.setHeader ('Authorization', authorizationHeader);
request.setEndpoint (endp);
request.setMethod ('GET');
request.setHeader ('Content-Type', 'application/json; charset=utf-8');
HTTPResponse response = http.send(request);
system.debug('--response : '+response.getBody());
if(response.getStatusCode() == 200){
system.debug('The Response Body: '+response.getBody());
Map<String, Object> deserializedPayload = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
Decimal balance = (Decimal)deserializedPayload.get('tax');
Opportunity opp = new Opportunity(Id = Ids[0]);
opp.amount__c = tax;
update opp;
}
if(response.getStatusCode() != 200){
System.debug('The Response Body: '+response.getBody());
System.debug('No data found!');
}
List has more than 1 row for assignment to SObject
on this line of my code -
Account acc = [SELECT Email__c FROM Account WHERE Id IN (SELECT AccountId from Opportunity WHERE Id =:Ids)];
My Apex class -
@InvocableMethod
public static void calculate(List<Id> Ids){
System.debug('--Id : '+Ids);
if(!System.isFuture()) {
CallculateCalloutAsync(Ids);
}
}
@future(callout=true)
public static void CallculateCalloutAsync(List<Id> Ids){
Account acc = [SELECT Email__c FROM Account WHERE Id IN (SELECT AccountId from Opportunity WHERE Id =:Ids)];
HTTP http = new HTTP();
HTTPRequest request = new HTTPRequest();
List<Amount__c> listCS = Amount__c.getall().values();
if (listCS != null && listCS.size() > 0) {
String endp = listCS[0].endpoint__c+acc.Email__c;
String auth = listCS[0].auth__c;
String authorizationHeader = 'Basic '+ auth;
request.setHeader ('Authorization', authorizationHeader);
request.setEndpoint (endp);
request.setMethod ('GET');
request.setHeader ('Content-Type', 'application/json; charset=utf-8');
HTTPResponse response = http.send(request);
system.debug('--response : '+response.getBody());
if(response.getStatusCode() == 200){
system.debug('The Response Body: '+response.getBody());
Map<String, Object> deserializedPayload = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
Decimal balance = (Decimal)deserializedPayload.get('tax');
Opportunity opp = new Opportunity(Id = Ids[0]);
opp.amount__c = tax;
update opp;
}
if(response.getStatusCode() != 200){
System.debug('The Response Body: '+response.getBody());
System.debug('No data found!');
}
This means the query is returning more than 1 accounts so you have to use List<Account>
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
list<Account> acc = [SELECT Id,Email__c, (SELECT Id FROM Opportunities) FROM Account WHERE Id In : Ids];
Please, try this one.May be it will work.
If, it helps you.Pls,mark it as best answer.