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
Soundar Rajan PonpandiSoundar Rajan Ponpandi 

Create new Opportunity(Enquiry) When Checkbox is enabled

Hi,

I am trying to create a new opportunity (Enquiry) when a checkbox (Enquiry_Creator__c) is enabled . anyone please give me a advise why it's not created.  Kindly Check the below code please.

************** Trigger ***********
Trigger VisualInsCreateEnquiry on Visual_Inspection__c (After Insert) {
    
    Set<Id> VisInsId = new Set<Id>();
    List<Opportunity> oppList = New List<Opportunity>();
    
    for(Visual_Inspection__c  vi : Trigger.New){
        If(vi.Enquiry_Creator__c == True){
            VisInsId.add(vi.Id);
            system.debug('++++++Visual Inspection Id : ' + VisInsId);
        }
    }
    
    If(VisInsId.size() > 0){
        System.debug('+++++++Visual Inspection Size is : ' + VisInsId.size() );
        
            oppList = [Select id,name, Customer_Name__c, Type, Sales_Office__c, Sales_Org1__c, Plant_Type__c, Sites__c, CloseDate, StageName
                       from Opportunity 
                       where Id IN :VisInsId AND RecordType.Name IN ('Service Centre')];
        
        for(Visual_Inspection__c vis : Trigger.new){
            If(VisInsId.size() > 0 && vis.Enquiry_Creator__c == True){
                for(Opportunity op : oppList){
            Opportunity oppty = New Opportunity();
            //oppty.RecordTypeId = oppList.Id;
            oppty.Name = 'Test Oppty';
            oppty.StageName =  'Enquiry';
            oppty.Sales_Office__c = 'Chennai';
            oppty.Sales_Org1__c = 'Chennai';
            oppty.CloseDate = System.today();
            oppty.Plant_Type__c = 'New Plant';
            oppty.Sites__c = 'Test Site';
            oppList.add(oppty);
        }
                
                IF(oppList.size() > 0){
            insert oppList;
        }
                
                
            }
        }
        
        
    }
}

 Thanks in advance !!!

Regards,
Soundar.
Best Answer chosen by Soundar Rajan Ponpandi
D-CoderD-Coder
Here you go ::
 
Trigger VisualInsCreateEnquiry on Visual_Inspection__c (After Insert, After Update) {
    
	Set<Id> VisInsId = new Set<Id>();
	List<Opportunity> oppList = new List<Opportunity>();
    List<Visual_Inspection__c> visList = New List<Visual_Inspection__c>();

    for(Visual_Inspection__c  vi : Trigger.New){
		
		VisInsId.add(vi.id);
    }

	visList = [SELECT id, Enquiry_Creator__c FROM Visual_Inspection__c WHERE id IN : VisInsId];
	
	for(Visual_Inspection__c  vi : visList){
		
		If(vi.Enquiry_Creator__c){
		
			Opportunity oppty = new Opportunity();
			
            oppty.Name = 'Test Oppty';
            oppty.StageName =  'Enquiry';
            oppty.Sales_Office__c = 'Chennai';
            oppty.Sales_Org1__c = 'Chennai';
            oppty.CloseDate = System.today();
            oppty.Plant_Type__c = 'New Plant';
            oppty.Sites__c = 'Test Site'; 
			
			oppList.add(oppty);
        }
    }
	
	if(oppList != null && oppList.size()>0){
		
		insert oppList;
	}
    
}

If this answers your query please mark this question as a solved so that it can be filtered out from unsolved questions.

All Answers

D-CoderD-Coder
Hi Soundar Raj,

While quering Opportunities at line no. 16 , see WHERE clouse , Change
Id IN :VisInsId
You are searching Opportunity ID in a Set of  Visual_Inspection__c IDs , which is incorrect.
I suppose there is a field on Opportunity as Visual_Inspection__c , and your WHERE clouse should be  WHERE Visual_Inspection__c IN :VisInsId

If this answers your query please mark this question as a solved so that it can be filtered out from unsolved questions.

 
Soundar Rajan PonpandiSoundar Rajan Ponpandi

Hi $C0RP!AN K!NG,

Thanks For Your quick response..

Actually i did one mistake !! we need to create a new Opportunity when a checkbox is true . So we need to query for visual_inspection__c ??

visList = [Select id,name from Visual_Inspection__c   where Id IN :VisInsId];


And I have some changes based on above Query. Now also i am getting
Error:  (Incompatible element type Opportunity for collection of Visual_Inspection__c)
 
