+ Start a Discussion
suresh dupadasuresh dupada 

hi folks i want to update child with owner namewhich reflects with parent record owner

this is my trigger   here "msport__c "is my child  nd "mstudent__c" is my parent


trigger GetOwner on MSport__c (Before insert, Before update)
{
    set<id> set1=new set<id>();
    map<id,string> map1=new map<id,string>();
    list<user> userlist=new list<user>();
    if(trigger.isBefore)
    {
        if(trigger.isinsert || trigger.isUpdate)
        {
            for(MSport__c sp:trigger.new)
            {
                set1.add(sp.MStudent__r.ownerid);
            }
            if(set1.size()>0 && set1!=null)
            {
                userlist=[select id,name from user where id in: set1];
                for(user u:userlist)
                {
                    map1.put(u.id,u.name);
                }
                if(map1.size()>0 && map1!=null)
                {
                    for(MSport__c s:trigger.new)
                    {
                        s.Owner__c=map1.get(s.MStudent__r.ownerid);
                      
                    }
                }
            }
        }
    }

}
Best Answer chosen by suresh dupada
asish1989asish1989
Hi Suresh 

Try this 

trigger GetOwner1 on MSport__c (Before insert, Before update)
{
    set<id> set1=new set<id>();
    if(trigger.isBefore)
    {
        if(trigger.isinsert || trigger.isUpdate)
        {
            for(MSport__c sp:trigger.new)
            {
                System.debug('hiii print ownerid---'+sp.mstudent__c);
                set1.add(sp.mstudent__c);
            }
           System.debug('hiii print set size---'+set1);
          if(set1.size() !=0 && set1 !=null){
              map<id,mstudent__c > map_id_Student = new map<id,mstudent__c >([SELECT id,Ownerid
                             FROM mstudent__c
                             WHERE id IN:set1]);
           
              for(MSport__c s:trigger.new)
              {
               s.Owner__c = map_id_Student .get(s.mstudent__c).ownerid;
                     
              }
          }
          
            
        }
    }

}

All Answers

AshlekhAshlekh
Hi,

Below code helps you

trigger GetOwner on MSport__c (Before insert, Before update)
{
    set<id> set1=new set<id>();
    map<id,string> map1=new map<id,string>();
    list<user> userlist=new list<user>();
	Set<Id> parentIdset = new set<ID>();
    if(trigger.isBefore)
    {
        if(trigger.isinsert || trigger.isUpdate)
        {
            for(MSport__c sp:trigger.new)
            {
                //set1.add(sp.MStudent__r.ownerid);
				parentIdset.add(sp.MStudent__c);
            }
			if(parentIdset.size()>0 )
            {
				Map<ID,mstudent__c> ParentMap = new Map<id,String>([select id,ownerid ,owner.name from mstudent__c where id in :parentIdset]);
                if(ParentMap.size()>0)
                {
                    for(MSport__c s:trigger.new)
                    {
						if(s.MStudent__c != null && ParentMap.containsKey(s.MStudent__c))
							s.Owner__c =ParentMap.get(s.MStudent__c).owner.name	;
                      
                    }
                }
            }
        }
    }

}


asish1989asish1989
Hi Suresh 

Try this 

trigger GetOwner1 on MSport__c (Before insert, Before update)
{
    set<id> set1=new set<id>();
    if(trigger.isBefore)
    {
        if(trigger.isinsert || trigger.isUpdate)
        {
            for(MSport__c sp:trigger.new)
            {
                System.debug('hiii print ownerid---'+sp.mstudent__c);
                set1.add(sp.mstudent__c);
            }
           System.debug('hiii print set size---'+set1);
          if(set1.size() !=0 && set1 !=null){
              map<id,mstudent__c > map_id_Student = new map<id,mstudent__c >([SELECT id,Ownerid
                             FROM mstudent__c
                             WHERE id IN:set1]);
           
              for(MSport__c s:trigger.new)
              {
               s.Owner__c = map_id_Student .get(s.mstudent__c).ownerid;
                     
              }
          }
          
            
        }
    }

}
This was selected as the best answer
suresh dupadasuresh dupada
I Want Explanation of the following,...... please 

map<id,mstudent__c > map_id_Student = new map<id,mstudent__c >([SELECT id,Ownerid
                             FROM mstudent__c
                             WHERE id IN:set1]);


s.Owner__c = map_id_Student .get(s.mstudent__c).ownerid;



asish1989asish1989
As we all know  a map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types. 

Get method returns the value to which the specified key is mapped.

map<id,mstudent__c > map_id_Student = new map<id,mstudent__c >([SELECT id,Ownerid
                             FROM mstudent__c
                             WHERE id IN:set1]);

In your code set1 contains lookup field id (mstudent record  id). Now map_id_Student will contain mstudent record id as key and mstudent record as value. if you get perticular record id then it will return mstudent record having id and Owner field value. 

s.Owner__c = map_id_Student .get(s.mstudent__c).ownerid;

map_id_Student .get(s.mstudent__c) returns a particular record, from that record you need Owner field value so It is written like this record.fieldvalue.

Important :

If this is what you where looking for then please mark it as a solution for others benefits.

Thank You