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
Bhagwat SalesforceBhagwat Salesforce 

Trigger Query

Hello Guys...
Pls Help Me for this...
1. Create custom object with label 'Demo Object' and API name Demo_Object.
2. Create a 'text' field with label 'Category'.
3. Create a 'lookup' field for 'Opportunity' object.
4. Write trigger for custom object. On insert of custom object record if the value of 'Category' field is equal to 'Opportunity' create a new opportunity record via trigger.
   The opportunity lookup needs to be populated with this new opportunity.
 
v varaprasadv varaprasad
Hi Bhagawat,

Try This : 

 
trigger createOpp on Demo_Object__c(Before insert){

list<opportunity> lstopps = new list<opportunity>();

for(Demo_Object__c do : trigger.new){
   opportunity opp = new opportunity();
   if(do.Category__c == 'Opportunity'){      
	  opp.name = 'testOpp';
	  //Add remaing fields here ..
	  insert opp;	  
   }
   if(opp != null)
   do.Opportunity__C = opp.id;
}


}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 
Akshay_DhimanAkshay_Dhiman
Hi Bhagawat,


Try this code
 
trigger CreateOpportunity on Demo_Object__c (before insert) {
    list<opportunity> opplist=new list<opportunity>();
    for(Demo_Object__c  co: trigger.new){
        if(co.Category__c == 'Opportunity'){  
            opportunity opp = new opportunity();
            opp.name = 'testOp';
            opp.closedate=system.today();
            opp.StageName='Prospecting';
                opplist.add(opp);    
        }
    }
    insert opplist;
    
     for(Demo_Object__c  co: trigger.new){
        if(co.Category__c == 'Opportunity'){  
            for(opportunity op:opplist){
                co.Opportunity__c=op.id;
            }  
        }
    }
}



Hope it will help you!

Thanks
Akshay