• Syed F Raza
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 8
    Questions
  • 5
    Replies
Hi All I have the following code
// I am creating a Class called LClass
//This class will have 3 data members
//Two of them primitive and one list of this //class

public class LClass {

public Integer A;
public String B;
public List<LClass> Llist = new List<LClass>();

//So far this code is saving without any issue on
//Developer Console

}

//Then in Execute Anonymous Window I wrote the following code

//I am instantiating the class 
LClass LC = new LClass();
LC.A=100;
LC.B='Myself';

//Now i would like to add this object in the Llist I created in the class.

Llist.add(LC);

//When i execute this code, it gives  me the following error.


Variable does not exist: Llist

Please advise why I am getting this error.
 
Hi,
Please refer to the following code where I am querying on a Custom Object STUDENT to return my results where country name is Netherlands. If the result comes back with records, I am updating the country name to Switzerland.
 
List<Student__c> sList = [Select Name, Country__c from Student__c
                         WHERE Country__c ='Netherlands'];

for(Student__c std:sList){
    if(std.country__c=='Netherlands'){
        std.Country__c='Switzerland';
        }
}

Update sList;
system.debug(sList.size());

This code runs as per expectations and queried records are updated as per the condition set.

However I have noticed that some of the developers would use 2 Lists for such operation where the first list would be used for the SOQL query and in the second list they will add the results of the records where Country field is being updated. The code is as follows:
List<Student__c> sList = [Select Name, Country__c from Student__c
                         WHERE Country__c ='Netherlands'];


List<Student__c> sList2 = new List<Student__c>();

for(Student__c std:sList){
    if(std.country__c=='Netherlands'){
        std.Country__c='Switzerland';
        sList2.add(std);
    }
}


Update sList2;
system.debug(sList2.size());

Please advise what is the difference between these 2 approaches.
Are they equally same as results are being achieved by both code.

 
Hi All,

I have an existing trigger on a custom object called Student for insert and update events. I wanted to add a condition to restrict deletion of record(s) on object Student. The trigger code I wrote is as follows:

 
trigger TriggerOnStudent on Student__c (before insert, before update, before delete) {
  TriggerHandlerOnStudent.AddSalutation(trigger.new);
  TriggerHandlerOnStudent.AddCampus(trigger.new);
   TriggerHandlerOnStudent.GiveDiscount(Trigger.new); 
    TriggerHandlerOnStudent.doNotDeleteError(Trigger.old); 
    
      
}

The code of trigger handler class is as follows:
 
public class TriggerHandlerOnStudent {
  public static void AddSalutation(List<Student__c> stuList){
        for(Student__c stu:stuList)
        if(stu.Gender__c!=null && stu.Gender__c=='Male')
        stu.Name='Mr '+stu.Name;
    
        else if(stu.Gender__c!=null && stu.Gender__c=='Female')
        stu.Name='Miss '+stu.Name; 
        
    }   
     
      public static void AddCampus(List<Student__c> studList1){
        for(Student__c stu1 : StudList1)
            if(stu1.Campus__c==null)
            stu1.campus__c='Wayne';
        }     
    
    public static void GiveDiscount(List<Student__c> studList2){
        for (Student__c stu2:studList2){
            if(stu2.Fees__c != null && stu2.County__c=='LA County')
                stu2.Fees__c=stu2.Fees__c - (stu2.Fees__c*.1);
            else if (stu2.Fees__c != null && stu2.County__c=='Orange County')
                stu2.Fees__c = stu2.Fees__c - (stu2.Fees__c*.2);
            else if (stu2.Fees__c != null && stu2.County__c =='Riverside County')
                stu2.Fees__c = stu2.Fees__c - (stu2.Fees__c*.3);
        }
        
        }

    public static void doNotDeleteError(List<student__c> studList3) {
        
        for(Student__c stu3:StudList3){
        stu3.addError('You cannot delete this record');
    }
} 
    
}

Once I activated this trigger, this trigger is working on insert and update events but it does not work when I try to delete a record. Even though it does not allow me delete a record but it gives me an error that I do not have rights as user and I need to contact administrator.

I deactivated this trigger.

Then I wrote a seperate trigger on Student object to restrict deletion of records and this time it works. The code of trigger is as follows:
trigger TriggerOnStudent on Student__c (before delete) {

       for(Student__c student1: trigger.old){
           student1.adderror('You cannot delete this record');
       }
    
      
}

Any idea what was wrong with the original trigger I wrote.
Defintely it was not issue of permissions as:
  1. I am the administrator.
  2. Not with the second trigger, code is working perfect


 
HI All,

I am writing a trigger where if the Contact being created is not related to an account, then we should assign this contact to a particular account:
 