Trigger VisualInsCreateEnquiry on Visual_Inspection__c (After Insert, After Update) {
    
    Set<Id> VisInsId = new Set<Id>();
    List<Visual_Inspection__c> visList = New List<Visual_Inspection__c>();
    
    for(Visual_Inspection__c  vi : Trigger.New){
        If(vi.Enquiry_Creator__c == True){
            VisInsId.add(vi.Id);
            system.debug('++++++Visual Inspection Id : ' + VisInsId);
        }
    }
    
    If(VisInsId.size() > 0){
        System.debug('+++++++Visual Inspection Size is : ' + VisInsId.size() );
        
            visList = [Select id,name from Visual_Inspection__c 
                       where Id IN :VisInsId];
        
        system.debug('Opportunity : ' + visList);
        
        
        for(Visual_Inspection__c vis : Trigger.new){
            If(vis.Enquiry_Creator__c == True && VisInsId.size() >0){
                for(Opportunity opp : visList){
                        Opportunity oppty = new Opportunity();
             oppty.Id = oppty.Visual_Inspection__c;
            oppty.Name = 'Test Oppty';
            oppty.StageName =  'Enquiry';
            oppty.Sales_Office__c = 'Chennai';
            oppty.Sales_Org1__c = 'Chennai';
            oppty.CloseDate = System.today();
            oppty.Plant_Type__c = 'New Plant';
            oppty.Sites__c = 'Test Site'; 
            visList.add(oppty);   // Error Line
                }
            
            }
        }
      
       
        
    }
}

Regards,
Soundar.
D-CoderD-Coder
Here you go ::
 
Trigger VisualInsCreateEnquiry on Visual_Inspection__c (After Insert, After Update) {
    
	Set<Id> VisInsId = new Set<Id>();
	List<Opportunity> oppList = new List<Opportunity>();
    List<Visual_Inspection__c> visList = New List<Visual_Inspection__c>();

    for(Visual_Inspection__c  vi : Trigger.New){
		
		VisInsId.add(vi.id);
    }

	visList = [SELECT id, Enquiry_Creator__c FROM Visual_Inspection__c WHERE id IN : VisInsId];
	
	for(Visual_Inspection__c  vi : visList){
		
		If(vi.Enquiry_Creator__c){
		
			Opportunity oppty = new Opportunity();
			
            oppty.Name = 'Test Oppty';
            oppty.StageName =  'Enquiry';
            oppty.Sales_Office__c = 'Chennai';
            oppty.Sales_Org1__c = 'Chennai';
            oppty.CloseDate = System.today();
            oppty.Plant_Type__c = 'New Plant';
            oppty.Sites__c = 'Test Site'; 
			
			oppList.add(oppty);
        }
    }
	
	if(oppList != null && oppList.size()>0){
		
		insert oppList;
	}
    
}

If this answers your query please mark this question as a solved so that it can be filtered out from unsolved questions.
This was selected as the best answer
Soundar Rajan PonpandiSoundar Rajan Ponpandi
HI $C0RP!AN K!NG,

I have some changes in my code by your Help.. thanks a lot!!

*********** Code Chages ************
 
Trigger VisualInsCreateEnquiry on Visual_Inspection__c (After Insert, After Update) {
    
    Set<Id> VisInsId = new Set<Id>();
    List<Visual_Inspection__c> visList = New List<Visual_Inspection__c>();
    List<Opportunity> opplIst = New List<Opportunity>();
    
    for(Visual_Inspection__c  vi : Trigger.New){
        If(vi.Enquiry_Creator__c == True){
            VisInsId.add(vi.Id);
            system.debug('++++++Visual Inspection Id : ' + VisInsId);
        }
    }
    
    If(VisInsId.size() > 0){
        
        visList = [Select id,name,Enquiry_Creator__c,Customer__c from Visual_Inspection__c where Id IN :VisInsId];
        
        
        system.debug('Opportunity : ' + visList);
        // for(Visual_Inspection__c vis : Trigger.new){
        //If(vis.Enquiry_Creator__c == True && VisInsId.size() >0){
        for(Visual_Inspection__c vis : visList){
            If(vis.Enquiry_Creator__c == True){
            Opportunity oppty = new Opportunity();
            oppty.Visual_Inspection__c = vis.Id;
            oppty.Name = vis.Customer__c + vis.Customer__c ;
            oppty.StageName =  'Enquiry';
            oppty.CloseDate = System.today();
            oppty.Plant_Type__c = 'New Plant';
            oppty.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Service Centre').getRecordTypeId();
            opplIst.add(oppty); 
            }
       
        }
        
        If(opplIst.size() > 0){
            Insert opplIst;
        }
        
    }
}

Thanks,
Soundar.