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
Hakim Upadhyay 45Hakim Upadhyay 45 

Record is creating with record id, not name

Hi All,

I am new in apex coding, I am trying to write trigger and this is my code.

My requirement is whenever I am creating new visitor record in the system( when I select multiselect picklist value which is 'Feve' and 'Dry Cough') new patient record should create under patient record.

My code is working fine, but patient record creates with ID, not name.

When I select only 1 value from the mutli-select picklist, then patient record creates with name.
trigger VisitorChangedIntoPatient on Visitor__c (after insert) {

    List<Patient__c> pat = new List<Patient__c>();
    
    for(Visitor__c visitor : Trigger.New)
    {
         Patient__c p = new Patient__c();
        
        if(trigger.isInsert && Visitor.Covid_19_Symtomp__c=='Fever' && Visitor.Covid_19_Symtomp__c=='DryCough')
        {
            p.id=visitor.id;
            p.Name=visitor.Name+'SQC';
            p.IsInfected__c=TRUE;
        }
        pat.add(p);
       
    }
    
    insert pat;
    
    
}
Best Answer chosen by Hakim Upadhyay 45
Agustin BAgustin B
HI Hakim verify that the multipicklist shows a list of strings divided by commas, so you will have "Fever,DryCough". You could use a contains for example to validate both.

if this helps you, please mark it as a best answer, it may help others.

All Answers

Divyansh Verma 10Divyansh Verma 10
Since you are new to coding, just my suggestion you should write this trigger first where you create a field "sales_Rep" on Account object and print the Account Owner name in the sales_Rep.
 For this code and for your code you can refer the below sample code.

trigger SalesRepOnAccount_Owner on Account (before insert,before update) {
    Set<Id> newAccountList=new Set<Id>();
    for(Account a:trigger.new)
    {
         newAccountList.add(a.OwnerId);   
        
    }
    Map<id,User> mapid= new Map<id,User>([select name from user where id in:newAccountList]);
    for(Account acc:trigger.new)
    {
        user usr=mapid.get(acc.OwnerId);
        acc.sales_Rep__c=usr.Name;

     }    

}


Thanks & regards
Divyansh Verma
Agustin BAgustin B
HI Hakim verify that the multipicklist shows a list of strings divided by commas, so you will have "Fever,DryCough". You could use a contains for example to validate both.

if this helps you, please mark it as a best answer, it may help others.
This was selected as the best answer
SarvaniSarvani
Hi Hakim,

Is there any relation between the objects Visitor__c and Patient__c. I see the code inside if loop in line 11 you are trying to set the id of patient record  ( p.id=visitor.id;) which is not possible. Salesforce will generate Ids once the record is created sucessfully. But  if you are adding the patient record to Visitor record as related list record with the related reference field (lookup field) look for correct field API name.
Go to the Patient object-> fields & Relationships-> Check if there is any field with reference to Visitor object as lookup or master detailed. Copy the API name if its there and try replacing the p.id with p.API_NAME. (p.Visitor__c=Visitor.id;)

If there is no relation between two objects but just create records based on condition you can remove line 11 from your code.

Hope this helps! Please mark as best if it solves your problem

Thanks
Hakim Upadhyay 45Hakim Upadhyay 45
@Sarvani: sorry that was my mistake. 
Please ignore this line :  ( p.id=visitor.id;)

Records are creating but with ID not name in Patient object.
Hakim Upadhyay 45Hakim Upadhyay 45
@Augstin: I tried with semicolumn and contains, but both are not working 
Hakim Upadhyay 45Hakim Upadhyay 45
Hello Eveyone, 

I got the answer. I used below solution:
if(Visitor.Covid_19_Symtomp__c.contains('Fever') && Visitor.Covid_19_Symtomp__c.contains('DryCough'))


Thansk For your help.
SarvaniSarvani
Hakim,

Please try with below code. It worked for me using contains. Also, make sure your visitor record's name field value is not blank.
 
trigger VisitorChangedIntoPatient on Visitor__c(after insert) {

    List<Patient__c> pat = new List<Patient__c>();
    
    for(Visitor__c visitor : Trigger.New)
    {
         Patient__c p = new Patient__c();
        
        if(trigger.isInsert  && (visitor.Covid_19_Symtomp__c.contains( 'Fever' ) && visitor.Covid_19_Symtomp__c.contains( 'DryCough' )) )
        {
            //p.id=visitor.id;
            p.Name= visitor.Name+'SQC';
            p.IsInfected__c=TRUE;
             pat.add(p);
        }
        else{ // do nothing}
       
     }
    
    if(pat.size()>0)
    insert pat;
    
    
}

The above code will create patient records only when a new visitor record is beign created with Covid_19_Symtomp__c having fever and dry cough selected with any other values.

Thanks