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
Jerry ClifftJerry Clifft 

I need to use a string list to update multiple records, but only a single record is updating.

I need to use a string list to update multiple records, but only a single record is updating.

I have this to deserialize my incomng json:

public class person {
    public String serviceID;
    public String responseStatus;
    public cls_equipmentList[] equipmentList;
}
class cls_equipmentList {
    public String action;
    public String receiverID;
    public String location;
    public String smartCardID;
    public String leaseInd;
}

I can see my data with:

        System.debug('DEBUG 3 ======== obj.equipmentList: ' + obj.equipmentList);

Which returns:

(cls_equipmentList:[
action=ADD, leaseInd=false, location=C, receiverID=R1270000274, smartCardID=S1270000274
],
cls_equipmentList:[
action=ADD, leaseInd=false, location=C, receiverID=R1270000264, smartCardID=S1270000264
])


Then I want to update each record in the equipmentList based on receiverID, but only the first record is updating.

I tired this:

    if(obj.equipmentList[0].receiverID != null){
        List<Equipment__c> allEQ = [SELECT Id, Name FROM Equipment__c WHERE Name = :obj.equipmentList[0].receiverID ];
        for (Equipment__C currentEQ : allEQ) {
            currentEQ.Action__c = 'Active';
        }
        update allEQ;
    }

All help is appreciated.
Best Answer chosen by Jerry Clifft
v varaprasadv varaprasad
Hi Jerry,

Please try once like below.I have not tested code you may get some syntactical errors.
 
list<string> receverIds = new list<string>();
	
	for(cls_equipmentList cc : obj.equipmentList){
	    if(cc.receiverID != null){
		   receverIds.add(cc.receiverID);
		}
	
	}
	
	if(receverIds != null)
	 List<Equipment__c> allEQ = [SELECT Id, Name FROM Equipment__c WHERE Name in :receverIds];
        for (Equipment__C currentEQ : allEQ) {
            currentEQ.Action__c = 'Active';
        }
        update allEQ;

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 

All Answers

v varaprasadv varaprasad
Hi Jerry,

Please try once like below.I have not tested code you may get some syntactical errors.
 
list<string> receverIds = new list<string>();
	
	for(cls_equipmentList cc : obj.equipmentList){
	    if(cc.receiverID != null){
		   receverIds.add(cc.receiverID);
		}
	
	}
	
	if(receverIds != null)
	 List<Equipment__c> allEQ = [SELECT Id, Name FROM Equipment__c WHERE Name in :receverIds];
        for (Equipment__C currentEQ : allEQ) {
            currentEQ.Action__c = 'Active';
        }
        update allEQ;

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
This was selected as the best answer
Jerry ClifftJerry Clifft
I think we are very close, right now it returns the following error:

Error: Compile Error: Variable does not exist: allEQ at line 12 column 38
Which is: "        for(Equipment__c currentEQ : allEQ){        "
 
Jerry ClifftJerry Clifft
I removed this "
if(receverIds != null)
" and now it works fine...I have no idea why, but is works. It updates all the records in the cc.receverIds.

Thanks for your help.