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
JAY_PJAY_P 

TRIGGER WRITTING


I have two object name is mobile__c and shutdown__c both object have connection to one field bachno_c 
and there are two typs of record mobile1 and mobile2 in the field call module__c 
so for that if record is mobile1 and in shutdown__c we have one field shutdown_type if i put if stop value it will change to start 
for the record mobile2 it will not change anything how to write trigger for that???
 
pconpcon
It sounds like you're looking for someone to write the code for this project. These forums are not meant for that; you'll want to post over on the AppExchange site [1] where you can match up with a developer looking to work on your project. 

[1] https://appexchange.salesforce.com/developers
Pruthvi KankunthalaPruthvi Kankunthala

@mike_13223435 : This community is meant for guiding and helping . Start writing the trigger by taking help from Salesforce resources . 
http://www.salesforcetutorial.com/apex-trigger-in-salesforce/
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm

Let us know if you are struck somehwere .
Deepthi BDeepthi B
Hello Mike,

Can you please elaborate your question? For any assistance you can check the trailhead modules on Triggers.
https://developer.salesforce.com/trailhead/module/apex_triggers

Regards,
Deepthi
JAY_PJAY_P
trigger ShutdownDataLoad on mobile_c (before update,after insert) {
set<String> vlsShtBrncCodeSet = new Set<String>();   //values for branchcode variable
set<id> vlsSet = new set<id>();
if(trigger.isAfter && trigger.isInsert){
 if(trigger.isbefore && trigger.isUpdate){
        for(mobile_c vls :Trigger.new){
            if(vls.Options__c == 'stop'){
                vlsShtBrncCodeSet.add(vls.Branch_Code_Filter__c);
                vlsSet.add(vls.id);
            }
                
        }       
        
    }
    if(!vlsShtBrncCodeSet.IsEmpty()){
        List<series__c> assLst = [Select id,Branch_Code__c,Module_Type__c from series__c where Module_Type__c =: 'ms1' AND Branch_Code__c IN:vlsShtBrncCodeSet ];
        set<string> asstBranchCodeSet = new set<String>();
        for(series__c ast : assLst){
            if(ast.Branch_Code__c != null && ast.Branch_Code__c != '')
            asstBranchCodeSet.add(ast.Branch_Code__c);
        }
        if(!asstBranchCodeSet.isEmpty()){
            System.debug('+++Codes'+asstBranchCodeSet);
            for(mobile_c  vls :Trigger.new){
                if(vls.Options__c == 'stop'){
                    System.debug('+++Codes related'+vls.Branch_Code_Filter__c);
                    if(asstBranchCodeSet.Contains(vls.Branch_Code_Filter__c )){
                        vls.Options__c = 'Star';
                    }
                }
                
            }       

        }
    }
    
   if(trigger.isbefore && trigger.isUpdate){
        for(mobile_c  vls :Trigger.new){
            if(vls.Options__c != null){
                vlsShtBrncCodeSet.add(vls.Branch_Code_Filter__c);
                vlsSet.add(vls.id);
            }
                
        }       
        
    }
    if(!vlsShtBrncCodeSet.IsEmpty()){
        List<series__c> assLst = [Select id,Branch_Code__c,Module_Type__c from series__c where Module_Type__c =: 'ms2' AND Branch_Code__c IN:vlsShtBrncCodeSet ];
        set<string> asstBranchCodeSet = new set<String>();
        for(series__c ast : assLst){
            if(ast.Branch_Code__c != null && ast.Branch_Code__c != '')
            asstBranchCodeSet.add(ast.Branch_Code__c);
        }
        if(!asstBranchCodeSet.isEmpty()){
            System.debug('+++Codes'+asstBranchCodeSet);
            for(mobile_c  vls :Trigger.new){
                if(vls.Options__c != null){
                    System.debug('+++Codes related'+vls.Branch_Code_Filter__c);
                    if(asstBranchCodeSet.Contains(vls.Branch_Code_Filter__c )){
                        vls.Options__c = '';
                    }
                }
                
            }       

        }
    }

This code is changing ms1 for stop to start and not changing ms2 for stop to start...
pconpcon
I will start by saying that your code is pretty confusing (and will not compile since you are missing some closed braces).  Let me see if I can reiterate what you are trying to accomplish here.  You are building up a set of branch codes to get your series records (where the options is equal to stop).  Then, if your mobile__c record has a series with ms1 associated with it and it's options currently equal 'stop' then it's suppose to change to 'Star'?  But then i'm not sure what you are trying to do when the module type exists for the branch code that is equal to ms2.  Below is some base code that could help you clean up yours a little bit.  It will only look for branch codes when options__c is stop (or was changed to stop) and will get your modules.  (I think you may want to do this both as a before trigger instead of an after for insert).
 
trigger ShutdownDataLoad on mobile_c (before update, before insert) {
    Set<String> branchCodes = new Set<String>();   
    Set<Id> mobilesToActOn = new Set<Id>();

    for (Mobile__c m : Trigger.new) {
        if (
            m.Options__c.equalsIgnoreCase('Stop') &&
            (
                Trigger.isInsert ||
                m.Options__c != Trigger.oldMap.Options__c
            )
        ) {
            branchCodes.add(m.Branch_Code_Filter);
            mobilesToActOn.add(m.Id);
        }
    }

    if (!branchCodes.isEmpty()) {
        Map<String, Series__c> m1Series = new Map<String, Series__c>();
        Map<String, Series__c> m2Series = new Map<String, Series__c>();

        for (Series__c s : [
            select Branch_Code__c,
                Module_Type__c
            from Series__c
            where Branch_Code__c in :branchCodes
                (
                    Module_Type__c = 'ms1' or
                    Module_Type__c = 'ms2'
                )
        ]) {
            if (s.Module_Type__c == 'ms1') {
                m1Series.put(s.Branch_Code__c, s);
            } else {
                m2Series.put(s.Branch_Code__c, s);
            }
        }   

        for (Id id : mobilesToActOn) {
            Mobile__c m = Trigger.newMap.get(id);

            if (m1Series.containsKey(m.Branch_Code_Filter)) {
                m.Options__c = 'Star';
            } else if (m2Series.containsKey(m.Branch_Code_Filter)) {
                // Do something
            }
        }
    }
}
NOTE: This code has not been tested and may contain typographical or logical errors
 
JAY_PJAY_P
YES WHAT I AM TRYING TO IS IF MS1 IT WILL CHANGE STOP TO START BUT MS2 IT WILL NOT CHANGE ANYTHING ?
pconpcon
Please do not yell.  Negative behavior like this is not taken lightly on these forums.  The code above will do what you want with some minor modifications.
 
trigger ShutdownDataLoad on mobile_c (before update, before insert) {
    Set<String> branchCodes = new Set<String>();
    Set<Id> mobilesToActOn = new Set<Id>();

    for (Mobile__c m : Trigger.new) {
        if (
            m.Options__c.equalsIgnoreCase('Stop') &&
            (   
                Trigger.isInsert ||
                m.Options__c != Trigger.oldMap.Options__c
            )
        ) { 
            branchCodes.add(m.Branch_Code_Filter);
            mobilesToActOn.add(m.Id);
        }
    }

    if (!branchCodes.isEmpty()) {
        Map<String, Series__c> m1Series = new Map<String, Series__c>();
        Map<String, Series__c> m2Series = new Map<String, Series__c>();

        for (Series__c s : [
            select Branch_Code__c,
                Module_Type__c
            from Series__c
            where Branch_Code__c in :branchCodes
                (   
                    Module_Type__c = 'ms1' or
                    Module_Type__c = 'ms2'
                )
        ]) {
            if (s.Module_Type__c == 'ms1') {
                m1Series.put(s.Branch_Code__c, s);
            } else {
                m2Series.put(s.Branch_Code__c, s);
            }
        }

        for (Id id : mobilesToActOn) {
            Mobile__c m = Trigger.newMap.get(id);

            if (
                m1Series.containsKey(m.Branch_Code_Filter) &&
                !m2Series.containsKey(m.Branch_Code_Filter)
            ) { 
                m.Options__c = 'Start';
            }
        }
    }
}

The code above will update the Options__c to 'Start' if there is a matching Series__c with an 'ms1' but not an 'ms2'.  If there is just an 'ms2' or no matching Series__c, the Options__c will remained unchanged.  The way it is currently written, this will only affect Mobile__c that are inserted with an Options__c of 'Stop' or have the Options__c changed from anything to 'Stop'.  You can modify that logic by altering lines 9-11.