• SFDC Admin 56
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 3
    Replies
This is a formulae filed and Cornerstone_Created_Date is a text field of 1/1/2017 12:00:00 AM format. on page layout its showing exactly what i want but in reports, when i try to enter this column, its showing me error on all the records.

DATETIMEVALUE( 

/* get the year */ 
RIGHT(LEFT(Cornerstone_Created_Date__c, FIND(" ", Cornerstone_Created_Date__c ) - 1), 4) 

& "-" & 

/* get the month */ 
LEFT(Cornerstone_Created_Date__c , FIND("/", Cornerstone_Created_Date__c ) - 1) 

& "-" & 

/* get the day */ 
LEFT( 
MID(Cornerstone_Created_Date__c, FIND("/", Cornerstone_Created_Date__c ) + 1, 4), 
FIND("/", MID(Cornerstone_Created_Date__c , FIND("/", Cornerstone_Created_Date__c) + 1, 4)) - 1 


& " " & 

/* get the Time */ 
MID(Cornerstone_Created_Date__c, 
FIND(" ", Cornerstone_Created_Date__c ), 
LEN(Cornerstone_Created_Date__c ) - FIND(" ",Cornerstone_Created_Date__c ) + 1 

)
I am trying to update Premier rep new fiel from dataloader and some records are updating but on some its giving me two types of error.

1) caused by: System.DmlException: Insert failed. First exception on row 1; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [UserOrGroupId]: [UserOrGroupId]

2)Sharingupdatepremier: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [] 

I have tried every condition and unable to figure out, any input is appereciated.

trigger Sharingupdatepremier on Account (before update) 
{
 Legal_Doc__Share PrimarysalesrepShr = new Legal_Doc__Share();

    if(trigger.isUpdate)
    {
        Set<Id> setAcc = new Set<Id>();
        Set<String> setPrimSal= new Set<String>();
        for (Account a : Trigger.old )
        {
            setAcc.add(a.id);
            setPrimSal.add(a.Premier_Rep2__c);
        }
        
        List<Legal_Doc__c> legaldocu = [Select id From Legal_Doc__c where Accounts__c in : setAcc];
        List<Legal_Doc__Share> legdoc = [Select UserOrGroupId From Legal_Doc__Share Where UserOrGroupId in :setPrimSal and ParentId = :legaldocu and  RowCause = 'Manual' ];
    
        if(!legdoc.isEmpty())
        {
            Delete legdoc;
        }
    } 
        
    List<Legal_Doc__Share> ListPrimarysalesrepShr = new List<Legal_Doc__Share>();
                
    Set<ID> setAccId = new Set<Id>();
    for (Account an : Trigger.new )
    {
        setAccId.add(an.id);
    }
    List<Legal_Doc__c> Listlegaldocum = [Select id,Accounts__c From Legal_Doc__c where Accounts__c in :setAccId];
    Map<Id,Legal_Doc__c> mapAccWiseLegalIDoc = new Map<Id,Legal_Doc__c>();           
    for(Legal_Doc__c leg : Listlegaldocum)
    {
        mapAccWiseLegalIDoc.put(leg.Accounts__c,leg);
    }
    
    for (Account an : Trigger.new )
    {
        
        If(mapAccWiseLegalIDoc.containsKey(an.Id))
        {
            PrimarysalesrepShr = new Legal_Doc__Share();
           
            PrimarysalesrepShr.ParentId = mapAccWiseLegalIDoc.get(an.Id).Id;
            PrimarysalesrepShr.UserOrGroupId = an.Premier_Rep2__c;
            PrimarysalesrepShr.AccessLevel = 'edit';
            
            ListPrimarysalesrepShr.add(PrimarysalesrepShr);
        }
    }
    if (Account.Premier_Rep2__c!=null)
    if (PrimarysalesrepShr.UserOrGroupId!=null){
    if(!ListPrimarysalesrepShr.isEmpty())
    {
        insert ListPrimarysalesrepShr ;
    }
}
}
Before Insert or Upsert list must not have two identically equal elements
When i am trying to update records from data loader, its giving me this error.Before Insert or Upsert list must not have two identically equal elements.
Below is my code and how can i change List to map?



