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 to update record name

I have a Subgrantee object, and a Subgrantee Profile object, with a master-child relation ship (Subgrantee = master, Subgrantee Profile=child). I want to write a trigger so that:
- when the Name field in Subgrantee is updated
- The Subgrantee Profile name is also update (it's not the same, you have to append something to it)
Thanks
Best Answer chosen by Long Truong
Long TruongLong Truong
I got the solution:
trigger updateRR on Account(after update)
    {
        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()];
            for(Subgrantee_Profile__c objRR : lstRR)
            {
                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;
            }
            update lstRR;
        }catch(Exception ex)
        {
            Trigger.new[0].addError('Error in resource request updates --'+ ex.getMessage());
        }
    }

 

All Answers

David ZhuDavid Zhu
may use the following as reference:
 
trigger accountOwnerEmailNotificationOnTask on SubGrantee__c(after update) {
for (SubGrantee__c s : trigger.new)
{
	SubGrantee__c old = trigger.oldmap.get(s.id);
        if (old.get('name') != s.get('name))
        {
		list<subgrantee_profile__c> sps = [select id,name from subgrantee_profile__c where subgrantee__c.id=(id)s.get('id')];
		for (subgrantee_profile__c sp : sps)
		{
			sp.name = 'xxxxxx';
		}
		update sps;
        }
}

 
Long TruongLong Truong
Hi,

I'm relatively new to trigger, and couldn't get the code above to work. Here are the API names for your reference:

Subgrantee is actually Account , and the name is just Name
Subgrantee Profile is Subgrantee_Profile__c, and the name is Name
Subgrantee Profile has a lookup field to lookup Account

I want to write to trigger so that whenever the Name of the Account is changed, then the Name of the Subgrantee Profile is also changed.
Thanks,
Long TruongLong Truong
I got the solution:
trigger updateRR on Account(after update)
    {
        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()];
            for(Subgrantee_Profile__c objRR : lstRR)
            {
                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;
            }
            update lstRR;
        }catch(Exception ex)
        {
            Trigger.new[0].addError('Error in resource request updates --'+ ex.getMessage());
        }
    }

 
This was selected as the best answer