trigger trigger_Contact1 on Contact (before insert) {


      List<contact> conlist = new List<contact>(); 
      for(Contact con :trigger.new){
      if(Accountid==null)
      Account.id=0015G00001dSXtWQAW;
      conlist.add(con);
      }
     
    insert conlist;
}

The account id I have mentioned is of an account to which I would like to assign all contacts to in case if the Account is not selected for such contacts at time of insert.
 
Hello!! I am curently new to learning development in Salesforce. As I understand that Visualforce is being phased out and it is being replaced by Lightning Aura and Lightning Web Components. Should I learn Visualforce or should I start with Apex and Lightning Aura? Please advise.
Hi All,

Please find below my apex and visualforce code:
 
public class Learn1 {
    public string aname{get;set;}
    public string arating{get;set;}
    public string aphone{set;get;}
    
    Account acc=new Account();
    
    public void getName(){
    	acc.Name=aname;
        acc.Rating=arating;
        acc.Phone=aPhone;
        
        insert acc;
        aname=Null;
        aphone=Null;
		}
}
<apex:page controller="Learn1">
    <apex:form>
    	<apex:pageblock>
            <apex:pageblocksection>
                <apex:outputLabel value="Name of Account" style="width:100px;"/>
            <apex:inputText value="{!aname}"/>
            </apex:pageblocksection>    
    		
        	<apex:pageblocksection>
            <apex:outputLabel value="Phone" />
        	<apex:inputText value="{!aphone}"/>
            </apex:pageblocksection>	
    
            <apex:pageblockbuttons>
            	<apex:commandButton value="Submit" action="{!getName}"/>
            </apex:pageblockbuttons>
    </apex:pageblock>
        
        
	</apex:form>
</apex:page>

On the Visualforce page, when I input the values for Name and Phone,  the value gets inserted and I can see the new record in Accounts. Also the values in the form become null.

However once I enter a different set of values, I get the following error:

System.DmlException: Insert failed. First exception on row 0 with id 001f400001MIzTMAA1; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!getName}' in component <apex:commandButton> in page learnvf1: Class.Learn1.getName: line 13, column 1
Class.Learn1.getName: line 13, column 1


Please advise. Thanks!!

 
Hi All,

In Apex, in a given class, how many times can we re-assign the value of a static variable. Also I belive we can initialize a static variable in static block, can we also initialize static variable in static method. Thanks!!
HI All,

I am trying to do a standard controller list for Object LEAD. While using Campaign to show as a column in the table, I am getting the following error:

Error: Invalid Field CampaignID for sObject Lead

Since Campaign is a LookUp field, I made Campaign to CampaignID. But the problem presists.

Code I wrote is as follows:
<apex:page sidebar="false" standardcontroller="Lead" recordSetVar="ld1">
    
    <apex:pageBlock title="List of Leads" tabStyle="Lead">
    
        <apex:pageBlockTable value="{!ld1}" var="l1">
        
            <apex:column value="{!l1.Name}" />
            <apex:column value="{!l1.EMail}" />
            <apex:column value="{!11.CampaginID}" />
            <apex:column value="{!l1.CleanStatus}" />
            <apex:column value="{!l1.DandbCompany}" />
            <apex:column value="{!l1.HasOptedOutOfEmail}" />
        
        </apex:pageBlockTable> 
    
    </apex:pageBlock>

</apex:page>

Any suggestions!!
 
Hi All I have the following code
// I am creating a Class called LClass
//This class will have 3 data members
//Two of them primitive and one list of this //class

public class LClass {

public Integer A;
public String B;
public List<LClass> Llist = new List<LClass>();

//So far this code is saving without any issue on
//Developer Console

}

//Then in Execute Anonymous Window I wrote the following code

//I am instantiating the class 
LClass LC = new LClass();
LC.A=100;
LC.B='Myself';

//Now i would like to add this object in the Llist I created in the class.

Llist.add(LC);

//When i execute this code, it gives  me the following error.


Variable does not exist: Llist

Please advise why I am getting this error.
 
Hi,
Please refer to the following code where I am querying on a Custom Object STUDENT to return my results where country name is Netherlands. If the result comes back with records, I am updating the country name to Switzerland.
 
List<Student__c> sList = [Select Name, Country__c from Student__c
                         WHERE Country__c ='Netherlands'];

for(Student__c std:sList){
    if(std.country__c=='Netherlands'){
        std.Country__c='Switzerland';
        }
}

Update sList;
system.debug(sList.size());

This code runs as per expectations and queried records are updated as per the condition set.

However I have noticed that some of the developers would use 2 Lists for such operation where the first list would be used for the SOQL query and in the second list they will add the results of the records where Country field is being updated. The code is as follows:
List<Student__c> sList = [Select Name, Country__c from Student__c
                         WHERE Country__c ='Netherlands'];