trigger Sharingupdate on Account (after update) 
{
 Legal_Doc__Share PrimarysalesrepShr = new Legal_Doc__Share();{

    if(trigger.isUpdate)
    {
        Set<Id> setAcc = new Set<Id>();
        Set<String> setPrimSal= new Set<String>();
        for (Account a : Trigger.old )
        {
            setAcc.add(a.id);
            setPrimSal.add(a.Primary_Sales_Rep2__c);
        }
        
        List<Legal_Doc__c> legaldocu = [Select id From Legal_Doc__c where Accounts__c in : setAcc];
        List<Legal_Doc__Share> legdoc = [Select UserOrGroupId From Legal_Doc__Share Where UserOrGroupId in :setPrimSal and ParentId = :legaldocu ];
    
        if(legdoc!= null)
        {
            Delete legdoc;
        }
    } 
        
    List<Legal_Doc__Share> ListPrimarysalesrepShr = new List<Legal_Doc__Share>();
                
    Set<ID> setAccId = new Set<Id>();
    for (Account an : Trigger.new )
    {
        setAccId.add(an.id);
    }
    List<Legal_Doc__c> Listlegaldocum = [Select id,Accounts__c From Legal_Doc__c where Accounts__c in :setAccId];
    Map<Id,Legal_Doc__c> mapAccWiseLegalIDoc = new Map<Id,Legal_Doc__c>();           
    for(Legal_Doc__c leg : Listlegaldocum)
    {
        mapAccWiseLegalIDoc.put(leg.Accounts__c,leg);
    }
    
    for (Account an : Trigger.new )
    {
        
        If(mapAccWiseLegalIDoc.containsKey(an.Id))
        {
           
            PrimarysalesrepShr.ParentId = mapAccWiseLegalIDoc.get(an.Id).Id;
            PrimarysalesrepShr.UserOrGroupId = an.Primary_Sales_Rep2__c ;
            PrimarysalesrepShr.AccessLevel = 'edit';
            
            ListPrimarysalesrepShr.add(PrimarysalesrepShr);
        }
    }
    if (PrimarysalesrepShr.UserOrGroupId!=null){
    if(ListPrimarysalesrepShr.size() > 0 )
    {
        insert ListPrimarysalesrepShr ;
    }
}

}
}
When i am trying to update records from data loader, its giving me this error.Before Insert or Upsert list must not have two identically equal elements.
Below is my code.



trigger Sharingupdate on Account (after update) 
{
 Legal_Doc__Share PrimarysalesrepShr = new Legal_Doc__Share();{

    if(trigger.isUpdate)
    {
        Set<Id> setAcc = new Set<Id>();
        Set<String> setPrimSal= new Set<String>();
        for (Account a : Trigger.old )
        {
            setAcc.add(a.id);
            setPrimSal.add(a.Primary_Sales_Rep2__c);
        }
        
        List<Legal_Doc__c> legaldocu = [Select id From Legal_Doc__c where Accounts__c in : setAcc];
        List<Legal_Doc__Share> legdoc = [Select UserOrGroupId From Legal_Doc__Share Where UserOrGroupId in :setPrimSal and ParentId = :legaldocu ];
    
        if(legdoc!= null)
        {
            Delete legdoc;
        }
    } 
        
    List<Legal_Doc__Share> ListPrimarysalesrepShr = new List<Legal_Doc__Share>();
                
    Set<ID> setAccId = new Set<Id>();
    for (Account an : Trigger.new )
    {
        setAccId.add(an.id);
    }
    List<Legal_Doc__c> Listlegaldocum = [Select id,Accounts__c From Legal_Doc__c where Accounts__c in :setAccId];
    Map<Id,Legal_Doc__c> mapAccWiseLegalIDoc = new Map<Id,Legal_Doc__c>();           
    for(Legal_Doc__c leg : Listlegaldocum)
    {
        mapAccWiseLegalIDoc.put(leg.Accounts__c,leg);
    }
    
    for (Account an : Trigger.new )
    {
        
        If(mapAccWiseLegalIDoc.containsKey(an.Id))
        {
           
            PrimarysalesrepShr.ParentId = mapAccWiseLegalIDoc.get(an.Id).Id;
            PrimarysalesrepShr.UserOrGroupId = an.Primary_Sales_Rep2__c ;
            PrimarysalesrepShr.AccessLevel = 'edit';
            
            ListPrimarysalesrepShr.add(PrimarysalesrepShr);
        }
    }
    if (PrimarysalesrepShr.UserOrGroupId!=null){
    if(ListPrimarysalesrepShr.size() > 0 )
    {
        insert ListPrimarysalesrepShr ;
    }
}

}
}
I am trying to Bulkify this trigger as we will use data loader in future to insert value in Primary sales rep field and with this existing code this trigger will fail, can you please hepl me in changing the code so that it will not hit governer limits.\

Here i am deleting the sharing permission for the previous assigned user and giving sharing permission to new user.

