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
suneel.patchipulusu@gmail.comsuneel.patchipulusu@gmail.com 

for (SObject obj: Trigger.new)?????

Hello all

 

Couls any one tell me what the SObject  obj does??    

According to my understanding:

NOrmally  SObject is an generic object and Account,Pricebook,Product,customObject__c are concrete objects

Trigger.New is used for to chnage the field values in that particular Object.

Below event I didnt undertstood what the flow is??? Could any one can explian??

 

This event fires beforeinsert

  for (SObject obj: Trigger.new)
        {
            Account a = (Account)obj;
            if (// condition)
            {
                // code
            }

 

 

Thanks in advance

sfdcfoxsfdcfox

I don't know why anyone would write the code this way, but it is perfectly valid code. Basically, they're casting Trigger.new into a List<SObject>, then casting each element (obj) into an Account SObject. In this case, you could remove that double casting:

 

for(Account a:Trigger.new) {
  if( /* condition */ ) {
    // code
  }
}

Code like this might be useful if you didn't know for sure what type of SObject you might be dealing with:

 

SObject[] objs = new SObject[0];
objs.addAll([select id,name from account]);
objs.addAll([select id,name from opportunity]);
for(sobject obj:objs) {
  if(obj.getsobjecttype()==account.sobjecttype) {
    account a = (account)obj;
    // Do stuff here
  }
  if(obj.getsobjecttype()==opportunity.sobjecttype) {
    opportunity o = (opportunity)o;
    // Do stuff here
  }
}