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
sonam gupthasonam guptha 

update non related table value to another table

HI All,

i have a custom table(Social_media__C) which doesn’t have any relationship with opportunity,And i have another custom table partner__c which is related to opportunity,now when first record(on partner__c table) is created related to a opportunity,that first partner__c one of the field value we need to update into Social_media__c’s one of the field.how can we do this?

            first.Created.partner__c.value = Social_media__c.value(no lookup to opportunity)
Best Answer chosen by sonam guptha
rajat Maheshwari 6rajat Maheshwari 6
Please use below code snippet and let me know, If it works :)
 
trigger SocialMediaTrigger on Social_media__c(before insert,before update)
  {
    
 Set<Id> st_oppId = new Set<Id>();
 Map<Id,Partner__c> mp_Partner;
    
for(Social_media__c socialMediaRecd : Trigger.new)
  {
     st_oppId.add(socialMediaRecd.Opportunity__c);
  }
  
  for(Partner__c partnerRecd : [Select Id,Opportunity__c,PopulateValuetoSocialMedia from Partner__c where Opportunity IN  : st_oppId])
    {
  if(mp_Partner==null)
      mp_Partner = new Map<Id,Partner__c>();
  
  mp_Partner.put(partnerRecd.Opportunity__c,partnerRecd);
}
  
for(Social_media__c socialMediaRecd : Trigger.new)
   {
   
     if(mp_partner!=null && mp_partner.containsKey(socialMediaRecd.Opportunity))
         socialMediaRecd.Field = mp_partner.get(socialMediaRecd.Opportunity__c).PopulateValuetoSocialMedia;
   
   }
}


Here Field represent the field of social media, which u will populate from partner field.
PopulateValuetoSocialMedia represent the field of partner, which you want to update.
Thanks
 

All Answers

suraj gupta 31suraj gupta 31
You can create a trigger on  the  partner__c object, that trigger will fire when you create the record on the partner___c then accordingly you can update the Social_media__c object with your required values.
Amit Singh 1Amit Singh 1
Hello Sonam,

Is there any relationship between Social_media__c and partner__c Object If yes, Then you can simply write a trigger as Suraj said. If no then how can you ditermine that which Partner object need to update.

Let me know if this helps :)

Thanks!
Amit Singh
sonam gupthasonam guptha
Hello,

Thank you for the response,as i said there is no relationship between Social_media__c and partner__c,so if i create lookup on Social_media__c to opportunity,then if i write a trigger on it,that may solve my problem?

if that is the case,then i will a create lookup to opportunity,but i have a dought will that lookup is really helpful to search the previous opportunitys related partner__c's records even,And also i need to find the first created record on the partner__c , that value i have to update on Social_media__c's field,also please provide any sample code snippet for the trigger.
rajat Maheshwari 6rajat Maheshwari 6
Please use below code snippet and let me know, If it works :)
 
trigger SocialMediaTrigger on Social_media__c(before insert,before update)
  {
    
 Set<Id> st_oppId = new Set<Id>();
 Map<Id,Partner__c> mp_Partner;
    
for(Social_media__c socialMediaRecd : Trigger.new)
  {
     st_oppId.add(socialMediaRecd.Opportunity__c);
  }
  
  for(Partner__c partnerRecd : [Select Id,Opportunity__c,PopulateValuetoSocialMedia from Partner__c where Opportunity IN  : st_oppId])
    {
  if(mp_Partner==null)
      mp_Partner = new Map<Id,Partner__c>();
  
  mp_Partner.put(partnerRecd.Opportunity__c,partnerRecd);
}
  
for(Social_media__c socialMediaRecd : Trigger.new)
   {
   
     if(mp_partner!=null && mp_partner.containsKey(socialMediaRecd.Opportunity))
         socialMediaRecd.Field = mp_partner.get(socialMediaRecd.Opportunity__c).PopulateValuetoSocialMedia;
   
   }
}


Here Field represent the field of social media, which u will populate from partner field.
PopulateValuetoSocialMedia represent the field of partner, which you want to update.
Thanks
 
This was selected as the best answer