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
PRIYAN NEERUDUPRIYAN NEERUDU 

RECORD TYPE FIELD UPDATE

User-added image

I have three record type C2B B2B D2B for contacts i have to update account amount filed with sum of contacts  amount field WITH DIFFERENT RECORDTYPE

eg: 10,000(C2B AMOUNT)+10,000(D2B MOUNT )-10,000(B2B AMOUNT)=AMOUNT(ACCOUNT FILED)


 
Best Answer chosen by PRIYAN NEERUDU
harsha__charsha__c
trigger CONUP on Contact (after update,after insert) {
    Map<Id, Decimal> acctIdToAmount = new Map<Id, Decimal>();
    List<Account> accountsToUpdate = new List<Account>();
    Map<ID,RecordType> typeMap = New Map<ID,RecordType>([Select ID, DeveloperName From RecordType Where sObjectType = 'CONTACT']);
	for (CONTACT CON : trigger.new) {
      	// If the Record Type = Intake Form
      	if (typeMap.get(CON.RecordTypeId).DeveloperName == 'C2B' || typeMap.get(CON.RecordTypeId).DeveloperName == 'D2B') {
        	//	Get the Contact Amount into a temp variable
        	Decimal sum = CON.AMOUNT_FIELD == null ? 0 : CON.AMOUNT_FIELD;

        	//	Sum it up with the already collected Amount	
        	if(acctIdToAmount.containsKey(CON.AccountId))
        		sum += acctIdToAmount.get(CON.AccountId); 
        	
        	//	Make a map with AccountId as Key and Amount as value 	
    	 	acctIdToAmount.put(CON.AccountId, sum);
      	}
      	else if(typeMap.get(CON.RecordTypeId).DeveloperName == 'E2B') {
      		Decimal sum = 0;

        	if(acctIdToAmount.containsKey(CON.AccountId))
        		sum = acctIdToAmount.get(CON.AccountId);

    		sum -= CON.AMOUNT_FIELD == null ? 0 : CON.AMOUNT_FIELD; 
        	 	
    	 	acctIdToAmount.put(CON.AccountId, sum);	
      	}      
    }

    for(Id accId : acctIdToAmount.keyset()) {
        Account acc;
    	if(acctIdToAmount.get(accId) >= 0)
               acc = new Account(Id = accId, Total_Amount__c = acctIdToAmount.get(accId));
        else
               acc = new Account(Id = accId, Pending_Amount__c = acctIdToAmount.get(accId));
    	accountsToUpdate.add(acc);
    }

    if(!accountsToUpdate.isEmpty())
    	update accountsToUpdate;
}

This should serve your purpose!

- Harsha

All Answers

harsha__charsha__c
You will have to take the trigger way for this and have your custom logic in there. Neither  Formula nor Rollup can acheive this.

- Harsha
PRIYAN NEERUDUPRIYAN NEERUDU
trigger CONUP on Contact (after update,after insert) {
    Map<ID,RecordType> typeMap = New Map<ID,RecordType>([Select ID, DeveloperName From RecordType Where sObjectType = 'CONTACT']);
for (CONTACT CON : trigger.new)
  {
      LIST<Account> acc=[SELECT PAID_AMOUNT__c,BALANCE_AMOUNT__c,amount__c FROM Account ];
          // If the Record Type = Intake Form
      if (CON.RecordType.DeveloperName == 'C2B' || CON.RecordType.DeveloperName == 'D2B')
      {
         
      }      
      }
  }


================= 

m unable to query this further ...........
harsha__charsha__c
trigger CONUP on Contact (after update,after insert) {
    Map<Id, Decimal> acctIdToAmount = new Map<Id, Decimal>();
    List<Account> accountsToUpdate = new List<Account>();
    Map<ID,RecordType> typeMap = New Map<ID,RecordType>([Select ID, DeveloperName From RecordType Where sObjectType = 'CONTACT']);
	for (CONTACT CON : trigger.new) {
      	// If the Record Type = Intake Form
      	if (typeMap.get(CON.RecordTypeId).DeveloperName == 'C2B' || typeMap.get(CON.RecordTypeId).DeveloperName == 'D2B') {
        	//	Get the Contact Amount into a temp variable
        	Decimal sum = CON.AMOUNT_FIELD == null ? 0 : CON.AMOUNT_FIELD;

        	//	Sum it up with the already collected Amount	
        	if(acctIdToAmount.containsKey(CON.AccountId))
        		sum += acctIdToAmount.get(CON.AccountId); 
        	
        	//	Make a map with AccountId as Key and Amount as value 	
    	 	acctIdToAmount.put(CON.AccountId, sum);
      	}
      	else if(typeMap.get(CON.RecordTypeId).DeveloperName == 'E2B') {
      		Decimal sum = 0;

        	if(acctIdToAmount.containsKey(CON.AccountId))
        		sum = acctIdToAmount.get(CON.AccountId);

    		sum -= CON.AMOUNT_FIELD == null ? 0 : CON.AMOUNT_FIELD; 
        	 	
    	 	acctIdToAmount.put(CON.AccountId, sum);	
      	}      
    }

    for(Id accId : acctIdToAmount.keyset()) {
    	Account acc = new Account(Id = accId, Amount = acctIdToAmount.get(accId));
    	accountsToUpdate.add(acc);
    }

    if(!accountsToUpdate.isEmpty())
    	update accountsToUpdate;
}


This should work. Give a try.

- Harsha
harsha__charsha__c
trigger CONUP on Contact (after update,after insert) {
    Map<Id, Decimal> acctIdToAmount = new Map<Id, Decimal>();
    List<Account> accountsToUpdate = new List<Account>();
    Map<ID,RecordType> typeMap = New Map<ID,RecordType>([Select ID, DeveloperName From RecordType Where sObjectType = 'CONTACT']);
	for (CONTACT CON : trigger.new) {
      	// If the Record Type = Intake Form
      	if (typeMap.get(CON.RecordTypeId).DeveloperName == 'C2B' || typeMap.get(CON.RecordTypeId).DeveloperName == 'D2B') {
        	//	Get the Contact Amount into a temp variable
        	Decimal sum = CON.AMOUNT_FIELD == null ? 0 : CON.AMOUNT_FIELD;

        	//	Sum it up with the already collected Amount	
        	if(acctIdToAmount.containsKey(CON.AccountId))
        		sum += acctIdToAmount.get(CON.AccountId); 
        	
        	//	Make a map with AccountId as Key and Amount as value 	
    	 	acctIdToAmount.put(CON.AccountId, sum);
      	}
      	else if(typeMap.get(CON.RecordTypeId).DeveloperName == 'E2B') {
      		Decimal sum = 0;

        	if(acctIdToAmount.containsKey(CON.AccountId))
        		sum = acctIdToAmount.get(CON.AccountId);

    		sum -= CON.AMOUNT_FIELD == null ? 0 : CON.AMOUNT_FIELD; 
        	 	
    	 	acctIdToAmount.put(CON.AccountId, sum);	
      	}      
    }

    for(Id accId : acctIdToAmount.keyset()) {
        Account acc;
    	if(acctIdToAmount.get(accId) >= 0)
               acc = new Account(Id = accId, Total_Amount__c = acctIdToAmount.get(accId));
        else
               acc = new Account(Id = accId, Pending_Amount__c = acctIdToAmount.get(accId));
    	accountsToUpdate.add(acc);
    }

    if(!accountsToUpdate.isEmpty())
    	update accountsToUpdate;
}

This should serve your purpose!

- Harsha
This was selected as the best answer