trigger Sharingupdate on Account (after update) {

    

     

        if(trigger.isUpdate){
            for (Account a : Trigger.old ){
                List<Legal_Doc__c> legaldocu = [Select id From Legal_Doc__c where Accounts__c= : a.id];
                List<Legal_Doc__Share> legdoc = [Select UserOrGroupId From Legal_Doc__Share Where UserOrGroupId =: a.Primary_Sales_Rep2__c and ParentId = :legaldocu ];
        
                                            
                    if(legdoc!= null){
        
                         Delete legdoc;
                                     }
                                    }     
                              } 
        
        
                                    
        
        
         Legal_Doc__Share PrimarysalesrepShr;
         PrimarysalesrepShr = new Legal_Doc__Share();
                   

        
    List<Legal_Doc__Share> legalShrs  = new List<Legal_Doc__Share>();
        for (Account an : Trigger.new ){
        
       
          Legal_Doc__c legaldocum = [Select id From Legal_Doc__c where Accounts__c= :an.id];
               

                    PrimarysalesrepShr.ParentId = legaldocum.Id;
                    PrimarysalesrepShr.UserOrGroupId = an.Primary_Sales_Rep2__c ;
                    PrimarysalesrepShr.AccessLevel = 'edit';

         
                                        }
                          insert PrimarysalesrepShr ;
                                                     }
Before Insert or Upsert list must not have two identically equal elements
When i am trying to update records from data loader, its giving me this error.Before Insert or Upsert list must not have two identically equal elements.
Below is my code and how can i change List to map?



trigger Sharingupdate on Account (after update) 
{
 Legal_Doc__Share PrimarysalesrepShr = new Legal_Doc__Share();{

    if(trigger.isUpdate)
    {
        Set<Id> setAcc = new Set<Id>();
        Set<String> setPrimSal= new Set<String>();
        for (Account a : Trigger.old )
        {
            setAcc.add(a.id);
            setPrimSal.add(a.Primary_Sales_Rep2__c);
        }
        
        List<Legal_Doc__c> legaldocu = [Select id From Legal_Doc__c where Accounts__c in : setAcc];
        List<Legal_Doc__Share> legdoc = [Select UserOrGroupId From Legal_Doc__Share Where UserOrGroupId in :setPrimSal and ParentId = :legaldocu ];
    
        if(legdoc!= null)
        {
            Delete legdoc;
        }
    } 
        
    List<Legal_Doc__Share> ListPrimarysalesrepShr = new List<Legal_Doc__Share>();
                
    Set<ID> setAccId = new Set<Id>();
    for (Account an : Trigger.new )
    {
        setAccId.add(an.id);
    }
    List<Legal_Doc__c> Listlegaldocum = [Select id,Accounts__c From Legal_Doc__c where Accounts__c in :setAccId];
    Map<Id,Legal_Doc__c> mapAccWiseLegalIDoc = new Map<Id,Legal_Doc__c>();           
    for(Legal_Doc__c leg : Listlegaldocum)
    {
        mapAccWiseLegalIDoc.put(leg.Accounts__c,leg);
    }
    
    for (Account an : Trigger.new )
    {
        
        If(mapAccWiseLegalIDoc.containsKey(an.Id))
        {
           
            PrimarysalesrepShr.ParentId = mapAccWiseLegalIDoc.get(an.Id).Id;
            PrimarysalesrepShr.UserOrGroupId = an.Primary_Sales_Rep2__c ;
            PrimarysalesrepShr.AccessLevel = 'edit';
            
            ListPrimarysalesrepShr.add(PrimarysalesrepShr);
        }
    }
    if (PrimarysalesrepShr.UserOrGroupId!=null){
    if(ListPrimarysalesrepShr.size() > 0 )
    {
        insert ListPrimarysalesrepShr ;
    }
}

}
}
I am trying to Bulkify this trigger as we will use data loader in future to insert value in Primary sales rep field and with this existing code this trigger will fail, can you please hepl me in changing the code so that it will not hit governer limits.\

Here i am deleting the sharing permission for the previous assigned user and giving sharing permission to new user.

trigger Sharingupdate on Account (after update) {

    

     

        if(trigger.isUpdate){
            for (Account a : Trigger.old ){
                List<Legal_Doc__c> legaldocu = [Select id From Legal_Doc__c where Accounts__c= : a.id];
                List<Legal_Doc__Share> legdoc = [Select UserOrGroupId From Legal_Doc__Share Where UserOrGroupId =: a.Primary_Sales_Rep2__c and ParentId = :legaldocu ];
        
                                            
                    if(legdoc!= null){
        
                         Delete legdoc;
                                     }
                                    }     
                              } 
        
        
                                    
        
        
         Legal_Doc__Share PrimarysalesrepShr;
         PrimarysalesrepShr = new Legal_Doc__Share();
                   

        
    List<Legal_Doc__Share> legalShrs  = new List<Legal_Doc__Share>();
        for (Account an : Trigger.new ){
        
       
          Legal_Doc__c legaldocum = [Select id From Legal_Doc__c where Accounts__c= :an.id];
               

                    PrimarysalesrepShr.ParentId = legaldocum.Id;
                    PrimarysalesrepShr.UserOrGroupId = an.Primary_Sales_Rep2__c ;
                    PrimarysalesrepShr.AccessLevel = 'edit';

         
                                        }
                          insert PrimarysalesrepShr ;
                                                     }