• Vijay Nagella
  • NEWBIE
  • 50 Points
  • Member since 2021
  • Solution Architect
  • Steadfast Consultancy Services

  • Chatter
    Feed
  • 2
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 6
    Replies

Ian Lin 585
Can anyone answer to the following scenario?. I need to create a account after a contact is created with the contact name as account name. If the account name with the same contact name exists assign the accountId to Id of existing account otherwise create a account with the name of the contact name and assign the new account id to contact.
My code is :
trigger CreateDefaultContact on Contact (after insert) {
       ContactDefault.createContact(trigger.new);
}

public class ContactDefault {
    
    public static void createContact(List<Contact> contactList)
    {
            List<Contact> needAcctsLst = new List<Contact>();
        for(Contact c : contactList){
            needAcctsLst.add(c);
        }
            List<Account> accountList = new List<Account>();
            List<Contact> conLst = new List<Contact>();
            for(Contact conObj : needAcctsLst)
            {
                String cname=conObj.FirstName+' '+conObj.LastName;
                Account acc = [select Id,name from Account where name=:cname LIMIT 1];
                if (acc!=null && acc.Id!=null){
                    System.debug( 'inside if ');
                    conObj.AccountId = acc.Id;
            }else{
                Account accObj = new Account();
                accObj.Name = cname;
                conObj.AccountId = accObj.Id;
                accountList.add(accObj);
                System.debug( 'inside else ');
            }
                
                conLst.add(conObj);
            }
            
            if(accountList.size() > 0)
            {
                insert accountList;
                
            }
           if(conLst.size() > 0)
            {
                System.debug( 'updating contact ');
                update conLst;
            }
    }
 }

@isTest
public class ContactDefaultTest {
    @testSetup
    static void testSetUpData() 
    {
        Account testAccount = new Account();
        testAccount.Name='aaa ttt' ;
        insert testAccount;
    }
    @istest
    public static void testMethod1(){
        Contact cont = new Contact();
        cont.FirstName='aaa';
        cont.LastName='ttt';
        insert cont;
        String cname=cont.FirstName+' '+cont.LastName;
        Account acc = [select Id,name from Account where name=:cname LIMIT 1];
        System.assertEquals(acc.Id,cont.AccountId,'Equals .. passed');
    }
}



It throws the below error. Can anyone help to fix this issue.?
Thanks.
14:59:37:160 EXCEPTION_THROWN [15]|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateDefaultContact: execution of AfterInsert
14:49:50:170 FATAL_ERROR System.FinalException: Record is read-only   
Hi, this is the trigger I have developed, I need to get accounts information, I need to reach a field called Type on the accont.


trigger Error_Opp on Opportunity (before insert) {
  for( Opportunity oppsupplier: trigger.new ){
       if(oppsupplier.Opp_Count__c == 1 && oppsupplier.type == 'Supplier')
        {
                 oppsupplier.addError('This is a Supplier Account, it can have only one Opportunity.');
       }
   }
}


Thanks 

Ian Lin 585
Can anyone answer to the following scenario?. I need to create a account after a contact is created with the contact name as account name. If the account name with the same contact name exists assign the accountId to Id of existing account otherwise create a account with the name of the contact name and assign the new account id to contact.
My code is :
trigger CreateDefaultContact on Contact (after insert) {
       ContactDefault.createContact(trigger.new);
}

public class ContactDefault {
    
    public static void createContact(List<Contact> contactList)
    {
            List<Contact> needAcctsLst = new List<Contact>();
        for(Contact c : contactList){
            needAcctsLst.add(c);
        }
            List<Account> accountList = new List<Account>();
            List<Contact> conLst = new List<Contact>();
            for(Contact conObj : needAcctsLst)
            {
                String cname=conObj.FirstName+' '+conObj.LastName;
                Account acc = [select Id,name from Account where name=:cname LIMIT 1];
                if (acc!=null && acc.Id!=null){
                    System.debug( 'inside if ');
                    conObj.AccountId = acc.Id;
            }else{
                Account accObj = new Account();
                accObj.Name = cname;
                conObj.AccountId = accObj.Id;
                accountList.add(accObj);
                System.debug( 'inside else ');
            }
                
                conLst.add(conObj);
            }
            
            if(accountList.size() > 0)
            {
                insert accountList;
                
            }
           if(conLst.size() > 0)
            {
                System.debug( 'updating contact ');
                update conLst;
            }
    }
 }

