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
Ashok S 7Ashok S 7 

how to slove this requirment

hai guys,
I have one requirments i have two objects one is x and another one is y . x and y have lookup relationship x object contains two fields.
1.primary exp.2.secondary exp. y object contains role field it is a picklist field when role value is primary then increment primary exp the value in the  x object same as when role value is secondary then increment secondary exp value in the x object.
how can i slove this requirment .please help me urgent
JAY_PJAY_P
You mean to say that if y object field primary is set to x object primary exp and same with secondary value assign to secondary exp which is in x object???
Ashok S 7Ashok S 7
No,
in x object primary exp is a field which is number data type i did not give any value.
when in the y object role field is  == 'primary' and click on save after in x object primary exp field become value '1'
suresh sanneboina 4suresh sanneboina 4
Hi,

Please find the below code
Trigger updatexobject on Y(after insert,after update)
{
	Map<Id,Integer> mapxprimary=new Map<Id,Integer>();
	Map<Id,Integer> mapxSecondary=new Map<Id,Integer>();
	for(Y yValues:Trigger.new)
	{
		if(yValues.Role != null && yValues.x != null)
		{
			if(yValues.Role == 'primary')
			{
				if(!mapxprimary.containsKey(yValues.x)){
					mapxprimary.put(yValues.x,mapxprimary.get(yValues.x)+1);
				}else
				{
					mapxprimary.put(yValues.x,1);
				}
			}else{
				if(!mapxSecondary.containsKey(yValues.x)){
					mapxSecondary.put(yValues.x,mapxSecondary.get(yValues.x)+1);
				}else
				{
					mapxSecondary.put(yValues.x,1);
				}
			}
		}
	}
	
	List<x> lstx=new List<x>();
	if(!mapxprimary.isEmpty())
	{
		for(Id xobj:mapxprimary.keySet())
		{
			x xobject=new x(Id=xobj,primaryexp=mapxprimary.get(xobj));
			lstx.add(xobject);
		}
	}
	if(!mapxSecondary.isEmpty())
	{
		for(Id xobj:mapxSecondary.keySet())
		{
			x xobject=new x(Id=xobj,secondary exp=mapxSecondary.get(xobj));
			lstx.add(xobject);
		}
	}
	if(!lstx.isEmpty())
	{
		update lstx;
	}
}
Ashok S 7Ashok S 7
i can the code
Ashok S 7Ashok S 7
but it is my code
trigger CalucateNumberContact on Contacts_Involved_in_Procedure__c (After insert,After update,After delete)
{
 
    Set<Id> ContactIds=new Set<Id>();
   List<Contact> updateAccount=new List<Contact>();
   contact co = new contact();
    co.Primary = 0;
  
   
     if(Trigger.isInsert||Trigger.isUpdate){
     for(Contacts_Involved_in_Procedure__c  accountdata:Trigger.new){
         ContactIds.add(accountdata.Contact__c);
     
     }
   }
   
   if(Trigger.isdelete)
   {
   
      for(Contacts_Involved_in_Procedure__c  oldacc:Trigger.old){
           ContactIds.add(oldacc.Contact__c);
      
      }
   
   }

   
 
  List<Contact> Listaccount=[select id,Primary from Contact where Id=:ContactIds];
     
  List<Contacts_Involved_in_Procedure__c> contac= [select id ,Role__c from Contacts_Involved_in_Procedure__c where Contact__c=:ContactIds];
 
       
           Integer count=0;
          
            if(con1.Role__c=='Primary Operator' || con1.Role__c=='Primary Echocardiologist'){
                count++;
               for(Contact acc12:Listaccount)
               {
                 acc12.Primary =count;
                 
                 }
            
            }
               
           }
         update Listaccount;
}

and it is incrementing. When i deleted all the record the primary value is not coming to hte zero.please refer below pic.

User-added image
in the above pic contact is the x object and contacts attending is the y object.Here in the contact attending there is no record i deleted but the above primary value is "1" not "0".
please help any one .Urgent
Ashok S 7Ashok S 7
Please tell me how to remove those "1" also.
please help me
suresh sanneboina 4suresh sanneboina 4
the code which you pasted is incorrect some lines of code is missing.

Please use the below code sample
Trigger updatexobject on Y(after insert,after update,after delete)
{
	Map<Id,Integer> mapxprimary=new Map<Id,Integer>();
	Map<Id,Integer> mapxSecondary=new Map<Id,Integer>();
	if(Trigger.insert || Trigger.isUpdate){
		for(Y yValues:Trigger.new)
		{
			if(yValues.Role != null && yValues.x != null)
			{
				if(yValues.Role == 'primary')
				{
					if(!mapxprimary.containsKey(yValues.x)){
						mapxprimary.put(yValues.x,mapxprimary.get(yValues.x)+1);
					}else
					{
						mapxprimary.put(yValues.x,1);
					}
				}else{
					if(!mapxSecondary.containsKey(yValues.x)){
						mapxSecondary.put(yValues.x,mapxSecondary.get(yValues.x)+1);
					}else
					{
						mapxSecondary.put(yValues.x,1);
					}
				}
			}
		}
	}
	if(Trigger.isDelete )
	{
		for(Y yValues:Trigger.Old)
		{
			if(yValues.Role != null && yValues.x != null)
			{
				if(yValues.Role == 'primary')
				{
					if(!mapxprimary.containsKey(yValues.x)){
						mapxprimary.put(yValues.x,mapxprimary.get(yValues.x)-1);
					}else
					{
						mapxprimary.put(yValues.x,-1);
					}
				}else{
					if(!mapxSecondary.containsKey(yValues.x)){
						mapxSecondary.put(yValues.x,mapxSecondary.get(yValues.x)-1);
					}else
					{
						mapxSecondary.put(yValues.x,-1);
					}
				}
			}
		}
	}
	
	List<x> lstx=new List<x>();
	if(!mapxprimary.isEmpty())
	{	
		for(x xobj:Select Id,primaryexp from x Where ID IN : mapxprimary.keySet())
		{
			x xobject=new x(Id=xobj.Id,primaryexp=(xobj.primaryexp != null? xobj.primaryexp+ mapxprimary.get(xobj.Id):mapxprimary.get(xobj.Id)));
			lstx.add(xobject);
		}
	}
	if(!mapxSecondary.isEmpty())
	{
		for(x xobj:Select Id,secondaryexp from x Where ID IN : mapxSecondary.keySet())
		{
			x xobject=new x(Id=xobj.Id,secondaryexp=(xobj.secondaryexp != null?xobj.secondaryexp + mapxSecondary.get(xobj.Id):mapxSecondary.get(xobj.Id)));
			lstx.add(xobject);
		}
	}
	if(!lstx.isEmpty())
	{
		update lstx;
	}
}