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
neshnesh 

How to use trigger below logic:my code sample-please help me

How to use Trigger -below logic--: wheneverTraining object survey  flag true i want to insert field into Training survey object..and also insert record type ..please help me..

------------------------------------------------------

List <training__c> t1 = new list <training__c>();
       list <training_survey__c> ts2 = new list<training_survey__c>();
       t1 = [ select day1surveyflag__c, day3surveyflag__c, day5surveyflag__c from training__c where day1surveyflag__c = true or day3surveyflag__c = true or day5surveyflag__c = true ];
       for(training__c tx : t1)
          { 
             training_survey__c ts = new training_survey__c();
             ts.training__C = tx.id;
             if(tx.day1surveyflag__c == true ) 
               {
                 ts.Survey_Type__c = 'Day1';
               }else
             if(tx.day3surveyflag__c == true )
               {
                 ts.Survey_Type__c = 'Day3';
               }else
             if(tx.day5surveyflag__c == true )
               {
                 ts.Survey_Type__c = 'Day5';
               }else
               {
                 ts.Survey_Type__c = 'Dropped';
               } 
             RecordType r = [Select id from Recordtype where sobjecttype = 'training_survey__c' and name= :ts.Survey_Type__c];
             ts.RecordTypeid = r.id;
             ts2.add(ts); 
          } 
        if(ts2 != null )
         {
           insert ts2;  
         }     

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi,

 

Below is the code as per your requirement. you have to make some modification in that as per your requirement.

Apex Trigger:

trigger updateFieldwithRecType on training__c (after insert, after update) {
	List <training__c> t1 = new list <training__c>();
	list <training_survey__c> ts2 = new list<training_survey__c>();
	t1 = [ select day1surveyflag__c, day3surveyflag__c, day5surveyflag__c from training__c where id IN: trigger.newMap.keyset() and (day1surveyflag__c = true or day3surveyflag__c = true or day5surveyflag__c = true)];
	Map<string,Id> mapRecordtype = new Map<string,Id>();
	set<String> sSurveytype = new set<String>();
	sSurveytype.add('Day1');
	sSurveytype.add('Day3');
	sSurveytype.add('Day5');
	sSurveytype.add('Dropped');
	List<RecordType> lstRecType = [select id from RecordType where sobjecttype = 'training_survey__c' and Name IN: sSurveytype];
	for(RecordType rt: lstRecType){
		mapRecordtype.put(rt.Name,rt.id);
	}
	for(training__c tx : t1){ 
		training_survey__c ts = new training_survey__c();
		ts.training__C = tx.id;
		if(tx.day1surveyflag__c == true){
			ts.Survey_Type__c = 'Day1';
		}else if(tx.day3surveyflag__c == true){
			ts.Survey_Type__c = 'Day3';
		}else if(tx.day5surveyflag__c == true){
			ts.Survey_Type__c = 'Day5';
		}else{
			ts.Survey_Type__c = 'Dropped';
		}
		ts.RecordTypeid = mapRecordtype.get(ts.Survey_Type__c);
		ts2.add(ts); 
	} 
	if(ts2 != null){
		insert ts2;  
	}   
}	

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

All Answers

GlynAGlynA

Hi Nesh,

 

This code was written as a controller, but if you need it to be a trigger, you can modify it:

 

public class CreateTrainingSurveys
{
    public CreateTrainingSurveys( ApexPages.StandardController controller ) {}

    public void createSurveys()
    {
        Set<String> set_SurveyTypes = new Set<String>{ 'Day1', 'Day3', 'Day5', 'Dropped' };

        Map<String,Id> map_RecordTypeIDs = new Map<String,Id>();

        for ( RecordType recordType :
            [   SELECT  Id
                FROM    RecordType
                WHERE   (   sObjectType = 'Training_Survey__c'
                        AND Name IN :set_SurveyTypes
                        )
            ]
            )
        {
            map_RecordTypeIDs.put( recordType.Name, recordType.Id );
        }

        List<Training_Survey__c> list_TrainingSurveysToCreate = new List<Training_Survey__c>();

        for ( Training__c training :
            [   SELECT  Day1SurveyFlag__c, Day3SurveyFlag__c, Day5SurveyFlag__c
                FROM    Training__c
                WHERE   (   Day1SurveyFlag__c = true
                        OR  Day3SurveyFlag__c = true
                        OR  Day5SurveyFlag__c = true
                        )
            ]
            )
        {
            String surveyType   = training.Day1SurveyFlag__c    ?   'Day1'
                                : training.Day3SurveyFlag__c    ?   'Day3'
                                : training.Day5SurveyFlag__c    ?   'Day5'
                                :                                   'Dropped';

            list_TrainingSurveysToCreate.add
            (   new Training_Survey__c
                (   Training__c     = training.Id,
                    Survey_Type__c  = surveyType,
                    RecordTypeId    = map_RecordTypeIDs.get( surveyType )
                )
            );
        }
        insert list_TrainingSurveysToCreate;
    }
}

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator

