+ Start a Discussion
Devendra Hirulkar 3Devendra Hirulkar 3 

how to return name instated of id

Hi friends,
i write a trigger that copy one obj to another
but when it copy it return a id instated of name
how to solve this
the below is my trigger


trigger copypro on Subsc__c (after insert,after update) 
{
  Map<ID, Account> Acc = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
  List<Id> listIds = new List<Id>();

  for (Subsc__c s : Trigger.new)
  {
    listIds.add(s.Company_Name__c);
  }

  //Populate the map. Also make sure you select the field you want to update, amount
  //The child relationship is more likely called Quotes__r (not Quote__r) but check
  //You only need to select the child quotes if you are going to do something for example checking whether the quote in the trigger is the latest
  Acc = new Map<Id, Account>([SELECT id, Product_Name__c,(SELECT ID,Product__r.Name  FROM Subscs__r) FROM Account WHERE ID IN :listIds]);


  for (Subsc__c sub : Trigger.new)
  {
     Account ac = Acc.get(sub.Company_Name__c);
    ac.Product_Name__c= sub.Product__r.Name;
  }

  update Acc.values();
}
Best Answer chosen by Devendra Hirulkar 3
Samadhan Sakhale 3Samadhan Sakhale 3
Hey Deoo,
Its very Simple as per Your Question you have to make some changes in your trigger & try the below Trigger

trigger copySubtoAcc on Subscription__c (after insert,after update) 
{
  Map<ID, Account> Acc = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
  List<Id> listIds = new List<Id>();
  set<ID>cObjectID = new set<ID>();   //Making a set of Product ID's

  for (Subscription__c s : Trigger.new)
  {
    listIds.add(s.Company_Name__c);
      
    if(s.Membership_Type__c != null)
    {
       cObjectID.add(s.Membership_Type__c);//takes the Lookup Record & Add that ID's in cObjectID set
     }
  }
    if(!cObjectID.isEmpty()){
        
        Map<ID,Product2> cObjectMap = new Map<ID,Product2>([select Id,Name from Product2 where Id IN: cObjectID]);
        
        for(Subscription__c s : trigger.new)
        {            
            if(cObjectMap.get(s.Membership_Type__c).Name != Null)
            {
                // fill the country name on Opportunity with Country Name on Country_Object__c
                String pro= cObjectMap.get(s.Membership_Type__c).Name;
                 Acc = new Map<Id, Account>([SELECT id, Product__c,(SELECT ID,Membership_Type__c  FROM Subscriptions__r) FROM Account WHERE ID IN :listIds]);
                Account myacc = acc.get(s.Company_Name__c);
                 myacc.Product__c =pro;
                update Acc.values();
            }
        }
    }
   
}


thanks,
Sam

All Answers

Samadhan Sakhale 3Samadhan Sakhale 3
Hey Deoo,
Its very Simple as per Your Question you have to make some changes in your trigger & try the below Trigger

trigger copySubtoAcc on Subscription__c (after insert,after update) 
{
  Map<ID, Account> Acc = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
  List<Id> listIds = new List<Id>();
  set<ID>cObjectID = new set<ID>();   //Making a set of Product ID's

  for (Subscription__c s : Trigger.new)
  {
    listIds.add(s.Company_Name__c);
      
    if(s.Membership_Type__c != null)
    {
       cObjectID.add(s.Membership_Type__c);//takes the Lookup Record & Add that ID's in cObjectID set
     }
  }
    if(!cObjectID.isEmpty()){
        
        Map<ID,Product2> cObjectMap = new Map<ID,Product2>([select Id,Name from Product2 where Id IN: cObjectID]);
        
        for(Subscription__c s : trigger.new)
        {            
            if(cObjectMap.get(s.Membership_Type__c).Name != Null)
            {
                // fill the country name on Opportunity with Country Name on Country_Object__c
                String pro= cObjectMap.get(s.Membership_Type__c).Name;
                 Acc = new Map<Id, Account>([SELECT id, Product__c,(SELECT ID,Membership_Type__c  FROM Subscriptions__r) FROM Account WHERE ID IN :listIds]);
                Account myacc = acc.get(s.Company_Name__c);
                 myacc.Product__c =pro;
                update Acc.values();
            }
        }
    }
   
}


thanks,
Sam
This was selected as the best answer
Devendra Hirulkar 3Devendra Hirulkar 3
thanks my problem have be solved............