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
Linda 98Linda 98 

Link Custom object to Opportunity

I am having a custom object which is lookup to opportunity.So i am having CO__c field on Opportunity and Opportunity is related list on Custom object.
So  i can link Opportunity to custom object,
I am trying to do this using apex trigger.
Every night custom object records are deleted and recreated,so each time on insert of custom object data,i want to check one particular field and match with opportunity based on that field.

Please help!!
rajaraja
Hello Linda,

Can you explain little bit more about your requirement.

Regards,
raja.
Hemant_SoniHemant_Soni
Hi Linda,

I think you need to consider Batch job according to your Requirement.
If you need  more help in batch job then explain your requirement in detail.
Linda 98Linda 98
No...Its not a batch job.
Actually,i am having a custom object named as 'Project__c' and its is having lookup to Opportunity.So Opportunity is related list on Project__c object  and Project__c is a look up field on Opportunity.

Now,Every night we have a job which delets Project records and recreates at 4 AM with one field 'Project Unit'
 which will be similar to 'unit number' field on oppportunity.
SO when project record is created,i need to check the project unit field and link opportunity to project record.

EDIT-:I had created a field on project__c which is a lookup  to opportunity named as Opportunity__c
Its being updated but value of project__c on opportunity is not being linked becasue of which relatedlist value is not connected.

SO BASICALLY,ITS TO SEARCH AND UPDATE CHILD OBJECT WHEN PARENT OBJECT IS CREATED.(LOOKUP)

sounds simple.but i am missing something.

My code til now
Excuse my typo errors.
trigger Linktoopportunity on project__c (before insert,before update) {

    Map<String,product2> pMap=new Map<String,product2>();
    List<Project__c> iList=new List<Inventory__c>();
    List<Opportunity> oppList=New List<Opportunity>();

    oppList=[select    id,name,accountid,unitnumber__c, from opportunity where unitnumber__c !=null];

    for(product2 pro  : [select id,productcode from product2]){
        pMap.put(pro.productcode,pro);
    }
 
    for(project__c I:trigger.new){
/*

HAVING OTHER CODE WHICH LINKS OTHER OBJECTS and does other stuff on insert and update.
  */  

// CODE WHICH LINKS OPPORTUNITY I MEANT WHICH FILLS OPPORTUNITY__C FIELD ON PROJECT OBJECT


       for(PROJECT__c p:trigger.new){

           if(p.Unit_number__c !=null){
               for(Integer op2=0;op2 < oppList.size();op2++){

               
               if((p.Unit_number__c ==oppList[op2].unitnumber__c )
               {               
               project__c pc =new project__c();
               pc.Opportunity__c=opplist[op2].id;
               ilist.add(pc);
               }
              }   
            }
           insert ilist;
           if(ilist.size() >0){
           p.Opportunity__c =opplist[0].id;       
           }
       }
}




 
Hemant_SoniHemant_Soni
Hi Linda,
Your trigger code is not writed properly and it having big issues like you use index for loop on Opportunity size and code is not bulkified.
And requirement is also not properly defined.
What i understand in your requirement is
1.there is a lookup of opportunity in Project__c.
2.There are Two field one is Project_Unit__c in Project and Unit_Number__c in Opportunity.
3.So When you Create Project there is one filed is visible which is Lookup of Opportunity.
4.When you select Opportunity From lookup and save record Project_Unit__c field is auto populated on detail page of Project which is having the same value as Unit_Number__c.

So if you tell me more about your requirement then i will give you better solution.
Thanks
Hemant
soni.sonihemant.hemant@gmail.com
Linda 98Linda 98
trigger Linktriggertoopportunity on Project__c (before insert,before update,after insert) {

    Map<String,product2> pMap=new Map<String,product2>();
    List<product2> p2List=new List<Product2>();
    List<Opportunity> oppList=New List<Opportunity>();
    List<Opportunity> oppList2=New List<Opportunity>();    
    oppList=[select id,name,accountid,UnitNumber__c from opportunity where  UnitNumber__c !=null];

    for(product2 pro  : [select id,productcode from product2]){
        pMap.put(pro.productcode,pro);
    }
    
     if(trigger.isinsert&&trigger.isbefore)
    for(Project__c I:trigger.new){

        if(i.Unit_Number__c != null && Pmap.Containskey(i.Unit_Number__c))
        {
            i.product__c=pMap.get(i.Unit_Number__c ).Id;
            break;
        } 
            
        else{
               
        Product2 newp =new Product2();
        newp.Name='CustomProduct';
        }
        p2List.add(newp);
        }
       
        insert p2List;
        if(p2List.size() >0){
        i.product__c=p2List[0].id; 
        }
       }
       
       if(trigger.isinsert && trigger.isafter){
       List<Project__c> newproject =new List<project__c>();
       set<id> opids=new set<id>();
       newproject=trigger.new;
       Opplist2=[select id,UnitNumber__c,project__c from opportunity where UnitNumber__c =:newproject[0].Unit_number__c];
       for(Project__c I2:newproject){

         opids.add(i2.id);
       }
      if(!opids.isempty()){
      List<opportunity> oList =[select id,project__c from opportunity where UnitNumber__c =:newproject[0].Unit_number__c];
       for(Opportunity ol:olist){
             
             ol.project__c =trigger.new[0].id;
       }      
      update olist;
      }       
    }
}
Thanks Hemant for help.I got my mistake and trigger is working fine,
This is my complete trigger which is working as expected.As you mentioned,its not bulkified could you please mention points what i missed to bulkify this trigger.

Please help me in bulkifying this trigger.