hitesh90hitesh90

Hi,

 

Below is the code as per your requirement. you have to make some modification in that as per your requirement.

Apex Trigger:

trigger updateFieldwithRecType on training__c (after insert, after update) {
	List <training__c> t1 = new list <training__c>();
	list <training_survey__c> ts2 = new list<training_survey__c>();
	t1 = [ select day1surveyflag__c, day3surveyflag__c, day5surveyflag__c from training__c where id IN: trigger.newMap.keyset() and (day1surveyflag__c = true or day3surveyflag__c = true or day5surveyflag__c = true)];
	Map<string,Id> mapRecordtype = new Map<string,Id>();
	set<String> sSurveytype = new set<String>();
	sSurveytype.add('Day1');
	sSurveytype.add('Day3');
	sSurveytype.add('Day5');
	sSurveytype.add('Dropped');
	List<RecordType> lstRecType = [select id from RecordType where sobjecttype = 'training_survey__c' and Name IN: sSurveytype];
	for(RecordType rt: lstRecType){
		mapRecordtype.put(rt.Name,rt.id);
	}
	for(training__c tx : t1){ 
		training_survey__c ts = new training_survey__c();
		ts.training__C = tx.id;
		if(tx.day1surveyflag__c == true){
			ts.Survey_Type__c = 'Day1';
		}else if(tx.day3surveyflag__c == true){
			ts.Survey_Type__c = 'Day3';
		}else if(tx.day5surveyflag__c == true){
			ts.Survey_Type__c = 'Day5';
		}else{
			ts.Survey_Type__c = 'Dropped';
		}
		ts.RecordTypeid = mapRecordtype.get(ts.Survey_Type__c);
		ts2.add(ts); 
	} 
	if(ts2 != null){
		insert ts2;  
	}   
}	

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

This was selected as the best answer
neshnesh
I have error -updateFieldwithRecType: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: RecordType.Name Trigger.updateFieldwithRecType: line 14, column 1
mapRecordtype.put(rt.Name,rt.id);
Please help me
neshnesh
Please help me - how to use trigger --below concept.
-----
List <training__c> t1 = new list <training__c>();
list <training_survey__c> ts2 = new list<training_survey__c>();
t1 = [ select day1surveyflag__c, day3surveyflag__c, day5surveyflag__c from training__c where day1surveyflag__c = true or day3surveyflag__c = true or day5surveyflag__c = true ];
for(training__c tx : t1)
{
training_survey__c ts = new training_survey__c();
ts.training__C = tx.id;
if(tx.day1surveyflag__c == true )
{
ts.Survey_Type__c = 'Day1';
}else
if(tx.day3surveyflag__c == true )
{
ts.Survey_Type__c = 'Day3';
}else
if(tx.day5surveyflag__c == true )
{
ts.Survey_Type__c = 'Day5';
}else
{
ts.Survey_Type__c = 'Dropped';
}
RecordType r = [Select id from Recordtype where sobjecttype = 'training_survey__c' and name= :ts.Survey_Type__c];
ts.RecordTypeid = r.id;
ts2.add(ts);
}
if(ts2 != null )
{
insert ts2;
}
---------------
hitesh90hitesh90

Try to use below trigger code.

 

Apex trigger:

trigger updateFieldwithRecType on training__c (after insert, after update) {
	List <training__c> t1 = new list <training__c>();
	list <training_survey__c> ts2 = new list<training_survey__c>();
	t1 = [ select day1surveyflag__c, day3surveyflag__c, day5surveyflag__c from training__c where id IN: trigger.newMap.keyset() and (day1surveyflag__c = true or day3surveyflag__c = true or day5surveyflag__c = true)];
	Map<string,Id> mapRecordtype = new Map<string,Id>();
	set<String> sSurveytype = new set<String>();
	sSurveytype.add('Day1');
	sSurveytype.add('Day3');
	sSurveytype.add('Day5');
	sSurveytype.add('Dropped');
	List<RecordType> lstRecType = [select id, Name from RecordType where sobjecttype = 'training_survey__c' and Name IN: sSurveytype];
	for(RecordType rt: lstRecType){
		mapRecordtype.put(rt.Name,rt.id);
	}
	for(training__c tx : t1){ 
		training_survey__c ts = new training_survey__c();
		ts.training__C = tx.id;
		if(tx.day1surveyflag__c == true){
			ts.Survey_Type__c = 'Day1';
		}else if(tx.day3surveyflag__c == true){
			ts.Survey_Type__c = 'Day3';
		}else if(tx.day5surveyflag__c == true){
			ts.Survey_Type__c = 'Day5';
		}else{
			ts.Survey_Type__c = 'Dropped';
		}
		ts.RecordTypeid = mapRecordtype.get(ts.Survey_Type__c);
		ts2.add(ts); 
	} 
	if(ts2 != null){
		insert ts2;  
	}   
}