@isTest
public class ContactDefaultTest {
    @testSetup
    static void testSetUpData() 
    {
        Account testAccount = new Account();
        testAccount.Name='aaa ttt' ;
        insert testAccount;
    }
    @istest
    public static void testMethod1(){
        Contact cont = new Contact();
        cont.FirstName='aaa';
        cont.LastName='ttt';
        insert cont;
        String cname=cont.FirstName+' '+cont.LastName;
        Account acc = [select Id,name from Account where name=:cname LIMIT 1];
        System.assertEquals(acc.Id,cont.AccountId,'Equals .. passed');
    }
}



It throws the below error. Can anyone help to fix this issue.?
Thanks.
14:59:37:160 EXCEPTION_THROWN [15]|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateDefaultContact: execution of AfterInsert
14:49:50:170 FATAL_ERROR System.FinalException: Record is read-only   
Hi, this is the trigger I have developed, I need to get accounts information, I need to reach a field called Type on the accont.


trigger Error_Opp on Opportunity (before insert) {
  for( Opportunity oppsupplier: trigger.new ){
       if(oppsupplier.Opp_Count__c == 1 && oppsupplier.type == 'Supplier')
        {
                 oppsupplier.addError('This is a Supplier Account, it can have only one Opportunity.');
       }
   }
}


Thanks 
i m writing trigger, created custom field  on product object  Total Quantity and Available Quantity .
i have to fill Available Quantity field when opportunityLineItem is Created
code is successfully deployed but not update  Available Quantity field

trigger AvailableQuantityonProductTrigger on OpportunityLineitem (after insert) {
    integer remainingQuantity=0;
    set<id> oliIds=new set<id>();
for(OpportunityLineitem o:Trigger.new){
    if(o.Quantity!=null){
    oliIds.add(o.id);
    }
 }
 list<OpportunityLineitem> oliList=[SELECT id,Quantity,Product2.Name,Product2.Total_Quantity__c,
 Product2.Available_Quantity__c FROM OpportunityLineitem WHERE id IN:oliIds];
 for(OpportunityLineitem oli:oliList){
    if(oli.Quantity!=null){
         remainingQuantity=integer.valueOf(oli.Product2.Total_Quantity__c) - integer.valueOf(oli.Quantity);
     }
     else{
         remainingQuantity=0;  
     }
     if(remainingQuantity!=null){
     oli.Product2.Available_Quantity__c=integer.valueOf(remainingQuantity);
     }
}
update oliList;
}
Thank you

Hi, everyone!

Here I need to create trigger that support gift cards which allows users to reduce Opportunity price on fixed amount. Opportunity and Gift Card has no relationship. User has write name of gift card in opportunity then its reduce amount opportunity for the amount of gift card.

public with sharing class GiftCardTriggerHandler {
    public void afterUpdate(List<Gift_Card__c> newGiftCards) {
        Set<String> opportunityNames = new Set<String>();
        for (Gift_Card__c giftCardItem : newGiftCards) {
            opportunityNames.add(giftCardItem.Name);
        }
        Map<ID, Opportunity> opportunitiesToUpdate = new Map<ID, Opportunity>([SELECT Id, Gift_Card__c, Amount FROM Opportunity WHERE Gift_Card__c IN: opportunityNames]);
        List<Gift_Card__c> giftCardToInsert = [SELECT Id, Name, Amount__c, Active__c FROM Gift_Card__c WHERE Name IN: opportunityNames];    

        for(String opportunityName : opportunityNames) {
            Decimal Amount = 0;
            Opportunity currentOpportunity = opportunitiesToUpdate.get(opportunityName);
            System.debug('Null' + currentOpportunity.Amount);
            for(Gift_Card__c giftCardItem : giftCardToInsert) {            
                if(giftCardItem.Name == opportunityName) {
                    if(giftCardItem.Active__c == true) {
                        Amount = currentOpportunity.Amount - giftCardItem.Amount__c;
                        giftCardItem.Active__c = false;
                    } 
                    else if(giftCardItem.Active__c == false) {
                           opportunityName.addError('Please, choose active Gift Card!'); 
                    }            
                } 
            }
            if (currentOpportunity.Amount < Amount) {
                currentOpportunity.Amount = Amount;
                opportunitiesToUpdate.put(opportunityName, currentOpportunity);
            }
        } 
        update opportunitiesToUpdate.values();
    }
}