• Visithra
  • NEWBIE
  • 20 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 12
    Questions
  • 3
    Replies
Create a future method to calculate total number of contacts under each account and update it in the account custom field names as number of contacts field, implement this using future method.
 
Get help with this trigger, when an no.contacts in account is entered same contacts should be created with its related account, and it should update as well.for eg when created with 3 no of contacts and while updating as 5, total 5 contacts should be created.,
i have trigger for inserted, got stuck in updation,
rigger AccountContactCaaseStudy3 on Account (after insert) {
    
   Switch On Trigger.OperationType
   {
       When AFTER_INSERT
       {
           List<Contact> conList=new List<Contact>();
           for(Account accRecord:trigger.new)
           {
               for(integer i=1;i<=accRecord.NumberofLocations__c; i++)
               {
                   Contact ConRecord=new Contact();
                   ConRecord.AccountID=accRecord.Id;
                   ConRecord.LastName= accRecord.Name +'Contact' +i;
                   conList.add(ConRecord);
                   
               }
               
           }
           if(conList.size()>0)
               insert conList;
       }
       
       
   }

}

Thanks.
Here is my code, 
//1/to get parent unique id
Set<Id> accountIDs=new Set<Id>();
            for(Opportunity oppRecord:Trigger.new)
            {
                if(oppRecord.AccountId!=null)
                    accountIDs.add(oppRecord.AccountId);
            }
//2. parent to child relationship 
            List<Account> accountList=[Select ID, Rating, Highest_opp_record__c,
                                       (Select ID, StageName, Amount,AccountID FROM Opportunities Where Amount>=50000)
                                       FROM Account Where ID in:accountIDs
                                       ];
//3. update the amount in account record
            List<Account> accList=new List<Account>();                      
            for(Account accRecord:accountList)
            {
                accRecord.Highest_opp_record__c=(accRecord.Opportunities).Amount;
                accList.add(accRecord);
                
            }
            if(accList.size()>0)
                update acclist;
        }
While creating a record in opportunity am facing the below error:
System.QueryException: List has more than 1 row for assignment to SObject Trigger.OpportunityTriggerAmount: line 27, column 1
 
Need to block the user for updating the address field more that 5 times, store the number of times allowed in a cutom label and use in trigger, if require create a custom fields to track number of changes so far completed.
Here is my code,
When BEFORE_INSERT
        {
            for(Account accRecord: Trigger.new){
                if(accRecord.Rating=='Hot')
                {
                    if(String.isBlank(accRecord.AnnualRevenue))
                    {
                        accRecord.AnnualRevenue=20000;
                    }
                }
            }
        }

Getting error as:
Method does not exist or incorrect signature: void isBlank(Decimal) from the type String
Please help me with the trigger code for the below scenario,
When an lead is updated status as closed-Not converted, the description field  should be entered otherwise it should block the user to update lead.

 
Map<ID, AggregateResult> accountMap=new Map<ID, AggregateResult>([Select 
                                                                 count(ID) ratingCount, Rating
                                                                 From Account
                                                                 Group by Rating
                                                                ]);
for(AggregateResult accRecord:accountMap)
{
    system.debug('ratingCount');
    system.debug(accRecord.get('Rating'));
}

Getting error as:: Line: 6, Column: 1
Loop must iterate over collection: Map<Id,AggregateResult>
public static void displaySobjectRecords(string objName,List<string> Fields, string field, string operator, string value){
        
        //List<string> objFields=new List<string>{'ID','Name'};
        //objFields.add('ID');
        //objFields.add('Name');
        
           String QueryStr='Select '+string.join(Fields, ',')+' From '+objName WHERE +field+' ' +operator+ ' ' +value;
        
        List<Sobject> lstObjects=Database.query(QueryStr);
        for(Sobject obj:lstObjects){
            system.debug('========');
            system.debug(obj.get('Name'));
            system.debug(obj.get('ID'));
            system.debug('========');
           
        }
        
    }

Thanks
public static void maxSalaryOFPositions(string position){
    List<Employee__c> existingEmployees = [SELECT Id, Name, Position__c, Salary__c FROM Employee__c];
     integer MaxSalary= existingEmployees[0].Salary__c;
    for(Employee__c emp:existingEmployees){
        
        if((emp.Position__c==position)&&(emp[1].Salary__c>MaxSalary)){
            MaxSalary=emp[1].Salary__c;
        }
        
    }system.debug('The max salary of'+position+ 'and its salary is'+MaxSalary);
        
   
       
}

pls help me with my code and here is my code. am getting 2 errors as illegal assignemnt from Decimal to integer and Expression must be list type:Employee__c
 
my code is
public class Emp {
    public boolean ifExists=false;
    
    
    public void insertEmp(string empName)
    {
        List<Employee__c> empl=[SELECT 
                                Id, Name 
                                FROM Employee__c];
        for(Employee__c E: empl){
                if(empl.Name!=empName){
                    insert empl;
                }
        }if(ifExists==true){
            system.debug('Record already exists with the same employee name!..Create Another Name.!');
        }
            }
        }


here it is allowing me to insert everytime with same name..
 
Create a future method to calculate total number of contacts under each account and update it in the account custom field names as number of contacts field, implement this using future method.
 
Map<ID, AggregateResult> accountMap=new Map<ID, AggregateResult>([Select 
                                                                 count(ID) ratingCount, Rating
                                                                 From Account
                                                                 Group by Rating
                                                                ]);
for(AggregateResult accRecord:accountMap)
{
    system.debug('ratingCount');
    system.debug(accRecord.get('Rating'));
}

Getting error as:: Line: 6, Column: 1
Loop must iterate over collection: Map<Id,AggregateResult>
public static void displaySobjectRecords(string objName,List<string> Fields, string field, string operator, string value){
        
        //List<string> objFields=new List<string>{'ID','Name'};
        //objFields.add('ID');
        //objFields.add('Name');
        
           String QueryStr='Select '+string.join(Fields, ',')+' From '+objName WHERE +field+' ' +operator+ ' ' +value;
        
        List<Sobject> lstObjects=Database.query(QueryStr);
        for(Sobject obj:lstObjects){
            system.debug('========');
            system.debug(obj.get('Name'));
            system.debug(obj.get('ID'));
            system.debug('========');
           
        }
        
    }

Thanks
my code is
public class Emp {
    public boolean ifExists=false;
    
    
    public void insertEmp(string empName)
    {
        List<Employee__c> empl=[SELECT 
                                Id, Name 
                                FROM Employee__c];
        for(Employee__c E: empl){
                if(empl.Name!=empName){
                    insert empl;
                }
        }if(ifExists==true){
            system.debug('Record already exists with the same employee name!..Create Another Name.!');
        }
            }
        }


here it is allowing me to insert everytime with same name..