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
sam_Adminsam_Admin 

Help in Trigger

Hi,

 I have object called Sales and it has field Quote__c which is lookup to Quote object and in Quote object we have field called

Opportunity__c which is lookup to Opp object now on Sales object page if i select Quote__c then the Opp field on this sales object should pull up the Opp from Quote , can anyone help on this ?

 

trigger Opp on Sales__c (before Insert, before Update) {

    for(Sales__c s : Trigger.New)
    {
      if(s.Quote__c != null)
        {
            Quote__c q = new Quote__c();
            s.Opp__c =  q.Opportunity__c;
        }
       
    }

}

PrakashbPrakashb

You can try the below code.

 

trigger Opp on Sales__c (before Insert, before Update) {

  

   Set<ID> quoteId = new Set<ID>();
    for(Sales__c s : Trigger.New)    {
      if(s.Quote__c != null)        {
           if(Trigger.isInsert)

                 quoteId.add(s.Quote__c);

          else if(Trigger.isUpdate){

                   if(s.Quote__c != Trigger.oldMap.get(s.ID).Quote__c)

                         quoteid.add(s.Quote__c);

          }
      }

     Map<ID,Id> quotemap = new Map<ID,ID>(); 

    for(Quote__c q : [selec Id,Opportunity__C from Quote where ID IN: quoteID]){

               quotemap.put(q.Id,q.Opportunity__C) ; 

   }

   for(Sales__c s : Trigger.New)    {
      if(s.Quote__c != null)        {
           if(Trigger.isInsert)

                 s.Opp__c =  quotemap.get(s.Quote__c);

          else if(Trigger.isUpdate){

                   if(s.Quote__c != Trigger.oldMap.get(s.ID).Quote__c)

                         s.Opp__c =  quotemap.get(s.Quote__c);

          }
      }


 }

TrinayTrinay

Hi sam,

     

           Refer the following TRIGGER, i hope it will give you  the solution.

 

trigger getRecord on Sales__c (after insert)
{
    for(Sales__c l : Trigger.New)
   {
      if(l.Quote__c != null)
     {
         Quote__c u = [SELECT id,Opportunity__c FROM Quote__c  WHERE id = :l.Quote__c];
         List<Sales__c> li = [SELECT Id,Opp__c FROM Sales__c WHERE id = :l.Id];
       

          for(Sales__c ap : li)
          {
             ap.Opp__c  = u.Opportunity__c ;
             update ap;
          }
     }
  }
}

 If this post is helpful to your solution, kindly mark this as the solution