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
Devendra Hirulkar 3Devendra Hirulkar 3 

​trigger to copy child field data to parent field data (workflow also if it possible by workflow)

trigger to copy child field data to parent field data 
like i have copy data from  student(child) to class (parent) but by matching the name of both child and parent 
if the name of the both child and parent are match then only save the data like if the name of both child and parent are match then when we save rollno on student it compare name if match then it automticlly stored on class
 thanks 
SonamSonam (Salesforce Developers) 
sample code(tweak/bulkify):

trigger CopyChildtoParent on Child (after insert,after update)
{
Child con = [Select #Fieldyouwishtocopy from Child where ID=:trigger.id[;

// SOQL to get the exact account with matching name as child
Parent par :  [select Id, #Fieldyouwishtoupdate  from Parent where Parent.name in :Trigger.new.Name]

par.#Fieldyouwishtoupdate = con.#Fieldyouwishtocopy
update con; 

 }

reference:
http://stackoverflow.com/questions/21738345/trigger-to-update-account-filed-when-a-corresponding-contact-field-is-updated-i
RamuRamu (Salesforce Developers) 
To do this, you would first need to create a map of all class record names and their id's something as map<string,id>. Then loop through the new collection of students and see if the map contains the same name, if yes then put the lookup value as the matching id from the map.

Hope this helps !!
Devendra Hirulkar 3Devendra Hirulkar 3
Hi both sonam and  ramu
in the above sonam trigger there is one error has occur 
(unexpected token: ':' at line 6 )here is trigger

trigger CopyChildtoParent on class__c (after insert) {

Child con = [Select Roll_No__c from class__c where ID=:trigger.id];

// SOQL to get the exact account with matching name as child
Parent par :  [select Id, Roll_No__c  from Student__c where Student__c.name in :Trigger.new.Name]

par.Roll_No__c = con.Roll_No__c
update con; 
thanks
Devendra
 
Devendra Hirulkar 3Devendra Hirulkar 3

Parent par :  [select Id, Roll_No__c  from Student__c where Student__c.name in :Trigger.new.Name](here the line error)
Devendra Hirulkar 3Devendra Hirulkar 3
how to map in my class(child ) there are three field   in the child object (classname, student,, rollno)
Devendra Hirulkar 3Devendra Hirulkar 3
hay ramu
please tell me what goes wrong about the trigger
the trigger is correct to the map but u say the(
and see if the map contains the same name, if yes then put the lookup value as the matching id from the map.
) i dont undestand what to do to match and put them


trigger dataupdate on class__c (before insert, before update)
{
    //create reference table for lookup value
    Map<String, Integer> stdMap = new Map<String, Integer>();
    //build reference table for VAT rates
    for (Student__c std : [SELECT Name, Roll_No__c FROM Student__c])
        stdMap.put(std.Name, std.Roll_No__c);
    //update Student__c with VAT rate
    for (Student__c stud : trigger.new)
    {
        Integer roll = stdMap.get(stud.Name);
        stud.Student__c = roll;
    }
}
thanks
AM2014AM2014
Hi ,
You can try the below code as well..

trigger UpdateRollNo on Class__c (before insert,before update) {     

       Map<Id,Student__c> studMap = new Map<Id,Student__c>();
       Set<Id> Ids = new Set<Id>();
                   
            for(Class__c c: Trigger.new)
            {
               Ids.add(c.Student_Name__c);
            }
            
            Map<id,Student__c> studmap2 = new Map<id,Student__c>([Select Id,Name,Student_Roll_Number__c From Student__c where Id IN : Ids]);
            
            for(Class__c c1 : Trigger.new)
              {
                  Student__c st = studMap2.get(c1.Student_Name__c);
                      if(st.Student_Roll_Number__c == '')
                          {
                          st.Student_Roll_Number__c = c1.Student_Roll_No__c;
                          studMap.put(st.id,st);
                          }
               }
            if(!studmap.isEmpty())
            {
               update studMap.values();
            }   
}              
Harish RamachandruniHarish Ramachandruni
Hi,

Create countfiled in parent ,


add belowtrigger in chield .
 
trigger SAP_Seq_Number on FX5__Ticket_Item__c (Before insert) {

    integer sum = 10;

list<id> parentids = new list<id>();

list<integer> parentids = new list<integer>();

   
    for( FX5__Ticket_Item__c  tr: Trigger.new()){

       parentids.(tr.parentrelationid) 

    }

list<parentapi> parentlts = new list<parentapi>(); 

Map<ID, parentapi > m = new Map<ID, parentapi >([SELECT Id,count  FROM parentapi ]);


for( parentapi pren : [select id,count from parent where id :parentids  ]){


   pren.count =  pren.count + sum ;

       parentlts.add(pren) 

    }

update parentlts;


 for(Class__c c1 : Trigger.new)

   {

  parentapi st = m.get(c1.Student_Name__c);


 c1.SAP_Sequence_Number__c  = st.count ;


}


}




Regards,
harish.