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
dd 

I am getting this error for following code: LocationTrigger: execution of Beforeinsert caused by System StringException: Invalid id: COEP Trigger LocationTngger: line 6

trigger LocationTrigger on Student c (before insert) 2 List<College_cs col [SELECT Name, state c from College_c];

for (Student c stu:Trigger.new) {

for (College c coll:col){ if(coll.state_cstu.state__c){

stu.college_name_c=col1.Name:
}
}
}
}
 
Best Answer chosen by d
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try below trigger.
 
trigger PopulateState on Student__c (before insert) {
set<String> mapst = new set<String>(); 
    
    
    For(Student__c st:Trigger.new){
if(st.state__c!=null)
        mapst.add(st.state__c);
    }
    if(mapst.size()>0){
   List<College__c> collist=[select id,State__c from College__c where state__c in :mapst];
        Map<String,Id> mapcoll= new Map<String,Id>();
        For(College__C co: collist){
            mapcoll.put(co.state__C,co.id);
        }
    for(Student__c st:Trigger.new){
        if(mapcoll.containsKey(st.State__c)){
            st.College__c=mapcoll.get(st.State__c);
        }
    }
    }

}

Thanks,
​​​​​​​

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi ,

Can you confirm what you are trying to achieve using this trigger so can modify this and share you.

Thanks,
 
dd
While creating  student record based on the state of  student college name with same state will be assigned directly 
dd
While creating student record based on the state of student ,college name with same state will be assigned directly.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Just can you confirm the below.

While creating Student record you select the college(Lookup field) so the state on student record shoule populated from State on that college record?

Thanks,
 
dd
Yes
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below trigger on Student object.
 
trigger PopulateState on Student__c (before insert,before update) {
set<id> mapst = new set<id>(); 
    
    
    For(Student__c st:Trigger.new){
if(st.College__c!=null)
        mapst.add(st.College__c);
    }
    if(mapst.size()>0){
   map<id,College__c> mapcoll=new map<id,College__c>([select id,State__c from College__c where id in :mapst]);
    
    for(Student__c st:Trigger.new){
        if(mapcoll.containsKey(st.College__c)){
            st.State__c=mapcoll.get(st.College__c).state__c;
        }
    }
    }

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
dd
Based on state college name should populate
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

If there are mutiple colleges with same state can we consider any college?

Thanks,
 
dd
Yeah
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try below trigger.
 
trigger PopulateState on Student__c (before insert) {
set<String> mapst = new set<String>(); 
    
    
    For(Student__c st:Trigger.new){
if(st.state__c!=null)
        mapst.add(st.state__c);
    }
    if(mapst.size()>0){
   List<College__c> collist=[select id,State__c from College__c where state__c in :mapst];
        Map<String,Id> mapcoll= new Map<String,Id>();
        For(College__C co: collist){
            mapcoll.put(co.state__C,co.id);
        }
    for(Student__c st:Trigger.new){
        if(mapcoll.containsKey(st.State__c)){
            st.College__c=mapcoll.get(st.State__c);
        }
    }
    }

}

Thanks,
​​​​​​​
This was selected as the best answer
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Is the code working as expected?

Thanks,
 
dd
Haven't checked yet
dd
Last part of the code helped .thank you
Miguel GrateMiguel Grate
Studying in college presents us with a unique set of benefits and challenges that contribute to our educational growth. College education opens doors to endless opportunities, allowing us to gain specialized knowledge and skills in our chosen fields. One of the significant advantages of pursuing a college degree is the exposure to a diverse range of subjects and perspectives. This exposure broadens our horizons, fosters critical thinking, and nurtures a deeper understanding of the world around us. Additionally, college provides a platform for personal and intellectual growth, where we can develop valuable skills such as research, communication, and problem-solving. However, it's important to acknowledge the challenges that come with college life. The academic workload can be demanding, requiring dedication and time management skills. Balancing coursework, assignments, and exams can be overwhelming at times. Furthermore, writing high-quality essays and papers that meet academic standards can be a daunting task. To overcome these challenges, it's beneficial to seek support from reliable resources. EduBirdie offers a comprehensive nursing essay writing service that can assist nursing students in crafting well-researched and professionally written essays. With the expertise of their skilled writers, students can save time and ensure the delivery of top-notch essays that demonstrate their understanding of nursing concepts and theories. By utilizing EduBirdie's nursing essay writing service, students can enhance their academic performance, improve their writing skills, and gain a deeper comprehension of nursing topics. To access this valuable resource, visit EduBirdie at https://essays.edubirdie.com/nursing-essay-writing-service. Their team of experienced writers will provide the guidance and support needed to excel in nursing studies. Remember, college is an exciting journey of self-discovery and intellectual development. Embracing the benefits and effectively managing the challenges will pave the way for a successful educational experience. With the assistance of EduBirdie's nursing essay writing service, students can thrive academically and confidently navigate the intricacies of their nursing studies.