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
$force$force 

code to be simplifeid

hi can any on help me in simplifying the code.

 

iam using list and doing some updation on a custom object.
now iwant use map instead of list .can any one simplify it.support bulk operations also.

 

trigger doc on federal__c(after insert)

 

Set<Id> sobjectSetOfIds = new Set<Id>();
List<federal__c> flist = new List<federal__c>();
 for(federal__c fs:trigger.new) {
    if(fs.Opportunity__c!=null)
     sobjectSetOfIds.add(fs.Opportunity__c);    
       flist.add(fs);
 }
 list<Conditions__c> comlist = [select id,opportunity__c,Contact__c,Federal__c from Conditions__c where opportunity__c =:sobjectSetOfIds];
 list<Conditions__c> updatedcomlist = new list<Conditions__c>();
 for(Federal__c fss :flist) {
    for(Conditions__c cm: comlist) {     
            cm.federal__c=fss.id;         
            updatedcomlist.add(cm);
        }
    }
    upsert updatedcomlist;
}

 

 

 

Thanks in advance.

JitendraJitendra

Hi,

 

Only change in your code is to use the IN clause. Please find your code:

 

trigger doc on federal__c(after insert)
{
	Set<Id> sobjectSetOfIds = new Set<Id>();
	List<federal__c> flist = new List<federal__c>();
	
	 for(federal__c fs:trigger.new) {
		if(fs.Opportunity__c!=null)
		 sobjectSetOfIds.add(fs.Opportunity__c);    
		 flist.add(fs);
	 }
	 
	 list<Conditions__c> comlist = [select id,opportunity__c,Contact__c,Federal__c from Conditions__c where opportunity__c IN :sobjectSetOfIds];
	 
	 list<Conditions__c> updatedcomlist = new list<Conditions__c>();
	 
	 for(Federal__c fss :flist) {
		for(Conditions__c cm: comlist) {     
				cm.federal__c=fss.id;         
				updatedcomlist.add(cm);
			}
		}
    upsert updatedcomlist;
}
 

 

$force$force

Hi ,

thanks for reply

 

the problem when i doing code review.
the code is inserting duplicate records and the insertion is failed.

it means when ever a new record is created in federal and the opportunity is not null

then the conditions__c ( federal__c)feild should be updated by the newly created federal id.

but in system debug it inserting twice when  i have checked.

 

ex:-

there is a opportunity.  for opportunity there are 5 conditions.

another opportunity there are2 conditions.when ever iam inserting two federal records for two opportunities through debug log.

then the federal id should be inserted for one oppty 5 times and another opty 2 times.

it means total of  7 records should be updated with the newly created federal id for 2 oppts.

but it is inserting 14 times and insertion is failed.

 

system.debug(updatedcomlist.size()+'Conditions to insert is -----------------------------------'+updatedcomlist);

14Conditions to insert is -----------------------------------(Conditions__c

 

Thats is the reason iwant to use map.

 

thanks

$F