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
Long TruongLong Truong 

Trigger works but new field value is not save???

I wrote a trigger so that when the Account Name is updated, the Subgrantee Profile (custom object, linked by look up relationship) Name is also updated.

For example: Account name is ABC, and Subgratee Profile name is 123-ABC. When the Account name is updated to EFG, Subgrantee Profile name should become 123-EFG.

I checked the debug pritn out, and all the values is as it should be, but the new name for the subgrantee profile is not being save???

trigger updateAcountNameonSubProfile on Account(after update)
    {
    for (Account a:Trigger.new)
    {
        try
        {   
//select the related Subgrantee Profile
            List<Subgrantee_Profile__c> lstRR=[Select r.Id,r.Name, r.Subgrantee__c from  Subgrantee_Profile__c r where r.Subgrantee__c in : Trigger.newMap.keyset()];
            system.debug('>>>list size: '+lstRR.size());
            for(Subgrantee_Profile__c objRR : lstRR)
   {
                system.debug('>>>updating this sub profile:'+objRR.name);
                if(Trigger.newMap.containskey(objRR.Subgrantee__c) )
                objRR.Name=Trigger.newMap.get(objRR.Subgrantee__c).Mailing_State__c + Trigger.newMap.get(objRR.Subgrantee__c).Associated_Project_Number__c+' - '+Trigger.newMap.get(objRR.Subgrantee__c).Name;
                system.debug('>>>New subgrantee profile name:'+objRR.Name);
            }
            update lstRR;
//the new name is correct, but the record in Salesforce is not updated????           
 System.debug('>>>after updating list - new sub profile name: '+ lstRR.get(0).name  );
        }catch(Exception ex)
        {
            Trigger.new[0].addError('Error in resource request updates --'+ ex.getMessage());
        }
    }
    }
VinojVinoj
Hi Long,

You may need to use a new list for the update rather than re-using lstRR.  If you do something like this does it work?
 
// new list
List<Subgrantee_Profile__c> toUpdate;
            
for (Subgrantee_Profile__c objRR : lstRR) {
	system.debug('>>>updating this sub profile:'+objRR.name);
                
	if(Trigger.newMap.containskey(objRR.Subgrantee__c) ) {
		objRR.Name=Trigger.newMap.get(objRR.Subgrantee__c).Mailing_State__c + Trigger.newMap.get(objRR.Subgrantee__c).Associated_Project_Number__c+' - '+Trigger.newMap.get(objRR.Subgrantee__c).Name;
        // add to new list
		toUpdate.add(objRR);
	}

	system.debug('>>>New subgrantee profile name:'+objRR.Name);
}
            
update toUpdate;

 
Long TruongLong Truong
It gives an error that says "attempt to deference a null object"....
VinojVinoj
Sorry, that first line should read:
List<Subgrantee_Profile__c> toUpdate = new List<Subgrantee_Profile__c>();
Not sure if that will fix the update issue though, so will continue to investigate and if I find anything will let you know.
VinojVinoj
I'm stumped.  I tested this out on a developer org and I'm getting the correct results after updating a single account with one related Subgrantee Profile record.  The related Subgrantee Profile is being updated. The only difference in my code is the name I set objRR to:
 
trigger updateAcountNameonSubProfile on Account(after update)
{
    for (Account a:Trigger.new) {
        try {   
            List<Subgrantee_Profile__c> lstRR=[Select Id, Name, Subgrantee__c from Subgrantee_Profile__c r where r.Subgrantee__c in : Trigger.newMap.keyset()];
            system.debug('>>>list size: '+lstRR.size());
            
            for(Subgrantee_Profile__c objRR : lstRR) {
                system.debug('>>>updating this sub profile:'+objRR.name);
                if(Trigger.newMap.containskey(objRR.Subgrantee__c) ) {
                    objRR.Name = 'new name ' + System.now();
                }                    
                system.debug('>>>New subgrantee profile name:'+objRR.Name);
            }
            update lstRR;

         System.debug('>>>after updating list - new sub profile name: '+ lstRR.get(0).name  );
        
        }catch(Exception ex) {
            Trigger.new[0].addError('Error in resource request updates --'+ ex.getMessage());
        }
    }
}

 
Paul S.Paul S.
How about this?
trigger updateAcountNameonSubProfile on Account(after u pdate){

    List<Subgrantee_Profile__c> lToUpdate = new List<Subgrantee_Profile__c>;

    for (Account a : Trigger.new){
        
        try{

            List<Subgrantee_Profile__c> lstRR=[Select r.Id,r.Name, r.Subgrantee__c from  Subgrantee_Profile__c r where r.Subgrantee__c in : Trigger.newMap.keyset()];
            
	    system.debug('>>>list size: '+lstRR.size());

            for(Subgrantee_Profile__c objRR : lstRR){

	        system.debug('>>>updating this sub profile:'+objRR.name);

		if(Trigger.newMap.containskey(objRR.Subgrantee__c)) {
                
		    Subgrantee_Profile__c sp = new Subgrantee_Profile__c();
		    sp.Id = objRR.id;			
		    sp.Name = Trigger.newMap.get(objRR.Subgrantee__c).Mailing_State__c + Trigger.newMap.get(objRR.Subgrantee__c).Associated_Project_Number__c+' - '+Trigger.newMap.get(objRR.Subgrantee__c).Name;
                    system.debug('>>>New subgrantee profile name:'+sp.Name);
		    lToUpdate.add(sp);
                }

	    }

	    update lToUpdate;

	    <b>//the new name is correct, but the record in Salesforce is not updated????
   
 	    System.debug('>>>after updating list - new sub profile name: '+ lstRR.get(0).name  );

        } catch(Exception ex){

	      Trigger.new[0].addError('Error in resource request updates --'+ ex.getMessage());
        }
    }

}

 
Long TruongLong Truong
@ [Vinoj] : your code works!! However, it only fires when I log in as a regular SF user. When I log in as a portal user it doesn't fire. And I checked to make sure the portal user have edit access to that field... But is solves 50% of our problem, so thanks a ton!
Paul S.Paul S.
That's really interesting. I can't think why that trigger wouldn't fire. That said, this code should still be bulkified. As it stands now, the code will fail if you try to bulk update a larger number of accounts. I can help with that once I can get to my computer.