List<Student__c> sList2 = new List<Student__c>();

for(Student__c std:sList){
    if(std.country__c=='Netherlands'){
        std.Country__c='Switzerland';
        sList2.add(std);
    }
}


Update sList2;
system.debug(sList2.size());

Please advise what is the difference between these 2 approaches.
Are they equally same as results are being achieved by both code.

 
Hi All,

I have an existing trigger on a custom object called Student for insert and update events. I wanted to add a condition to restrict deletion of record(s) on object Student. The trigger code I wrote is as follows:

 
trigger TriggerOnStudent on Student__c (before insert, before update, before delete) {
  TriggerHandlerOnStudent.AddSalutation(trigger.new);
  TriggerHandlerOnStudent.AddCampus(trigger.new);
   TriggerHandlerOnStudent.GiveDiscount(Trigger.new); 
    TriggerHandlerOnStudent.doNotDeleteError(Trigger.old); 
    
      
}

The code of trigger handler class is as follows:
 
public class TriggerHandlerOnStudent {
  public static void AddSalutation(List<Student__c> stuList){
        for(Student__c stu:stuList)
        if(stu.Gender__c!=null && stu.Gender__c=='Male')
        stu.Name='Mr '+stu.Name;
    
        else if(stu.Gender__c!=null && stu.Gender__c=='Female')
        stu.Name='Miss '+stu.Name; 
        
    }   
     
      public static void AddCampus(List<Student__c> studList1){
        for(Student__c stu1 : StudList1)
            if(stu1.Campus__c==null)
            stu1.campus__c='Wayne';
        }     
    
    public static void GiveDiscount(List<Student__c> studList2){
        for (Student__c stu2:studList2){
            if(stu2.Fees__c != null && stu2.County__c=='LA County')
                stu2.Fees__c=stu2.Fees__c - (stu2.Fees__c*.1);
            else if (stu2.Fees__c != null && stu2.County__c=='Orange County')
                stu2.Fees__c = stu2.Fees__c - (stu2.Fees__c*.2);
            else if (stu2.Fees__c != null && stu2.County__c =='Riverside County')
                stu2.Fees__c = stu2.Fees__c - (stu2.Fees__c*.3);
        }
        
        }

    public static void doNotDeleteError(List<student__c> studList3) {
        
        for(Student__c stu3:StudList3){
        stu3.addError('You cannot delete this record');
    }
} 
    
}

Once I activated this trigger, this trigger is working on insert and update events but it does not work when I try to delete a record. Even though it does not allow me delete a record but it gives me an error that I do not have rights as user and I need to contact administrator.

I deactivated this trigger.

Then I wrote a seperate trigger on Student object to restrict deletion of records and this time it works. The code of trigger is as follows:
trigger TriggerOnStudent on Student__c (before delete) {

       for(Student__c student1: trigger.old){
           student1.adderror('You cannot delete this record');
       }
    
      
}

Any idea what was wrong with the original trigger I wrote.
Defintely it was not issue of permissions as:
  1. I am the administrator.
  2. Not with the second trigger, code is working perfect


 
Hi All,

Please find below my apex and visualforce code:
 
public class Learn1 {
    public string aname{get;set;}
    public string arating{get;set;}
    public string aphone{set;get;}
    
    Account acc=new Account();
    
    public void getName(){
    	acc.Name=aname;
        acc.Rating=arating;
        acc.Phone=aPhone;
        
        insert acc;
        aname=Null;
        aphone=Null;
		}
}
<apex:page controller="Learn1">
    <apex:form>
    	<apex:pageblock>
            <apex:pageblocksection>
                <apex:outputLabel value="Name of Account" style="width:100px;"/>
            <apex:inputText value="{!aname}"/>
            </apex:pageblocksection>    
    		
        	<apex:pageblocksection>
            <apex:outputLabel value="Phone" />
        	<apex:inputText value="{!aphone}"/>
            </apex:pageblocksection>	
    
            <apex:pageblockbuttons>
            	<apex:commandButton value="Submit" action="{!getName}"/>
            </apex:pageblockbuttons>
    </apex:pageblock>
        
        
	</apex:form>
</apex:page>

On the Visualforce page, when I input the values for Name and Phone,  the value gets inserted and I can see the new record in Accounts. Also the values in the form become null.

However once I enter a different set of values, I get the following error:

System.DmlException: Insert failed. First exception on row 0 with id 001f400001MIzTMAA1; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!getName}' in component <apex:commandButton> in page learnvf1: Class.Learn1.getName: line 13, column 1
Class.Learn1.getName: line 13, column 1


Please advise. Thanks!!